]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/udp6_usrreq.c
xnu-1699.22.81.tar.gz
[apple/xnu.git] / bsd / netinet6 / udp6_usrreq.c
1 /*
2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $ */
30 /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */
31
32 /*
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * Copyright (c) 1982, 1986, 1989, 1993
63 * The Regents of the University of California. All rights reserved.
64 *
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
67 * are met:
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
80 *
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * SUCH DAMAGE.
92 *
93 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
94 */
95
96 #include <sys/param.h>
97 #include <sys/kernel.h>
98 #include <sys/malloc.h>
99 #include <sys/mbuf.h>
100 #include <sys/protosw.h>
101 #include <sys/socket.h>
102 #include <sys/socketvar.h>
103 #include <sys/sysctl.h>
104 #include <sys/errno.h>
105 #include <sys/stat.h>
106 #include <sys/systm.h>
107 #include <sys/syslog.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
110
111 #include <net/if.h>
112 #include <net/route.h>
113 #include <net/if_types.h>
114 #include <net/ntstat.h>
115
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip_var.h>
122 #include <netinet/udp.h>
123 #include <netinet/udp_var.h>
124 #include <netinet/ip6.h>
125 #include <netinet6/ip6_var.h>
126 #include <netinet6/in6_pcb.h>
127 #include <netinet/icmp6.h>
128 #include <netinet6/udp6_var.h>
129 #include <netinet6/ip6protosw.h>
130
131 #if IPSEC
132 #include <netinet6/ipsec.h>
133 #include <netinet6/ipsec6.h>
134 extern int ipsec_bypass;
135 #endif /*IPSEC*/
136
137 /*
138 * UDP protocol inplementation.
139 * Per RFC 768, August, 1980.
140 */
141
142 extern struct protosw inetsw[];
143 static int udp6_detach(struct socket *so);
144 static void udp6_append(struct inpcb *, struct ip6_hdr *,
145 struct sockaddr_in6 *, struct mbuf *, int);
146
147 extern void ipfwsyslog( int level, const char *format,...);
148 extern int fw_verbose;
149
150 #if IPFIREWALL
151 #define log_in_vain_log( a ) { \
152 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
153 ipfwsyslog a ; \
154 } \
155 else log a ; \
156 }
157 #else
158 #define log_in_vain_log( a ) { log a; }
159 #endif
160
161 /*
162 * subroutine of udp6_input(), mainly for source code readability.
163 */
164 static void
165 udp6_append(struct inpcb *last, __unused struct ip6_hdr *ip6,
166 struct sockaddr_in6 *udp_in6, struct mbuf *n, int off)
167 {
168 struct mbuf *opts = NULL;
169 int ret = 0;
170 #if CONFIG_MACF_NET
171 if (mac_inpcb_check_deliver(last, n, AF_INET6, SOCK_DGRAM) != 0) {
172 m_freem(n);
173 return;
174 }
175 #endif
176 if ((last->in6p_flags & IN6P_CONTROLOPTS) != 0 ||
177 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
178 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
179 ret = ip6_savecontrol(last, n, &opts);
180 if (ret != 0) {
181 m_freem(n);
182 m_freem(opts);
183 return;
184 }
185 }
186 m_adj(n, off);
187 if (nstat_collect) {
188 locked_add_64(&last->inp_stat->rxpackets, 1);
189 locked_add_64(&last->inp_stat->rxbytes, n->m_pkthdr.len);
190 }
191 so_recv_data_stat(last->in6p_socket, n, 0);
192 if (sbappendaddr(&last->in6p_socket->so_rcv,
193 (struct sockaddr *)udp_in6, n, opts, NULL) == 0)
194 udpstat.udps_fullsock++;
195 else
196 sorwakeup(last->in6p_socket);
197 }
198
199 int
200 udp6_input(
201 struct mbuf **mp,
202 int *offp,
203 int proto)
204 {
205 #pragma unused(proto)
206 struct mbuf *m = *mp;
207 struct ifnet *ifp;
208 register struct ip6_hdr *ip6;
209 register struct udphdr *uh;
210 register struct inpcb *in6p;
211 struct mbuf *opts = NULL;
212 int off = *offp;
213 int plen, ulen, ret = 0;
214 struct sockaddr_in6 udp_in6;
215 struct inpcbinfo *pcbinfo = &udbinfo;
216 struct sockaddr_in6 fromsa;
217
218 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), return IPPROTO_DONE);
219
220 ifp = m->m_pkthdr.rcvif;
221 ip6 = mtod(m, struct ip6_hdr *);
222
223 #if defined(NFAITH) && 0 < NFAITH
224 if (faithprefix(&ip6->ip6_dst)) {
225 /* XXX send icmp6 host/port unreach? */
226 m_freem(m);
227 return IPPROTO_DONE;
228 }
229 #endif
230
231 udpstat.udps_ipackets++;
232
233 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
234 uh = (struct udphdr *)((caddr_t)ip6 + off);
235 ulen = ntohs((u_short)uh->uh_ulen);
236
237 if (plen != ulen) {
238 udpstat.udps_badlen++;
239 goto bad;
240 }
241
242 /* destination port of 0 is illegal, based on RFC768. */
243 if (uh->uh_dport == 0)
244 goto bad;
245
246 /*
247 * Checksum extended UDP header and data.
248 */
249 if (uh->uh_sum) {
250 if ((apple_hwcksum_rx != 0) && (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) {
251 uh->uh_sum = m->m_pkthdr.csum_data;
252 uh->uh_sum ^= 0xffff;
253 }
254 else {
255 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
256 udpstat.udps_badsum++;
257 goto bad;
258 }
259 }
260 }
261 #ifndef __APPLE__
262 else
263 udpstat.udps_nosum++;
264 #endif
265
266 /*
267 * Construct sockaddr format source address.
268 */
269 init_sin6(&fromsa, m);
270 fromsa.sin6_port = uh->uh_sport;
271
272
273 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
274 int reuse_sock = 0, mcast_delivered = 0;
275 struct ip6_moptions *imo;
276 struct mbuf *n = NULL;
277
278 /*
279 * Deliver a multicast datagram to all sockets
280 * for which the local and remote addresses and ports match
281 * those of the incoming datagram. This allows more than
282 * one process to receive multicasts on the same port.
283 * (This really ought to be done for unicast datagrams as
284 * well, but that would cause problems with existing
285 * applications that open both address-specific sockets and
286 * a wildcard socket listening to the same port -- they would
287 * end up receiving duplicates of every unicast datagram.
288 * Those applications open the multiple sockets to overcome an
289 * inadequacy of the UDP socket interface, but for backwards
290 * compatibility we avoid the problem here rather than
291 * fixing the interface. Maybe 4.5BSD will remedy this?)
292 */
293
294 /*
295 * In a case that laddr should be set to the link-local
296 * address (this happens in RIPng), the multicast address
297 * specified in the received packet does not match with
298 * laddr. To cure this situation, the matching is relaxed
299 * if the receiving interface is the same as one specified
300 * in the socket and if the destination multicast address
301 * matches one of the multicast groups specified in the socket.
302 */
303
304 /*
305 * Construct sockaddr format source address.
306 */
307 init_sin6(&udp_in6, m); /* general init */
308 udp_in6.sin6_port = uh->uh_sport;
309 /*
310 * KAME note: usually we drop udphdr from mbuf here.
311 * We need udphdr for IPsec processing so we do that later.
312 */
313
314 /*
315 * Locate pcb(s) for datagram.
316 * (Algorithm copied from raw_intr().)
317 */
318 lck_rw_lock_shared(pcbinfo->mtx);
319
320 LIST_FOREACH(in6p, &udb, inp_list) {
321
322 if ((in6p->inp_vflag & INP_IPV6) == 0)
323 continue;
324
325 if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) == WNT_STOPUSING)
326 continue;
327
328 udp_lock(in6p->in6p_socket, 1, 0);
329
330 if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) {
331 udp_unlock(in6p->in6p_socket, 1, 0);
332 continue;
333 }
334 if (in6p->in6p_lport != uh->uh_dport) {
335 udp_unlock(in6p->in6p_socket, 1, 0);
336 continue;
337 }
338
339 /*
340 * Handle socket delivery policy for any-source
341 * and source-specific multicast. [RFC3678]
342 */
343 imo = in6p->in6p_moptions;
344 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
345 struct sockaddr_in6 mcaddr;
346 int blocked;
347
348 IM6O_LOCK(imo);
349 bzero(&mcaddr, sizeof(struct sockaddr_in6));
350 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
351 mcaddr.sin6_family = AF_INET6;
352 mcaddr.sin6_addr = ip6->ip6_dst;
353
354 blocked = im6o_mc_filter(imo, ifp,
355 (struct sockaddr *)&mcaddr,
356 (struct sockaddr *)&fromsa);
357 IM6O_UNLOCK(imo);
358 if (blocked != MCAST_PASS) {
359 udp_unlock(in6p->in6p_socket, 1, 0);
360 continue;
361 }
362 }
363 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
364 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
365 &ip6->ip6_src) ||
366 in6p->in6p_fport != uh->uh_sport) {
367 udp_unlock(in6p->in6p_socket, 1, 0);
368 continue;
369 }
370 }
371
372 reuse_sock = in6p->inp_socket->so_options &
373 (SO_REUSEPORT | SO_REUSEADDR);
374
375 {
376 #if IPSEC
377 int skipit = 0;
378 /* Check AH/ESP integrity. */
379 if (ipsec_bypass == 0) {
380 if (ipsec6_in_reject_so(m, in6p->inp_socket)) {
381 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
382 /* do not inject data to pcb */
383 skipit = 1;
384 }
385 }
386 if (skipit == 0)
387 #endif /*IPSEC*/
388 {
389 /*
390 * KAME NOTE: do not
391 * m_copy(m, offset, ...) below.
392 * sbappendaddr() expects M_PKTHDR,
393 * and m_copy() will copy M_PKTHDR
394 * only if offset is 0.
395 */
396 if (reuse_sock)
397 n = m_copy(m, 0, M_COPYALL);
398 udp6_append(in6p, ip6, &udp_in6, m,
399 off + sizeof (struct udphdr));
400 mcast_delivered++;
401 }
402 udp_unlock(in6p->in6p_socket, 1, 0);
403 }
404 /*
405 * Don't look for additional matches if this one does
406 * not have either the SO_REUSEPORT or SO_REUSEADDR
407 * socket options set. This heuristic avoids searching
408 * through all pcbs in the common case of a non-shared
409 * port. It assumes that an application will never
410 * clear these options after setting them.
411 */
412 if (reuse_sock == 0 || ((m = n) == NULL))
413 break;
414 /*
415 * Recompute IP and UDP header pointers for new mbuf
416 */
417 ip6 = mtod(m, struct ip6_hdr *);
418 uh = (struct udphdr *)((caddr_t)ip6 + off);
419 }
420 lck_rw_done(pcbinfo->mtx);
421
422 if (mcast_delivered == 0) {
423 /*
424 * No matching pcb found; discard datagram.
425 * (No need to send an ICMP Port Unreachable
426 * for a broadcast or multicast datgram.)
427 */
428 udpstat.udps_noport++;
429 #ifndef __APPLE__
430 udpstat.udps_noportmcast++;
431 #endif
432 goto bad;
433 }
434
435 if (reuse_sock != 0) /* free the extra copy of mbuf */
436 m_freem(m);
437 return IPPROTO_DONE;
438 }
439 /*
440 * Locate pcb for datagram.
441 */
442 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
443 &ip6->ip6_dst, uh->uh_dport, 1,
444 m->m_pkthdr.rcvif);
445 if (in6p == 0) {
446 if (log_in_vain) {
447 char buf[INET6_ADDRSTRLEN];
448
449 strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof(buf));
450 if (log_in_vain != 3)
451 log(LOG_INFO,
452 "Connection attempt to UDP %s:%d from %s:%d\n",
453 buf, ntohs(uh->uh_dport),
454 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
455 else if (!(m->m_flags & (M_BCAST | M_MCAST)) &&
456 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src))
457 log_in_vain_log((LOG_INFO,
458 "Connection attempt to UDP %s:%d from %s:%d\n",
459 buf, ntohs(uh->uh_dport),
460 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)));
461 }
462 udpstat.udps_noport++;
463 if (m->m_flags & M_MCAST) {
464 printf("UDP6: M_MCAST is set in a unicast packet.\n");
465 #ifndef __APPLE__
466 udpstat.udps_noportmcast++;
467 #endif
468 goto bad;
469 }
470 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
471 return IPPROTO_DONE;
472 }
473 #if IPSEC
474 /*
475 * Check AH/ESP integrity.
476 */
477 if (ipsec_bypass == 0) {
478 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
479 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
480 in_pcb_checkstate(in6p, WNT_RELEASE, 0);
481 goto bad;
482 }
483 }
484 #endif /*IPSEC*/
485
486 /*
487 * Construct sockaddr format source address.
488 * Stuff source address and datagram in user buffer.
489 */
490 udp_lock(in6p->in6p_socket, 1, 0);
491
492 if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) {
493 udp_unlock(in6p->in6p_socket, 1, 0);
494 goto bad;
495 }
496
497 init_sin6(&udp_in6, m); /* general init */
498 udp_in6.sin6_port = uh->uh_sport;
499 if ((in6p->in6p_flags & IN6P_CONTROLOPTS) != 0 ||
500 (in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
501 (in6p->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) {
502 ret = ip6_savecontrol(in6p, m, &opts);
503 if (ret != 0) {
504 udp_unlock(in6p->in6p_socket, 1, 0);
505 goto bad;
506 }
507 }
508 m_adj(m, off + sizeof(struct udphdr));
509 if (nstat_collect) {
510 locked_add_64(&in6p->inp_stat->rxpackets, 1);
511 locked_add_64(&in6p->inp_stat->rxbytes, m->m_pkthdr.len);
512 }
513 so_recv_data_stat(in6p->in6p_socket, m, 0);
514 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
515 (struct sockaddr *)&udp_in6,
516 m, opts, NULL) == 0) {
517 m = NULL;
518 opts = NULL;
519 udpstat.udps_fullsock++;
520 udp_unlock(in6p->in6p_socket, 1, 0);
521 goto bad;
522 }
523 sorwakeup(in6p->in6p_socket);
524 udp_unlock(in6p->in6p_socket, 1, 0);
525 return IPPROTO_DONE;
526 bad:
527 if (m)
528 m_freem(m);
529 if (opts)
530 m_freem(opts);
531 return IPPROTO_DONE;
532 }
533
534 void
535 udp6_ctlinput(
536 int cmd,
537 struct sockaddr *sa,
538 void *d)
539 {
540 struct udphdr uh;
541 struct ip6_hdr *ip6;
542 struct mbuf *m;
543 int off = 0;
544 struct ip6ctlparam *ip6cp = NULL;
545 const struct sockaddr_in6 *sa6_src = NULL;
546 void (*notify)(struct inpcb *, int) = udp_notify;
547 struct udp_portonly {
548 u_int16_t uh_sport;
549 u_int16_t uh_dport;
550 } *uhp;
551
552 if (sa->sa_family != AF_INET6 ||
553 sa->sa_len != sizeof(struct sockaddr_in6))
554 return;
555
556 if ((unsigned)cmd >= PRC_NCMDS)
557 return;
558 if (PRC_IS_REDIRECT(cmd))
559 notify = in6_rtchange, d = NULL;
560 else if (cmd == PRC_HOSTDEAD)
561 d = NULL;
562 else if (inet6ctlerrmap[cmd] == 0)
563 return;
564
565 /* if the parameter is from icmp6, decode it. */
566 if (d != NULL) {
567 ip6cp = (struct ip6ctlparam *)d;
568 m = ip6cp->ip6c_m;
569 ip6 = ip6cp->ip6c_ip6;
570 off = ip6cp->ip6c_off;
571 sa6_src = ip6cp->ip6c_src;
572 } else {
573 m = NULL;
574 ip6 = NULL;
575 sa6_src = &sa6_any;
576 }
577
578 if (ip6) {
579 /*
580 * XXX: We assume that when IPV6 is non NULL,
581 * M and OFF are valid.
582 */
583
584 /* check if we can safely examine src and dst ports */
585 if (m->m_pkthdr.len < off + sizeof(*uhp))
586 return;
587
588 bzero(&uh, sizeof(uh));
589 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
590
591 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
592 (struct sockaddr*)ip6cp->ip6c_src,
593 uh.uh_sport, cmd, NULL, notify);
594 } else
595 (void) in6_pcbnotify(&udbinfo, sa, 0, (struct sockaddr *)&sa6_src,
596 0, cmd, NULL, notify);
597 }
598
599 #ifndef __APPLE__
600 static int
601 udp6_getcred SYSCTL_HANDLER_ARGS
602 {
603 struct sockaddr_in6 addrs[2];
604 struct inpcb *inp;
605 int error, s;
606
607 error = proc_suser(req->p);
608 if (error)
609 return (error);
610
611 if (req->newlen != sizeof(addrs))
612 return (EINVAL);
613 if (req->oldlen != sizeof(*(kauth_cred_t)0))
614 return (EINVAL);
615 error = SYSCTL_IN(req, addrs, sizeof(addrs));
616 if (error)
617 return (error);
618 s = splnet();
619 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
620 addrs[1].sin6_port,
621 &addrs[0].sin6_addr, addrs[0].sin6_port,
622 1, NULL);
623 if (!inp || !inp->inp_socket || !inp->inp_socket->so_cred) {
624 error = ENOENT;
625 goto out;
626 }
627 /*
628 * XXX This should not be copying out a credential!!!! This
629 * XXX is an opaque type, and is not intended to be introspected,
630 * XXX and the size of this structure *WILL* change as planned MACF
631 * XXX and kauth changes go forward.
632 */
633 error = SYSCTL_OUT(req, inp->inp_socket->so_cred->pc_ucred,
634 sizeof(*(kauth_cred_t)0));
635
636 out:
637 splx(s);
638 return (error);
639 }
640
641 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
642 0, 0,
643 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
644 #endif
645
646 static int
647 udp6_abort(struct socket *so)
648 {
649 struct inpcb *inp;
650
651 inp = sotoinpcb(so);
652 if (inp == 0)
653 return EINVAL; /* ??? possible? panic instead? */
654 soisdisconnected(so);
655 in6_pcbdetach(inp);
656 return 0;
657 }
658
659 static int
660 udp6_attach(struct socket *so, __unused int proto, struct proc *p)
661 {
662 struct inpcb *inp;
663 int error;
664
665 inp = sotoinpcb(so);
666 if (inp != 0)
667 return EINVAL;
668
669 error = in_pcballoc(so, &udbinfo, p);
670 if (error)
671 return error;
672
673 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
674 error = soreserve(so, udp_sendspace, udp_recvspace);
675 if (error)
676 return error;
677 }
678 inp = (struct inpcb *)so->so_pcb;
679 inp->inp_vflag |= INP_IPV6;
680 if (ip6_mapped_addr_on)
681 inp->inp_vflag |= INP_IPV4;
682 inp->in6p_hops = -1; /* use kernel default */
683 inp->in6p_cksum = -1; /* just to be sure */
684 /*
685 * XXX: ugly!!
686 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
687 * because the socket may be bound to an IPv6 wildcard address,
688 * which may match an IPv4-mapped IPv6 address.
689 */
690 inp->inp_ip_ttl = ip_defttl;
691 nstat_udp_new_pcb(inp);
692 return 0;
693 }
694
695 static int
696 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
697 {
698 struct inpcb *inp;
699 int error;
700
701 inp = sotoinpcb(so);
702 if (inp == 0)
703 return EINVAL;
704
705 inp->inp_vflag &= ~INP_IPV4;
706 inp->inp_vflag |= INP_IPV6;
707 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
708 struct sockaddr_in6 *sin6_p;
709
710 sin6_p = (struct sockaddr_in6 *)nam;
711
712 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
713 inp->inp_vflag |= INP_IPV4;
714 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
715 struct sockaddr_in sin;
716
717 in6_sin6_2_sin(&sin, sin6_p);
718 inp->inp_vflag |= INP_IPV4;
719 inp->inp_vflag &= ~INP_IPV6;
720 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
721 return error;
722 }
723 }
724
725 error = in6_pcbbind(inp, nam, p);
726 return error;
727 }
728
729 static int
730 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
731 {
732 struct inpcb *inp;
733 int error;
734
735 inp = sotoinpcb(so);
736 if (inp == 0)
737 return EINVAL;
738
739 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
740 struct sockaddr_in6 *sin6_p;
741
742 sin6_p = (struct sockaddr_in6 *)nam;
743 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
744 struct sockaddr_in sin;
745
746 if (inp->inp_faddr.s_addr != INADDR_ANY)
747 return EISCONN;
748 in6_sin6_2_sin(&sin, sin6_p);
749 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p, NULL);
750 if (error == 0) {
751 inp->inp_vflag |= INP_IPV4;
752 inp->inp_vflag &= ~INP_IPV6;
753 soisconnected(so);
754 }
755 return error;
756 }
757 }
758
759 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
760 return EISCONN;
761 error = in6_pcbconnect(inp, nam, p);
762 if (error == 0) {
763 if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { /* should be non mapped addr */
764 inp->inp_vflag &= ~INP_IPV4;
765 inp->inp_vflag |= INP_IPV6;
766 }
767 soisconnected(so);
768 }
769 return error;
770 }
771
772 static int
773 udp6_detach(struct socket *so)
774 {
775 struct inpcb *inp;
776
777 inp = sotoinpcb(so);
778 if (inp == 0)
779 return EINVAL;
780 in6_pcbdetach(inp);
781 return 0;
782 }
783
784 static int
785 udp6_disconnect(struct socket *so)
786 {
787 struct inpcb *inp;
788
789 inp = sotoinpcb(so);
790 if (inp == 0)
791 return EINVAL;
792
793 if (inp->inp_vflag & INP_IPV4) {
794 struct pr_usrreqs *pru;
795
796 pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
797 return ((*pru->pru_disconnect)(so));
798 }
799
800 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
801 return ENOTCONN;
802
803 in6_pcbdisconnect(inp);
804 inp->in6p_laddr = in6addr_any;
805 inp->in6p_last_outif = 0;
806 so->so_state &= ~SS_ISCONNECTED; /* XXX */
807 return 0;
808 }
809
810 static int
811 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
812 struct mbuf *control, struct proc *p)
813 {
814 struct inpcb *inp;
815 int error = 0;
816
817 inp = sotoinpcb(so);
818 if (inp == 0) {
819 error = EINVAL;
820 goto bad;
821 }
822
823 if (addr) {
824 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
825 error = EINVAL;
826 goto bad;
827 }
828 if (addr->sa_family != AF_INET6) {
829 error = EAFNOSUPPORT;
830 goto bad;
831 }
832 }
833
834 if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
835 int hasv4addr;
836 struct sockaddr_in6 *sin6 = 0;
837
838 if (addr == 0)
839 hasv4addr = (inp->inp_vflag & INP_IPV4);
840 else {
841 sin6 = (struct sockaddr_in6 *)addr;
842 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
843 ? 1 : 0;
844 }
845 if (hasv4addr) {
846 struct pr_usrreqs *pru;
847
848 if (sin6)
849 in6_sin6_2_sin_in_sock(addr);
850 pru = ip_protox[IPPROTO_UDP]->pr_usrreqs;
851 error = ((*pru->pru_send)(so, flags, m, addr, control,
852 p));
853 /* addr will just be freed in sendit(). */
854 return error;
855 }
856 }
857
858 return udp6_output(inp, m, addr, control, p);
859
860 bad:
861 m_freem(m);
862 return(error);
863 }
864
865 struct pr_usrreqs udp6_usrreqs = {
866 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
867 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
868 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
869 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
870 in6_mapped_sockaddr, sosend, soreceive, pru_sopoll_notsupp
871 };