]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/netinet6/udp6_usrreq.c
Initial revision
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / netinet6 / udp6_usrreq.c
1 //==========================================================================
2 //
3 //      src/sys/netinet6/udp6_usrreq.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*      $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $        */
23 /*      $KAME: udp6_usrreq.c,v 1.43 2001/11/12 05:04:16 jinmei Exp $    */
24
25 /*
26  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  *    notice, this list of conditions and the following disclaimer in the
36  *    documentation and/or other materials provided with the distribution.
37  * 3. Neither the name of the project nor the names of its contributors
38  *    may be used to endorse or promote products derived from this software
39  *    without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  */
53
54 /*
55  * Copyright (c) 1982, 1986, 1989, 1993
56  *      The Regents of the University of California.  All rights reserved.
57  *
58  * Redistribution and use in source and binary forms, with or without
59  * modification, are permitted provided that the following conditions
60  * are met:
61  * 1. Redistributions of source code must retain the above copyright
62  *    notice, this list of conditions and the following disclaimer.
63  * 2. Redistributions in binary form must reproduce the above copyright
64  *    notice, this list of conditions and the following disclaimer in the
65  *    documentation and/or other materials provided with the distribution.
66  * 3. All advertising materials mentioning features or use of this software
67  *    must display the following acknowledgement:
68  *      This product includes software developed by the University of
69  *      California, Berkeley and its contributors.
70  * 4. Neither the name of the University nor the names of its contributors
71  *    may be used to endorse or promote products derived from this software
72  *    without specific prior written permission.
73  *
74  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84  * SUCH DAMAGE.
85  *
86  *      @(#)udp_var.h   8.1 (Berkeley) 6/10/93
87  */
88
89 #include <sys/param.h>
90 #include <sys/mbuf.h>
91 #include <sys/protosw.h>
92 #include <sys/socket.h>
93 #include <sys/socketvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/errno.h>
96
97 #include <net/if.h>
98 #include <net/route.h>
99 #include <net/if_types.h>
100
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/ip.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
107 #include <netinet/udp.h>
108 #include <netinet/udp_var.h>
109 #include <netinet/ip6.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/in6_pcb.h>
112 #include <netinet/icmp6.h>
113 #include <netinet6/udp6_var.h>
114 #include <netinet6/ip6protosw.h>
115
116 #ifdef IPSEC
117 #include <netinet6/ipsec.h>
118 #endif /* IPSEC */
119
120 /*
121  * UDP protocol inplementation.
122  * Per RFC 768, August, 1980.
123  */
124
125 extern  struct protosw inetsw[];
126 static  int udp6_detach __P((struct socket *so));
127
128 int
129 udp6_input(mp, offp, proto)
130         struct mbuf **mp;
131         int *offp, proto;
132 {
133         struct mbuf *m = *mp;
134         register struct ip6_hdr *ip6;
135         register struct udphdr *uh;
136         register struct inpcb *in6p;
137         struct ip6_recvpktopts opts;
138         int off = *offp;
139         int plen, ulen;
140         struct sockaddr_in6 udp_in6;
141
142         bzero(&opts, sizeof(opts));
143
144         ip6 = mtod(m, struct ip6_hdr *);
145
146 #if defined(NFAITH) && 0 < NFAITH
147         if (faithprefix(&ip6->ip6_dst)) {
148                 /* XXX send icmp6 host/port unreach? */
149                 m_freem(m);
150                 return IPPROTO_DONE;
151         }
152 #endif
153
154 #ifndef PULLDOWN_TEST
155         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
156         ip6 = mtod(m, struct ip6_hdr *);
157         uh = (struct udphdr *)((caddr_t)ip6 + off);
158 #else
159         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
160         if (!uh)
161                 return IPPROTO_DONE;
162 #endif
163
164         udpstat.udps_ipackets++;
165
166         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
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: traditionally we dropped udpiphdr 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) == 0)
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                                         continue;
237                         }
238                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
239                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
240                                                         &ip6->ip6_src) ||
241                                    in6p->in6p_fport != uh->uh_sport)
242                                         continue;
243                         }
244
245                         if (last != NULL) {
246                                 struct  mbuf *n;
247
248 #ifdef IPSEC
249                                 /*
250                                  * Check AH/ESP integrity.
251                                  */
252                                 if (ipsec6_in_reject_so(m, last->inp_socket))
253                                         ipsec6stat.in_polvio++;
254                                         /* do not inject data into pcb */
255                                 else
256 #endif /* IPSEC */
257                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
258                                         /*
259                                          * KAME NOTE: do not
260                                          * m_copy(m, offset, ...) above.
261                                          * sbappendaddr() expects M_PKTHDR,
262                                          * and m_copy() will copy M_PKTHDR
263                                          * only if offset is 0.
264                                          */
265                                         if (last->in6p_flags & IN6P_CONTROLOPTS
266                                             || last->in6p_socket->so_options & SO_TIMESTAMP)
267                                                 ip6_savecontrol(last, ip6, n,
268                                                                 &opts, NULL);
269
270                                         m_adj(n, off + sizeof(struct udphdr));
271                                         if (sbappendaddr(&last->in6p_socket->so_rcv,
272                                                         (struct sockaddr *)&udp_in6,
273                                                         n, opts.head) == 0) {
274                                                 m_freem(n);
275                                                 if (opts.head)
276                                                         m_freem(opts.head);
277                                                 udpstat.udps_fullsock++;
278                                         } else
279                                                 sorwakeup(last->in6p_socket);
280                                         bzero(&opts, sizeof(opts));
281                                 }
282                         }
283                         last = in6p;
284                         /*
285                          * Don't look for additional matches if this one does
286                          * not have either the SO_REUSEPORT or SO_REUSEADDR
287                          * socket options set.  This heuristic avoids searching
288                          * through all pcbs in the common case of a non-shared
289                          * port.  It assumes that an application will never
290                          * clear these options after setting them.
291                          */
292                         if ((last->in6p_socket->so_options &
293                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
294                                 break;
295                 }
296
297                 if (last == NULL) {
298                         /*
299                          * No matching pcb found; discard datagram.
300                          * (No need to send an ICMP Port Unreachable
301                          * for a broadcast or multicast datgram.)
302                          */
303                         udpstat.udps_noport++;
304                         udpstat.udps_noportmcast++;
305                         goto bad;
306                 }
307 #ifdef IPSEC
308                 /*
309                  * Check AH/ESP integrity.
310                  */
311                 if (ipsec6_in_reject_so(m, last->inp_socket)) {
312                         ipsec6stat.in_polvio++;
313                         goto bad;
314                 }
315 #endif /* IPSEC */
316                 if (last->in6p_flags & IN6P_CONTROLOPTS
317                     || last->in6p_socket->so_options & SO_TIMESTAMP)
318                         ip6_savecontrol(last, ip6, m, &opts, NULL);
319
320                 m_adj(m, off + sizeof(struct udphdr));
321                 if (sbappendaddr(&last->in6p_socket->so_rcv,
322                                 (struct sockaddr *)&udp_in6,
323                                 m, opts.head) == 0) {
324                         udpstat.udps_fullsock++;
325                         goto bad;
326                 }
327                 sorwakeup(last->in6p_socket);
328                 return IPPROTO_DONE;
329         }
330         /*
331          * Locate pcb for datagram.
332          */
333         in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
334                                   &ip6->ip6_dst, uh->uh_dport, 1,
335                                   m->m_pkthdr.rcvif);
336         if (in6p == 0) {
337                 if (log_in_vain) {
338                         char buf[INET6_ADDRSTRLEN];
339
340                         strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
341                         log(LOG_INFO,
342                             "Connection attempt to UDP %s:%d from %s:%d\n",
343                             buf, ntohs(uh->uh_dport),
344                             ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
345                 }
346                 udpstat.udps_noport++;
347                 if (m->m_flags & M_MCAST) {
348                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
349                         udpstat.udps_noportmcast++;
350                         goto bad;
351                 }
352                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
353                 return IPPROTO_DONE;
354         }
355 #ifdef IPSEC
356         /*
357          * Check AH/ESP integrity.
358          */
359         if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
360                 ipsec6stat.in_polvio++;
361                 goto bad;
362         }
363 #endif /* IPSEC */
364
365         /*
366          * Construct sockaddr format source address.
367          * Stuff source address and datagram in user buffer.
368          */
369         init_sin6(&udp_in6, m); /* general init */
370         udp_in6.sin6_port = uh->uh_sport;
371         if (in6p->in6p_flags & IN6P_CONTROLOPTS
372             || in6p->in6p_socket->so_options & SO_TIMESTAMP)
373                 ip6_savecontrol(in6p, ip6, m, &opts, NULL);
374         m_adj(m, off + sizeof(struct udphdr));
375         if (sbappendaddr(&in6p->in6p_socket->so_rcv,
376                         (struct sockaddr *)&udp_in6,
377                         m, opts.head) == 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.head)
387                 m_freem(opts.head);
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         void (*notify) __P((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(&udb, sa, uh.uh_dport, 
452                                      (struct sockaddr *)ip6cp->ip6c_src,
453                                      uh.uh_sport, cmd, cmdarg, notify);
454         } else
455                 (void) in6_pcbnotify(&udb, sa, 0, (struct sockaddr *)&sa6_src,
456                                      0, cmd, cmdarg, notify);
457 }
458
459 static int
460 udp6_abort(struct socket *so)
461 {
462         struct inpcb *inp;
463         int s;
464
465         inp = sotoinpcb(so);
466         if (inp == 0)
467                 return EINVAL;  /* ??? possible? panic instead? */
468         soisdisconnected(so);
469         s = splnet();
470         in6_pcbdetach(inp);
471         splx(s);
472         return 0;
473 }
474
475 static int
476 udp6_attach(struct socket *so, int proto, struct proc *p)
477 {
478         struct inpcb *inp;
479         int s, error;
480
481         inp = sotoinpcb(so);
482         if (inp != 0)
483                 return EINVAL;
484
485         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
486                 error = soreserve(so, udp_sendspace, udp_recvspace);
487                 if (error)
488                         return error;
489         }
490         s = splnet();
491         error = in_pcballoc(so, &udbinfo, p);
492         splx(s);
493         if (error)
494                 return error;
495         inp = (struct inpcb *)so->so_pcb;
496         inp->inp_vflag |= INP_IPV6;
497         inp->in6p_hops = -1;    /* use kernel default */
498         inp->in6p_cksum = -1;   /* just to be sure */
499         /*
500          * XXX: ugly!!
501          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
502          * because the socket may be bound to an IPv6 wildcard address,
503          * which may match an IPv4-mapped IPv6 address.
504          */
505         inp->inp_ip_ttl = ip_defttl;
506         return 0;
507 }
508
509 static int
510 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
511 {
512         struct inpcb *inp;
513         int s, error;
514
515         inp = sotoinpcb(so);
516         if (inp == 0)
517                 return EINVAL;
518
519         inp->inp_vflag &= ~INP_IPV4;
520         inp->inp_vflag |= INP_IPV6;
521         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
522                 struct sockaddr_in6 *sin6_p;
523
524                 sin6_p = (struct sockaddr_in6 *)nam;
525
526                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
527                         inp->inp_vflag |= INP_IPV4;
528                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
529                         struct sockaddr_in sin;
530
531                         in6_sin6_2_sin(&sin, sin6_p);
532                         inp->inp_vflag |= INP_IPV4;
533                         inp->inp_vflag &= ~INP_IPV6;
534                         s = splnet();
535                         error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
536                         splx(s);
537                         return error;
538                 }
539         }
540
541         s = splnet();
542         error = in6_pcbbind(inp, nam, p);
543         splx(s);
544         return error;
545 }
546
547 static int
548 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
549 {
550         struct inpcb *inp;
551         int s, error;
552
553         inp = sotoinpcb(so);
554         if (inp == 0)
555                 return EINVAL;
556
557         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
558                 struct sockaddr_in6 *sin6_p;
559
560                 sin6_p = (struct sockaddr_in6 *)nam;
561                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
562                         struct sockaddr_in sin;
563
564                         if (inp->inp_faddr.s_addr != INADDR_ANY)
565                                 return EISCONN;
566                         in6_sin6_2_sin(&sin, sin6_p);
567                         s = splnet();
568                         error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
569                         splx(s);
570                         if (error == 0) {
571                                 inp->inp_vflag |= INP_IPV4;
572                                 inp->inp_vflag &= ~INP_IPV6;
573                                 soisconnected(so);
574                         }
575                         return error;
576                 }
577         }
578         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
579                 return EISCONN;
580         s = splnet();
581         error = in6_pcbconnect(inp, nam, p);
582         splx(s);
583         if (error == 0) {
584                 if (ip6_mapped_addr_on) { /* should be non mapped addr */
585                         inp->inp_vflag &= ~INP_IPV4;
586                         inp->inp_vflag |= INP_IPV6;
587                 }
588                 soisconnected(so);
589         }
590         return error;
591 }
592
593 static int
594 udp6_detach(struct socket *so)
595 {
596         struct inpcb *inp;
597         int s;
598
599         inp = sotoinpcb(so);
600         if (inp == 0)
601                 return EINVAL;
602         s = splnet();
603         in6_pcbdetach(inp);
604         splx(s);
605         return 0;
606 }
607
608 static int
609 udp6_disconnect(struct socket *so)
610 {
611         struct inpcb *inp;
612         int s;
613
614         inp = sotoinpcb(so);
615         if (inp == 0)
616                 return EINVAL;
617
618 #ifdef INET
619         if (inp->inp_vflag & INP_IPV4) {
620                 struct pr_usrreqs *pru;
621
622                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
623                 return ((*pru->pru_disconnect)(so));
624         }
625 #endif
626
627         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
628                 return ENOTCONN;
629
630         s = splnet();
631         in6_pcbdisconnect(inp);
632         inp->in6p_laddr = in6addr_any;
633         splx(s);
634         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
635         return 0;
636 }
637
638 static int
639 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
640           struct mbuf *control, struct proc *p)
641 {
642         struct inpcb *inp;
643         int error = 0;
644
645         inp = sotoinpcb(so);
646         if (inp == 0) {
647                 error = EINVAL;
648                 goto bad;
649         }
650
651         if (addr) {
652                 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 
653                         error = EINVAL;
654                         goto bad;
655                 }
656                 if (addr->sa_family != AF_INET6) {
657                         error = EAFNOSUPPORT;
658                         goto bad;
659                 }
660         }
661
662 #ifdef INET
663         if (ip6_mapped_addr_on) {
664                 int hasv4addr;
665                 struct sockaddr_in6 *sin6 = 0;
666
667                 if (addr == 0)
668                         hasv4addr = (inp->inp_vflag & INP_IPV4);
669                 else {
670                         sin6 = (struct sockaddr_in6 *)addr;
671                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
672                                 ? 1 : 0;
673                 }
674                 if (hasv4addr) {
675                         struct pr_usrreqs *pru;
676
677                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
678                                 /* 
679                                  * since a user of this socket set the
680                                  * IPV6_V6ONLY flag, we discard this
681                                  * datagram destined to a v4 addr.
682                                  */
683                                 return EINVAL;
684                         }
685                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
686                             && !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
687                                 /*
688                                  * when remote addr is IPv4-mapped
689                                  * address, local addr should not be
690                                  * an IPv6 address; since you cannot
691                                  * determine how to map IPv6 source
692                                  * address to IPv4.
693                                  */
694                                 return EINVAL;
695                         }
696                         if (sin6)
697                                 in6_sin6_2_sin_in_sock(addr);
698                         pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
699                         error = ((*pru->pru_send)(so, flags, m, addr, control,
700                                                   p));
701                         /* addr will just be freed in sendit(). */
702                         return error;
703                 }
704         }
705 #endif
706
707         return udp6_output(inp, m, addr, control, p);
708
709   bad:
710         m_freem(m);
711         return(error);
712 }
713
714 struct pr_usrreqs udp6_usrreqs = {
715         udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
716         pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
717         pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
718         pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
719         in6_mapped_sockaddr, sosend, soreceive, sopoll
720 };
721