]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/udp6_usrreq.c
This commit was generated by cvs2svn to compensate for changes in r54690,
[FreeBSD/FreeBSD.git] / 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
30 /*
31  * Copyright (c) 1982, 1986, 1989, 1993
32  *      The Regents of the University of California.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *      This product includes software developed by the University of
45  *      California, Berkeley and its contributors.
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_var.h   8.1 (Berkeley) 6/10/93
63  * $FreeBSD$
64  */
65
66 #include <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/errno.h>
75 #include <sys/stat.h>
76 #include <sys/systm.h>
77 #include <sys/syslog.h>
78 #include <sys/proc.h>
79
80 #include <net/if.h>
81 #include <net/route.h>
82 #include <net/if_types.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/udp.h>
91 #include <netinet/udp_var.h>
92 #include <netinet6/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/icmp6.h>
96 #include <netinet6/udp6_var.h>
97 #include <netinet6/ip6protosw.h>
98
99 #ifdef IPSEC
100 #include <netinet6/ipsec.h>
101 #endif /*IPSEC*/
102
103 #include "faith.h"
104
105 /*
106  * UDP protocol inplementation.
107  * Per RFC 768, August, 1980.
108  */
109
110 extern  struct protosw inetsw[];
111 static  int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
112 static  int udp6_detach __P((struct socket *so));
113
114 static int
115 in6_mcmatch(in6p, ia6, ifp)
116         struct inpcb *in6p;
117         register struct in6_addr *ia6;
118         struct ifnet *ifp;
119 {
120         struct ip6_moptions *im6o = in6p->in6p_moptions;
121         struct in6_multi_mship *imm;
122
123         if (im6o == NULL)
124                 return 0;
125
126         for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
127              imm = imm->i6mm_chain.le_next) {
128                 if ((ifp == NULL ||
129                      imm->i6mm_maddr->in6m_ifp == ifp) &&
130                     IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
131                                        ia6))
132                         return 1;
133         }
134         return 0;
135 }
136
137 int
138 udp6_input(mp, offp, proto)
139         struct mbuf **mp;
140         int *offp, proto;
141 {
142         struct mbuf *m = *mp;
143         register struct ip6_hdr *ip6;
144         register struct udphdr *uh;
145         register struct inpcb *in6p;
146         struct  mbuf *opts = 0;
147         int off = *offp;
148         int plen, ulen;
149         struct sockaddr_in6 udp_in6;
150
151 #if defined(NFAITH) && 0 < NFAITH
152         if (m->m_pkthdr.rcvif) {
153                 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
154                         /* XXX send icmp6 host/port unreach? */
155                         m_freem(m);
156                         return IPPROTO_DONE;
157                 }
158         }
159 #endif
160         udpstat.udps_ipackets++;
161
162         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
163
164         ip6 = mtod(m, struct ip6_hdr *);
165         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
166         uh = (struct udphdr *)((caddr_t)ip6 + off);
167         ulen = ntohs((u_short)uh->uh_ulen);
168
169         if (plen != ulen) {
170                 udpstat.udps_badlen++;
171                 goto bad;
172         }
173
174         /*
175          * Checksum extended UDP header and data.
176          */
177         if (uh->uh_sum == 0)
178                 udpstat.udps_nosum++;
179         else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
180                 udpstat.udps_badsum++;
181                 goto bad;
182         }
183
184         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
185                 struct  inpcb *last;
186
187                 /*
188                  * Deliver a multicast datagram to all sockets
189                  * for which the local and remote addresses and ports match
190                  * those of the incoming datagram.  This allows more than
191                  * one process to receive multicasts on the same port.
192                  * (This really ought to be done for unicast datagrams as
193                  * well, but that would cause problems with existing
194                  * applications that open both address-specific sockets and
195                  * a wildcard socket listening to the same port -- they would
196                  * end up receiving duplicates of every unicast datagram.
197                  * Those applications open the multiple sockets to overcome an
198                  * inadequacy of the UDP socket interface, but for backwards
199                  * compatibility we avoid the problem here rather than
200                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
201                  */
202
203                 /*
204                  * In a case that laddr should be set to the link-local
205                  * address (this happens in RIPng), the multicast address
206                  * specified in the received packet does not match with
207                  * laddr. To cure this situation, the matching is relaxed
208                  * if the receiving interface is the same as one specified
209                  * in the socket and if the destination multicast address
210                  * matches one of the multicast groups specified in the socket.
211                  */
212
213                 /*
214                  * Construct sockaddr format source address.
215                  */
216                 init_sin6(&udp_in6, m); /* general init */
217                 udp_in6.sin6_port = uh->uh_sport;
218                 /*
219                  * KAME note: usually we drop udphdr from mbuf here.
220                  * We need udphdr for IPsec processing so we do that later.
221                  */
222
223                 /*
224                  * Locate pcb(s) for datagram.
225                  * (Algorithm copied from raw_intr().)
226                  */
227                 last = NULL;
228                 LIST_FOREACH(in6p, &udb, inp_list) {
229                         if ((in6p->inp_vflag & INP_IPV6) == NULL)
230                                 continue;
231                         if (in6p->in6p_lport != uh->uh_dport)
232                                 continue;
233                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
234                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
235                                                         &ip6->ip6_dst) &&
236                                     !in6_mcmatch(in6p, &ip6->ip6_dst,
237                                                  m->m_pkthdr.rcvif))
238                                         continue;
239                         }
240                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
241                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
242                                                         &ip6->ip6_src) ||
243                                    in6p->in6p_fport != uh->uh_sport)
244                                         continue;
245                         }
246
247                         if (last != NULL) {
248                                 struct  mbuf *n;
249
250 #ifdef IPSEC
251                                 /*
252                                  * Check AH/ESP integrity.
253                                  */
254                                 if (last != NULL &&
255                                     ipsec6_in_reject_so(m, last->inp_socket)) {
256                                         ipsec6stat.in_polvio++;
257                                         /* do not inject data into pcb */
258                                 } else
259 #endif /*IPSEC*/
260                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
261                                         /*
262                                          * KAME NOTE: do not
263                                          * m_copy(m, offset, ...) above.
264                                          * sbappendaddr() expects M_PKTHDR,
265                                          * and m_copy() will copy M_PKTHDR
266                                          * only if offset is 0.
267                                          */
268                                         if (last->in6p_flags & IN6P_CONTROLOPTS
269                                             || last->in6p_socket->so_options & SO_TIMESTAMP)
270                                                 ip6_savecontrol(last, &opts,
271                                                                 ip6, n);
272                                         m_adj(n, off + sizeof(struct udphdr));
273                                         if (sbappendaddr(&last->in6p_socket->so_rcv,
274                                                         (struct sockaddr *)&udp_in6,
275                                                         n, opts) == 0) {
276                                                 m_freem(n);
277                                                 if (opts)
278                                                         m_freem(opts);
279                                                 udpstat.udps_fullsock++;
280                                         } else
281                                                 sorwakeup(last->in6p_socket);
282                                         opts = 0;
283                                 }
284                         }
285                         last = in6p;
286                         /*
287                          * Don't look for additional matches if this one does
288                          * not have either the SO_REUSEPORT or SO_REUSEADDR
289                          * socket options set.  This heuristic avoids searching
290                          * through all pcbs in the common case of a non-shared
291                          * port.  It assumes that an application will never
292                          * clear these options after setting them.
293                          */
294                         if ((last->in6p_socket->so_options &
295                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
296                                 break;
297                 }
298
299                 if (last == NULL) {
300                         /*
301                          * No matching pcb found; discard datagram.
302                          * (No need to send an ICMP Port Unreachable
303                          * for a broadcast or multicast datgram.)
304                          */
305                         udpstat.udps_noport++;
306                         udpstat.udps_noportmcast++;
307                         goto bad;
308                 }
309 #ifdef IPSEC
310                 /*
311                  * Check AH/ESP integrity.
312                  */
313                 if (last != NULL && ipsec6_in_reject_so(m, last->inp_socket)) {
314                         ipsec6stat.in_polvio++;
315                         goto bad;
316                 }
317 #endif /*IPSEC*/
318                 if (last->in6p_flags & IN6P_CONTROLOPTS
319                     || last->in6p_socket->so_options & SO_TIMESTAMP)
320                         ip6_savecontrol(last, &opts, ip6, m);
321
322                 m_adj(m, off + sizeof(struct udphdr));
323                 if (sbappendaddr(&last->in6p_socket->so_rcv,
324                                 (struct sockaddr *)&udp_in6,
325                                 m, opts) == 0) {
326                         udpstat.udps_fullsock++;
327                         goto bad;
328                 }
329                 sorwakeup(last->in6p_socket);
330                 return IPPROTO_DONE;
331         }
332         /*
333          * Locate pcb for datagram.
334          */
335         in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
336                                   &ip6->ip6_dst, uh->uh_dport, 1,
337                                   m->m_pkthdr.rcvif);
338         if (in6p == 0) {
339                 if (log_in_vain) {
340                         char buf[INET6_ADDRSTRLEN];
341
342                         strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
343                         log(LOG_INFO,
344                             "Connection attempt to UDP %s:%d from %s:%d\n",
345                             buf, ntohs(uh->uh_dport),
346                             ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
347                 }
348                 udpstat.udps_noport++;
349                 if (m->m_flags & M_MCAST) {
350                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
351                         udpstat.udps_noportmcast++;
352                         goto bad;
353                 }
354                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
355                 return IPPROTO_DONE;
356         }
357 #ifdef IPSEC
358         /*
359          * Check AH/ESP integrity.
360          */
361         if (in6p != NULL && ipsec6_in_reject_so(m, in6p->in6p_socket)) {
362                 ipsec6stat.in_polvio++;
363                 goto bad;
364         }
365 #endif /*IPSEC*/
366
367         /*
368          * Construct sockaddr format source address.
369          * Stuff source address and datagram in user buffer.
370          */
371         init_sin6(&udp_in6, m); /* general init */
372         udp_in6.sin6_port = uh->uh_sport;
373         if (in6p->in6p_flags & IN6P_CONTROLOPTS
374             || in6p->in6p_socket->so_options & SO_TIMESTAMP)
375                 ip6_savecontrol(in6p, &opts, ip6, m);
376         m_adj(m, off + sizeof(struct udphdr));
377         if (sbappendaddr(&in6p->in6p_socket->so_rcv,
378                         (struct sockaddr *)&udp_in6,
379                         m, opts) == 0) {
380                 udpstat.udps_fullsock++;
381                 goto bad;
382         }
383         sorwakeup(in6p->in6p_socket);
384         return IPPROTO_DONE;
385 bad:
386         if (m)
387                 m_freem(m);
388         if (opts)
389                 m_freem(opts);
390         return IPPROTO_DONE;
391 }
392
393 void
394 udp6_ctlinput(cmd, sa, d)
395         int cmd;
396         struct sockaddr *sa;
397         void *d;
398 {
399         register struct udphdr *uhp;
400         struct udphdr uh;
401         struct sockaddr_in6 sa6;
402         struct ip6_hdr *ip6;
403         struct mbuf *m;
404         int off;
405
406         if (sa->sa_family != AF_INET6 ||
407             sa->sa_len != sizeof(struct sockaddr_in6))
408                 return;
409
410         if (!PRC_IS_REDIRECT(cmd) &&
411             ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
412                 return;
413
414         /* if the parameter is from icmp6, decode it. */
415         if (d != NULL) {
416                 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
417                 m = ip6cp->ip6c_m;
418                 ip6 = ip6cp->ip6c_ip6;
419                 off = ip6cp->ip6c_off;
420         } else {
421                 m = NULL;
422                 ip6 = NULL;
423         }
424
425         /* translate addresses into internal form */
426         sa6 = *(struct sockaddr_in6 *)sa;
427         if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
428                 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
429
430         if (ip6) {
431                 /*
432                  * XXX: We assume that when IPV6 is non NULL,
433                  * M and OFF are valid.
434                  */
435                 struct in6_addr s;
436
437                 /* translate addresses into internal form */
438                 memcpy(&s, &ip6->ip6_src, sizeof(s));
439                 if (IN6_IS_ADDR_LINKLOCAL(&s))
440                         s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
441
442                 if (m->m_len < off + sizeof(uh)) {
443                         /*
444                          * this should be rare case,
445                          * so we compromise on this copy...
446                          */
447                         m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
448                         uhp = &uh;
449                 } else
450                         uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
451                 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
452                                      uhp->uh_dport, &s,
453                                      uhp->uh_sport, cmd, udp_notify);
454         } else
455                 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
456                                      &zeroin6_addr, 0, cmd, udp_notify);
457 }
458
459 static int
460 udp6_getcred SYSCTL_HANDLER_ARGS
461 {
462         struct sockaddr_in6 addrs[2];
463         struct inpcb *inp;
464         int error, s;
465
466         error = suser(req->p);
467         if (error)
468                 return (error);
469         error = SYSCTL_IN(req, addrs, sizeof(addrs));
470         if (error)
471                 return (error);
472         s = splnet();
473         inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
474                                  addrs[1].sin6_port,
475                                  &addrs[0].sin6_addr, addrs[0].sin6_port,
476                                  1, NULL);
477         if (!inp || !inp->inp_socket || !inp->inp_socket->so_cred) {
478                 error = ENOENT;
479                 goto out;
480         }
481         error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
482                            sizeof(struct ucred));
483
484 out:
485         splx(s);
486         return (error);
487 }
488
489 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
490             0, 0,
491             udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
492
493 int
494 udp6_output(in6p, m, addr6, control, p)
495         register struct inpcb *in6p;
496         register struct mbuf *m;
497         struct sockaddr *addr6;
498         struct mbuf *control;
499         struct proc *p;
500 {
501         register int ulen = m->m_pkthdr.len;
502         int plen = sizeof(struct udphdr) + ulen;
503         struct ip6_hdr *ip6;
504         struct udphdr *udp6;
505         struct  in6_addr laddr6;
506         int s = 0, error = 0;
507         struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
508
509         if (control) {
510                 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
511                         goto release;
512                 in6p->in6p_outputopts = &opt;
513         }
514
515         if (addr6) {
516                 laddr6 = in6p->in6p_laddr;
517                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
518                         error = EISCONN;
519                         goto release;
520                 }
521                 /*
522                  * Must block input while temporarily connected.
523                  */
524                 s = splnet();
525                 /*
526                  * XXX: the user might want to overwrite the local address
527                  * via an ancillary data.
528                  */
529                 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
530                 error = in6_pcbconnect(in6p, addr6, p);
531                 if (error) {
532                         splx(s);
533                         goto release;
534                 }
535         } else {
536                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
537                         error = ENOTCONN;
538                         goto release;
539                 }
540         }
541         /*
542          * Calculate data length and get a mbuf
543          * for UDP and IP6 headers.
544          */
545         M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
546                   M_DONTWAIT);
547         if (m == 0) {
548                 error = ENOBUFS;
549                 if (addr6)
550                         splx(s);
551                 goto release;
552         }
553
554         /*
555          * Stuff checksum and output datagram.
556          */
557         ip6 = mtod(m, struct ip6_hdr *);
558         ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
559         ip6->ip6_vfc    = IPV6_VERSION;
560         /* ip6_plen will be filled in ip6_output. */
561         ip6->ip6_nxt    = IPPROTO_UDP;
562         ip6->ip6_hlim   = in6_selecthlim(in6p,
563                                          in6p->in6p_route.ro_rt ?
564                                          in6p->in6p_route.ro_rt->rt_ifp :
565                                          NULL);
566         ip6->ip6_src    = in6p->in6p_laddr;
567         ip6->ip6_dst    = in6p->in6p_faddr;
568
569         udp6 = (struct udphdr *)(ip6 + 1);
570         udp6->uh_sport = in6p->in6p_lport;
571         udp6->uh_dport = in6p->in6p_fport;
572         udp6->uh_ulen  = htons((u_short)plen);
573         udp6->uh_sum   = 0;
574
575         if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
576                                         sizeof(struct ip6_hdr), plen)) == 0) {
577                 udp6->uh_sum = 0xffff;
578         }
579
580         udpstat.udps_opackets++;
581
582 #ifdef IPSEC
583         m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
584 #endif /*IPSEC*/
585         error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
586                             0, in6p->in6p_moptions, NULL);
587
588         if (addr6) {
589                 in6_pcbdisconnect(in6p);
590                 in6p->in6p_laddr = laddr6;
591                 splx(s);
592         }
593         goto releaseopt;
594
595 release:
596         m_freem(m);
597
598 releaseopt:
599         if (control) {
600                 in6p->in6p_outputopts = stickyopt;
601                 m_freem(control);
602         }
603         return(error);
604 }
605
606 static int
607 udp6_abort(struct socket *so)
608 {
609         struct inpcb *inp;
610         int s;
611
612         inp = sotoinpcb(so);
613         if (inp == 0)
614                 return EINVAL;  /* ??? possible? panic instead? */
615         soisdisconnected(so);
616         s = splnet();
617         in6_pcbdetach(inp);
618         splx(s);
619         return 0;
620 }
621
622 static int
623 udp6_attach(struct socket *so, int proto, struct proc *p)
624 {
625         struct inpcb *inp;
626         int s, error;
627
628         inp = sotoinpcb(so);
629         if (inp != 0)
630                 return EINVAL;
631
632         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
633                 error = soreserve(so, udp_sendspace, udp_recvspace);
634                 if (error)
635                         return error;
636         }
637         s = splnet();
638         error = in_pcballoc(so, &udbinfo, p);
639         splx(s);
640         if (error)
641                 return error;
642         inp = (struct inpcb *)so->so_pcb;
643         inp->inp_vflag |= INP_IPV6;
644         inp->in6p_hops = -1;    /* use kernel default */
645         inp->in6p_cksum = -1;   /* just to be sure */
646 #ifdef IPSEC
647         error = ipsec_init_policy(so, &inp->in6p_sp);
648         if (error != 0) {
649                 in6_pcbdetach(inp);
650                 return (error);
651         }
652 #endif /*IPSEC*/
653         return 0;
654 }
655
656 static int
657 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
658 {
659         struct inpcb *inp;
660         int s, error;
661
662         inp = sotoinpcb(so);
663         if (inp == 0)
664                 return EINVAL;
665
666         inp->inp_vflag &= ~INP_IPV4;
667         inp->inp_vflag |= INP_IPV6;
668         if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) {
669                 struct sockaddr_in6 *sin6_p;
670
671                 sin6_p = (struct sockaddr_in6 *)nam;
672
673                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
674                         inp->inp_vflag |= INP_IPV4;
675                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
676                         struct sockaddr_in sin;
677
678                         in6_sin6_2_sin(&sin, sin6_p);
679                         inp->inp_vflag |= INP_IPV4;
680                         inp->inp_vflag &= ~INP_IPV6;
681                         s = splnet();
682                         error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
683                         splx(s);
684                         return error;
685                 }
686         }
687
688         s = splnet();
689         error = in6_pcbbind(inp, nam, p);
690         splx(s);
691         return error;
692 }
693
694 static int
695 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
696 {
697         struct inpcb *inp;
698         int s, error;
699
700         inp = sotoinpcb(so);
701         if (inp == 0)
702                 return EINVAL;
703
704         if (ip6_mapped_addr_on) {
705                 struct sockaddr_in6 *sin6_p;
706
707                 sin6_p = (struct sockaddr_in6 *)nam;
708                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
709                         struct sockaddr_in sin;
710
711                         if (inp->inp_faddr.s_addr != INADDR_ANY)
712                                 return EISCONN;
713                         in6_sin6_2_sin(&sin, sin6_p);
714                         s = splnet();
715                         error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
716                         splx(s);
717                         if (error == NULL) {
718                                 inp->inp_vflag |= INP_IPV4;
719                                 inp->inp_vflag &= ~INP_IPV6;
720                                 soisconnected(so);
721                         }
722                         return error;
723                 }
724         }
725
726         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
727                 return EISCONN;
728         s = splnet();
729         error = in6_pcbconnect(inp, nam, p);
730         if (ip6_auto_flowlabel) {
731                 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
732                 inp->in6p_flowinfo |=
733                         (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
734         }
735         splx(s);
736         if (error == NULL) {
737                 if (ip6_mapped_addr_on) { /* should be non mapped addr */
738                         inp->inp_vflag &= ~INP_IPV4;
739                         inp->inp_vflag |= INP_IPV6;
740                 }
741                 soisconnected(so);
742         }
743         return error;
744 }
745
746 static int
747 udp6_detach(struct socket *so)
748 {
749         struct inpcb *inp;
750         int s;
751
752         inp = sotoinpcb(so);
753         if (inp == 0)
754                 return EINVAL;
755         s = splnet();
756         in6_pcbdetach(inp);
757         splx(s);
758         return 0;
759 }
760
761 static int
762 udp6_disconnect(struct socket *so)
763 {
764         struct inpcb *inp;
765         int s;
766
767         inp = sotoinpcb(so);
768         if (inp == 0)
769                 return EINVAL;
770
771         if (inp->inp_vflag & INP_IPV4) {
772                 struct pr_usrreqs *pru;
773
774                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
775                 return ((*pru->pru_disconnect)(so));
776         }
777
778         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
779                 return ENOTCONN;
780
781         s = splnet();
782         in6_pcbdisconnect(inp);
783         inp->in6p_laddr = in6addr_any;
784         splx(s);
785         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
786         return 0;
787 }
788
789 static int
790 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
791           struct mbuf *control, struct proc *p)
792 {
793         struct inpcb *inp;
794
795         inp = sotoinpcb(so);
796         if (inp == 0) {
797                 m_freem(m);
798                 return EINVAL;
799         }
800
801         if (ip6_mapped_addr_on) {
802                 int hasv4addr;
803                 struct sockaddr_in6 *sin6 = 0;
804
805                 if (addr == 0)
806                         hasv4addr = (inp->inp_vflag & INP_IPV4);
807                 else {
808                         sin6 = (struct sockaddr_in6 *)addr;
809                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
810                                 ? 1 : 0;
811                 }
812                 if (hasv4addr) {
813                         struct pr_usrreqs *pru;
814                         int error;
815
816                         if (sin6)
817                                 in6_sin6_2_sin_in_sock(addr);
818                         pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
819                         error = ((*pru->pru_send)(so, flags, m, addr, control,
820                                                   p));
821                         /* addr will just be freed in sendit(). */
822                         return error;
823                 }
824         }
825
826         return udp6_output(inp, m, addr, control, p);
827 }
828
829 struct pr_usrreqs udp6_usrreqs = {
830         udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
831         pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
832         pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
833         pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
834         in6_mapped_sockaddr, sosend, soreceive, sopoll
835 };