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