]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/netinet6/udp6_usrreq.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / netinet6 / udp6_usrreq.c
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, 1989, 1993
35  *      The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
62  */
63
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66
67 #include "opt_inet.h"
68 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
70
71 #include <sys/param.h>
72 #include <sys/errno.h>
73 #include <sys/kernel.h>
74 #include <sys/lock.h>
75 #include <sys/mbuf.h>
76 #include <sys/proc.h>
77 #include <sys/protosw.h>
78 #include <sys/signalvar.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/stat.h>
82 #include <sys/sx.h>
83 #include <sys/sysctl.h>
84 #include <sys/syslog.h>
85 #include <sys/systm.h>
86
87 #include <net/if.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip6.h>
97 #include <netinet/icmp6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/udp.h>
100 #include <netinet/udp_var.h>
101 #include <netinet6/ip6protosw.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/udp6_var.h>
105 #include <netinet6/scope6_var.h>
106
107 #ifdef IPSEC
108 #include <netinet6/ipsec.h>
109 #include <netinet6/ipsec6.h>
110 #endif /* IPSEC */
111
112 #ifdef FAST_IPSEC
113 #include <netipsec/ipsec.h>
114 #include <netipsec/ipsec6.h>
115 #endif /* FAST_IPSEC */
116
117 /*
118  * UDP protocol inplementation.
119  * Per RFC 768, August, 1980.
120  */
121
122 extern  struct protosw inetsw[];
123 static  int udp6_detach __P((struct socket *so));
124
125 int
126 udp6_input(mp, offp, proto)
127         struct mbuf **mp;
128         int *offp, proto;
129 {
130         struct mbuf *m = *mp, *opts;
131         register struct ip6_hdr *ip6;
132         register struct udphdr *uh;
133         register struct inpcb *in6p;
134         int off = *offp;
135         int plen, ulen;
136         struct sockaddr_in6 fromsa;
137
138         opts = NULL;
139
140         ip6 = mtod(m, struct ip6_hdr *);
141
142         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
143                 /* XXX send icmp6 host/port unreach? */
144                 m_freem(m);
145                 return IPPROTO_DONE;
146         }
147
148 #ifndef PULLDOWN_TEST
149         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
150         ip6 = mtod(m, struct ip6_hdr *);
151         uh = (struct udphdr *)((caddr_t)ip6 + off);
152 #else
153         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
154         if (!uh)
155                 return IPPROTO_DONE;
156 #endif
157
158         udpstat.udps_ipackets++;
159
160         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
161         ulen = ntohs((u_short)uh->uh_ulen);
162
163         if (plen != ulen) {
164                 udpstat.udps_badlen++;
165                 goto bad;
166         }
167
168         /*
169          * Checksum extended UDP header and data.
170          */
171         if (uh->uh_sum == 0) {
172                 udpstat.udps_nosum++;
173                 goto bad;
174         }
175         if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
176                 udpstat.udps_badsum++;
177                 goto bad;
178         }
179
180         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
181                 struct  inpcb *last;
182
183                 /*
184                  * Deliver a multicast datagram to all sockets
185                  * for which the local and remote addresses and ports match
186                  * those of the incoming datagram.  This allows more than
187                  * one process to receive multicasts on the same port.
188                  * (This really ought to be done for unicast datagrams as
189                  * well, but that would cause problems with existing
190                  * applications that open both address-specific sockets and
191                  * a wildcard socket listening to the same port -- they would
192                  * end up receiving duplicates of every unicast datagram.
193                  * Those applications open the multiple sockets to overcome an
194                  * inadequacy of the UDP socket interface, but for backwards
195                  * compatibility we avoid the problem here rather than
196                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
197                  */
198
199                 /*
200                  * In a case that laddr should be set to the link-local
201                  * address (this happens in RIPng), the multicast address
202                  * specified in the received packet does not match with
203                  * laddr. To cure this situation, the matching is relaxed
204                  * if the receiving interface is the same as one specified
205                  * in the socket and if the destination multicast address
206                  * matches one of the multicast groups specified in the socket.
207                  */
208
209                 /*
210                  * Construct sockaddr format source address.
211                  */
212                 init_sin6(&fromsa, m);
213                 fromsa.sin6_port = uh->uh_sport;
214                 /*
215                  * KAME note: traditionally we dropped udpiphdr from mbuf here.
216                  * We need udphdr for IPsec processing so we do that later.
217                  */
218
219                 /*
220                  * Locate pcb(s) for datagram.
221                  * (Algorithm copied from raw_intr().)
222                  */
223                 last = NULL;
224                 LIST_FOREACH(in6p, &udb, inp_list) {
225                         if ((in6p->inp_vflag & INP_IPV6) == 0)
226                                 continue;
227                         if (in6p->in6p_lport != uh->uh_dport)
228                                 continue;
229                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
230                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
231                                                         &ip6->ip6_dst))
232                                         continue;
233                         }
234                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
235                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
236                                                         &ip6->ip6_src) ||
237                                     in6p->in6p_fport != uh->uh_sport)
238                                         continue;
239                         }
240
241                         if (last != NULL) {
242                                 struct mbuf *n;
243
244 #if defined(IPSEC) || defined(FAST_IPSEC)
245                                 /*
246                                  * Check AH/ESP integrity.
247                                  */
248                                 if (ipsec6_in_reject(m, last)) {
249 #ifdef IPSEC
250                                         ipsec6stat.in_polvio++;
251 #endif /* IPSEC */
252                                         /* do not inject data into pcb */
253                                 } else
254 #endif /*IPSEC || FAST_IPSEC*/
255                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
256                                         /*
257                                          * KAME NOTE: do not
258                                          * m_copy(m, offset, ...) above.
259                                          * sbappendaddr() expects M_PKTHDR,
260                                          * and m_copy() will copy M_PKTHDR
261                                          * only if offset is 0.
262                                          */
263                                         if (last->in6p_flags & IN6P_CONTROLOPTS
264                                             || last->in6p_socket->so_options & SO_TIMESTAMP)
265                                                 ip6_savecontrol(last, n, &opts);
266
267                                         m_adj(n, off + sizeof(struct udphdr));
268                                         if (sbappendaddr(&last->in6p_socket->so_rcv,
269                                                         (struct sockaddr *)&fromsa,
270                                                         n, opts) == 0) {
271                                                 m_freem(n);
272                                                 if (opts)
273                                                         m_freem(opts);
274                                                 udpstat.udps_fullsock++;
275                                         } else
276                                                 sorwakeup(last->in6p_socket);
277                                         opts = NULL;
278                                 }
279                         }
280                         last = in6p;
281                         /*
282                          * Don't look for additional matches if this one does
283                          * not have either the SO_REUSEPORT or SO_REUSEADDR
284                          * socket options set.  This heuristic avoids searching
285                          * through all pcbs in the common case of a non-shared
286                          * port.  It assumes that an application will never
287                          * clear these options after setting them.
288                          */
289                         if ((last->in6p_socket->so_options &
290                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
291                                 break;
292                 }
293
294                 if (last == NULL) {
295                         /*
296                          * No matching pcb found; discard datagram.
297                          * (No need to send an ICMP Port Unreachable
298                          * for a broadcast or multicast datgram.)
299                          */
300                         udpstat.udps_noport++;
301                         udpstat.udps_noportmcast++;
302                         goto bad;
303                 }
304 #if defined(IPSEC) || defined(FAST_IPSEC)
305                 /*
306                  * Check AH/ESP integrity.
307                  */
308                 if (ipsec6_in_reject(m, last)) {
309 #ifdef IPSEC
310                         ipsec6stat.in_polvio++;
311 #endif /* IPSEC */
312                         goto bad;
313                 }
314 #endif /*IPSEC || FAST_IPSEC*/
315                 if (last->in6p_flags & IN6P_CONTROLOPTS
316                     || last->in6p_socket->so_options & SO_TIMESTAMP)
317                         ip6_savecontrol(last, m, &opts);
318
319                 m_adj(m, off + sizeof(struct udphdr));
320                 if (sbappendaddr(&last->in6p_socket->so_rcv,
321                                 (struct sockaddr *)&fromsa,
322                                 m, opts) == 0) {
323                         udpstat.udps_fullsock++;
324                         goto bad;
325                 }
326                 sorwakeup(last->in6p_socket);
327                 return IPPROTO_DONE;
328         }
329         /*
330          * Locate pcb for datagram.
331          */
332         in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
333                                   &ip6->ip6_dst, uh->uh_dport, 1,
334                                   m->m_pkthdr.rcvif);
335         if (in6p == 0) {
336                 if (log_in_vain) {
337                         char buf[INET6_ADDRSTRLEN];
338
339                         strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
340                         log(LOG_INFO,
341                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
342                             buf, ntohs(uh->uh_dport),
343                             ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
344                 }
345                 udpstat.udps_noport++;
346                 if (m->m_flags & M_MCAST) {
347                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
348                         udpstat.udps_noportmcast++;
349                         goto bad;
350                 }
351                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
352                 return IPPROTO_DONE;
353         }
354 #if defined(IPSEC) || defined(FAST_IPSEC)
355         /*
356          * Check AH/ESP integrity.
357          */
358         if (ipsec6_in_reject(m, in6p)) {
359 #ifdef IPSEC
360                 ipsec6stat.in_polvio++;
361 #endif /* IPSEC */
362                 goto bad;
363         }
364 #endif /*IPSEC || FAST_IPSEC*/
365
366         /*
367          * Construct sockaddr format source address.
368          * Stuff source address and datagram in user buffer.
369          */
370         init_sin6(&fromsa, m);
371         fromsa.sin6_port = uh->uh_sport;
372         if (in6p->in6p_flags & IN6P_CONTROLOPTS
373             || in6p->in6p_socket->so_options & SO_TIMESTAMP)
374                 ip6_savecontrol(in6p, m, &opts);
375         m_adj(m, off + sizeof(struct udphdr));
376         if (sbappendaddr(&in6p->in6p_socket->so_rcv,
377                         (struct sockaddr *)&fromsa, m, opts) == 0) {
378                 udpstat.udps_fullsock++;
379                 goto bad;
380         }
381         sorwakeup(in6p->in6p_socket);
382         return IPPROTO_DONE;
383 bad:
384         if (m)
385                 m_freem(m);
386         if (opts)
387                 m_freem(opts);
388         return IPPROTO_DONE;
389 }
390
391 void
392 udp6_ctlinput(cmd, sa, d)
393         int cmd;
394         struct sockaddr *sa;
395         void *d;
396 {
397         struct udphdr uh;
398         struct ip6_hdr *ip6;
399         struct mbuf *m;
400         int off = 0;
401         struct ip6ctlparam *ip6cp = NULL;
402         const struct sockaddr_in6 *sa6_src = NULL;
403         void *cmdarg;
404         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
405         struct udp_portonly {
406                 u_int16_t uh_sport;
407                 u_int16_t uh_dport;
408         } *uhp;
409
410         if (sa->sa_family != AF_INET6 ||
411             sa->sa_len != sizeof(struct sockaddr_in6))
412                 return;
413
414         if ((unsigned)cmd >= PRC_NCMDS)
415                 return;
416         if (PRC_IS_REDIRECT(cmd))
417                 notify = in6_rtchange, d = NULL;
418         else if (cmd == PRC_HOSTDEAD)
419                 d = NULL;
420         else if (inet6ctlerrmap[cmd] == 0)
421                 return;
422
423         /* if the parameter is from icmp6, decode it. */
424         if (d != NULL) {
425                 ip6cp = (struct ip6ctlparam *)d;
426                 m = ip6cp->ip6c_m;
427                 ip6 = ip6cp->ip6c_ip6;
428                 off = ip6cp->ip6c_off;
429                 cmdarg = ip6cp->ip6c_cmdarg;
430                 sa6_src = ip6cp->ip6c_src;
431         } else {
432                 m = NULL;
433                 ip6 = NULL;
434                 cmdarg = NULL;
435                 sa6_src = &sa6_any;
436         }
437
438         if (ip6) {
439                 /*
440                  * XXX: We assume that when IPV6 is non NULL,
441                  * M and OFF are valid.
442                  */
443
444                 /* check if we can safely examine src and dst ports */
445                 if (m->m_pkthdr.len < off + sizeof(*uhp))
446                         return;
447
448                 bzero(&uh, sizeof(uh));
449                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
450
451                 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
452                                      (struct sockaddr *)ip6cp->ip6c_src,
453                                      uh.uh_sport, cmd, cmdarg, notify);
454         } else
455                 (void) in6_pcbnotify(&udbinfo, sa, 0,
456                                      (const struct sockaddr *)sa6_src,
457                                      0, cmd, cmdarg, notify);
458 }
459
460 static int
461 udp6_getcred(SYSCTL_HANDLER_ARGS)
462 {
463         struct xucred xuc;
464         struct sockaddr_in6 addrs[2];
465         struct inpcb *inp;
466         int error, s;
467
468         error = suser(req->td);
469         if (error)
470                 return (error);
471
472         if (req->newlen != sizeof(addrs))
473                 return (EINVAL);
474         if (req->oldlen != sizeof(struct xucred))
475                 return (EINVAL);
476         error = SYSCTL_IN(req, addrs, sizeof(addrs));
477         if (error)
478                 return (error);
479         if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
480             (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
481                 return (error);
482         }
483         s = splnet();
484         inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
485                                  addrs[1].sin6_port,
486                                  &addrs[0].sin6_addr, addrs[0].sin6_port,
487                                  1, NULL);
488         if (!inp || !inp->inp_socket) {
489                 error = ENOENT;
490                 goto out;
491         }
492         cru2x(inp->inp_socket->so_cred, &xuc);
493         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
494 out:
495         splx(s);
496         return (error);
497 }
498
499 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
500             0, 0,
501             udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
502
503 static int
504 udp6_abort(struct socket *so)
505 {
506         struct inpcb *inp;
507         int s;
508
509         INP_INFO_WLOCK(&udbinfo);
510         inp = sotoinpcb(so);
511         if (inp == 0) {
512                 INP_INFO_WUNLOCK(&udbinfo);
513                 return EINVAL;  /* ??? possible? panic instead? */
514         }
515         soisdisconnected(so);
516         s = splnet();
517         INP_LOCK(inp);
518         in6_pcbdetach(inp);
519         INP_INFO_WUNLOCK(&udbinfo);
520         splx(s);
521         return 0;
522 }
523
524 static int
525 udp6_attach(struct socket *so, int proto, struct thread *td)
526 {
527         struct inpcb *inp;
528         int s, error;
529
530         INP_INFO_WLOCK(&udbinfo);
531         inp = sotoinpcb(so);
532         if (inp != 0) {
533                 INP_INFO_WUNLOCK(&udbinfo);
534                 return EINVAL;
535         }
536
537         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
538                 error = soreserve(so, udp_sendspace, udp_recvspace);
539                 if (error) {
540                         INP_INFO_WUNLOCK(&udbinfo);
541                         return error;
542                 }
543         }
544         s = splnet();
545         error = in_pcballoc(so, &udbinfo);
546         splx(s);
547         if (error) {
548                 INP_INFO_WUNLOCK(&udbinfo);
549                 return error;
550         }
551         inp = (struct inpcb *)so->so_pcb;
552         INP_INFO_WUNLOCK(&udbinfo);
553         inp->inp_vflag |= INP_IPV6;
554         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
555                 inp->inp_vflag |= INP_IPV4;
556         inp->in6p_hops = -1;    /* use kernel default */
557         inp->in6p_cksum = -1;   /* just to be sure */
558         /*
559          * XXX: ugly!!
560          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
561          * because the socket may be bound to an IPv6 wildcard address,
562          * which may match an IPv4-mapped IPv6 address.
563          */
564         inp->inp_ip_ttl = ip_defttl;
565         INP_UNLOCK(inp);
566         return 0;
567 }
568
569 static int
570 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
571 {
572         struct inpcb *inp;
573         int s, error;
574
575         INP_INFO_WLOCK(&udbinfo);
576         inp = sotoinpcb(so);
577         if (inp == 0) {
578                 INP_INFO_WUNLOCK(&udbinfo);
579                 return EINVAL;
580         }
581         INP_LOCK(inp);
582
583         inp->inp_vflag &= ~INP_IPV4;
584         inp->inp_vflag |= INP_IPV6;
585         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
586                 struct sockaddr_in6 *sin6_p;
587
588                 sin6_p = (struct sockaddr_in6 *)nam;
589
590                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
591                         inp->inp_vflag |= INP_IPV4;
592                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
593                         struct sockaddr_in sin;
594
595                         in6_sin6_2_sin(&sin, sin6_p);
596                         inp->inp_vflag |= INP_IPV4;
597                         inp->inp_vflag &= ~INP_IPV6;
598                         s = splnet();
599                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
600                             td->td_ucred);
601                         goto out;
602                 }
603         }
604
605         s = splnet();
606         error = in6_pcbbind(inp, nam, td->td_ucred);
607 out:
608         INP_UNLOCK(inp);
609         INP_INFO_WUNLOCK(&udbinfo);
610         splx(s);
611         return error;
612 }
613
614 static int
615 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
616 {
617         struct inpcb *inp;
618         int s, error;
619
620         INP_INFO_WLOCK(&udbinfo);
621         inp = sotoinpcb(so);
622         if (inp == 0) {
623                 INP_INFO_WUNLOCK(&udbinfo);
624                 return EINVAL;
625         }
626         INP_LOCK(inp);
627
628         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
629                 struct sockaddr_in6 *sin6_p;
630
631                 sin6_p = (struct sockaddr_in6 *)nam;
632                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
633                         struct sockaddr_in sin;
634
635                         if (inp->inp_faddr.s_addr != INADDR_ANY)
636                                 return EISCONN;
637                         in6_sin6_2_sin(&sin, sin6_p);
638                         s = splnet();
639                         error = in_pcbconnect(inp, (struct sockaddr *)&sin,
640                             td->td_ucred);
641                         splx(s);
642                         if (error == 0) {
643                                 inp->inp_vflag |= INP_IPV4;
644                                 inp->inp_vflag &= ~INP_IPV6;
645                                 soisconnected(so);
646                         }
647                         goto out;
648                 }
649         }
650         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
651                 error = EISCONN;
652                 goto out;
653         }
654         s = splnet();
655         error = in6_pcbconnect(inp, nam, td->td_ucred);
656         splx(s);
657         if (error == 0) {
658                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
659                         /* should be non mapped addr */
660                         inp->inp_vflag &= ~INP_IPV4;
661                         inp->inp_vflag |= INP_IPV6;
662                 }
663                 soisconnected(so);
664         }
665 out:
666         INP_UNLOCK(inp);
667         INP_INFO_WUNLOCK(&udbinfo);
668         return error;
669 }
670
671 static int
672 udp6_detach(struct socket *so)
673 {
674         struct inpcb *inp;
675         int s;
676
677         INP_INFO_WLOCK(&udbinfo);
678         inp = sotoinpcb(so);
679         if (inp == 0) {
680                 INP_INFO_WUNLOCK(&udbinfo);
681                 return EINVAL;
682         }
683         INP_LOCK(inp);
684         s = splnet();
685         in6_pcbdetach(inp);
686         splx(s);
687         INP_INFO_WUNLOCK(&udbinfo);
688         return 0;
689 }
690
691 static int
692 udp6_disconnect(struct socket *so)
693 {
694         struct inpcb *inp;
695         int error, s;
696
697         INP_INFO_WLOCK(&udbinfo);
698         inp = sotoinpcb(so);
699         if (inp == 0) {
700                 INP_INFO_WUNLOCK(&udbinfo);
701                 return EINVAL;
702         }
703         INP_LOCK(inp);
704
705 #ifdef INET
706         if (inp->inp_vflag & INP_IPV4) {
707                 struct pr_usrreqs *pru;
708
709                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
710                 error = (*pru->pru_disconnect)(so);
711                 goto out;
712         }
713 #endif
714
715         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
716                 error = ENOTCONN;
717                 goto out;
718         }
719
720         s = splnet();
721         in6_pcbdisconnect(inp);
722         inp->in6p_laddr = in6addr_any;
723         splx(s);
724         /* XXXRW: so_state locking? */
725         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
726 out:
727         INP_UNLOCK(inp);
728         INP_INFO_WUNLOCK(&udbinfo);
729         return 0;
730 }
731
732 static int
733 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
734           struct mbuf *control, struct thread *td)
735 {
736         struct inpcb *inp;
737         int error = 0;
738
739         INP_INFO_WLOCK(&udbinfo);
740         inp = sotoinpcb(so);
741         if (inp == 0) {
742                 INP_INFO_WUNLOCK(&udbinfo);
743                 m_freem(m);
744                 return EINVAL;
745         }
746         INP_LOCK(inp);
747
748         if (addr) {
749                 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 
750                         error = EINVAL;
751                         goto bad;
752                 }
753                 if (addr->sa_family != AF_INET6) {
754                         error = EAFNOSUPPORT;
755                         goto bad;
756                 }
757         }
758
759 #ifdef INET
760         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
761                 int hasv4addr;
762                 struct sockaddr_in6 *sin6 = 0;
763
764                 if (addr == 0)
765                         hasv4addr = (inp->inp_vflag & INP_IPV4);
766                 else {
767                         sin6 = (struct sockaddr_in6 *)addr;
768                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
769                                 ? 1 : 0;
770                 }
771                 if (hasv4addr) {
772                         struct pr_usrreqs *pru;
773
774                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
775                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
776                                 /*
777                                  * when remote addr is IPv4-mapped
778                                  * address, local addr should not be
779                                  * an IPv6 address; since you cannot
780                                  * determine how to map IPv6 source
781                                  * address to IPv4.
782                                  */
783                                 error = EINVAL;
784                                 goto out;
785                         }
786                         if (sin6)
787                                 in6_sin6_2_sin_in_sock(addr);
788                         pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
789                         error = ((*pru->pru_send)(so, flags, m, addr, control,
790                                                   td));
791                         /* addr will just be freed in sendit(). */
792                         goto out;
793                 }
794         }
795 #endif
796
797         error = udp6_output(inp, m, addr, control, td);
798 out:
799         INP_UNLOCK(inp);
800         INP_INFO_WUNLOCK(&udbinfo);
801         return error;
802
803   bad:
804         INP_UNLOCK(inp);
805         INP_INFO_WUNLOCK(&udbinfo);
806         m_freem(m);
807         return (error);
808 }
809
810 struct pr_usrreqs udp6_usrreqs = {
811         .pru_abort =            udp6_abort,
812         .pru_attach =           udp6_attach,
813         .pru_bind =             udp6_bind,
814         .pru_connect =          udp6_connect,
815         .pru_control =          in6_control,
816         .pru_detach =           udp6_detach,
817         .pru_disconnect =       udp6_disconnect,
818         .pru_peeraddr =         in6_mapped_peeraddr,
819         .pru_send =             udp6_send,
820         .pru_shutdown =         udp_shutdown,
821         .pru_sockaddr =         in6_mapped_sockaddr,
822         .pru_sosetlabel =       in_pcbsosetlabel
823 };