1 /*
2 * Copyright (c) 2000-2024 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 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
61 */
62 /*
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Version 2.0.
67 */
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/mcache.h>
75 #include <sys/proc.h>
76 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <libkern/OSAtomic.h>
82 #include <kern/uipc_socket.h>
83 #include <kern/zalloc.h>
84
85 #include <pexpert/pexpert.h>
86
87 #include <net/if.h>
88 #include <net/net_api_stats.h>
89 #include <net/route.h>
90 #include <net/content_filter.h>
91 #include <net/sockaddr_utils.h>
92
93 #define _IP_VHL
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_tclass.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip_var.h>
101
102 #include <netinet6/in6_pcb.h>
103
104
105 #if IPSEC
106 #include <netinet6/ipsec.h>
107 #endif /*IPSEC*/
108
109 #if DUMMYNET
110 #include <netinet/ip_dummynet.h>
111 #endif /* DUMMYNET */
112
113 int rip_detach(struct socket *);
114 int rip_abort(struct socket *);
115 int rip_disconnect(struct socket *);
116 int rip_bind(struct socket *, struct sockaddr *, struct proc *);
117 int rip_connect(struct socket *, struct sockaddr *, struct proc *);
118 int rip_shutdown(struct socket *);
119
120 struct inpcbhead ripcb;
121 struct inpcbinfo ripcbinfo;
122
123 /* control hooks for dummynet */
124 #if DUMMYNET
125 ip_dn_ctl_t *ip_dn_ctl_ptr;
126 #endif /* DUMMYNET */
127
128 /*
129 * Nominal space allocated to a raw ip socket.
130 */
131 #define RIPSNDQ 8192
132 #define RIPRCVQ 8192
133
134 static KALLOC_TYPE_DEFINE(ripzone, struct inpcb, NET_KT_DEFAULT);
135
136 /*
137 * Raw interface to IP protocol.
138 */
139
140 /*
141 * Initialize raw connection block q.
142 */
143 void
rip_init(struct protosw * pp,struct domain * dp)144 rip_init(struct protosw *pp, struct domain *dp)
145 {
146 #pragma unused(dp)
147 static int rip_initialized = 0;
148 struct inpcbinfo *pcbinfo;
149
150 VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
151
152 if (!os_atomic_cmpxchg(&rip_initialized, 0, 1, relaxed)) {
153 return;
154 }
155
156 LIST_INIT(&ripcb);
157 ripcbinfo.ipi_listhead = &ripcb;
158 /*
159 * XXX We don't use the hash list for raw IP, but it's easier
160 * to allocate a one entry hash list than it is to check all
161 * over the place for ipi_hashbase == NULL.
162 */
163 hashinit_counted_by(1, ripcbinfo.ipi_hashbase,
164 ripcbinfo.ipi_hashbase_count);
165 ripcbinfo.ipi_hashmask = ripcbinfo.ipi_hashbase_count - 1;
166 hashinit_counted_by(1, ripcbinfo.ipi_porthashbase,
167 ripcbinfo.ipi_porthashbase_count);
168 ripcbinfo.ipi_porthashmask = ripcbinfo.ipi_porthashbase_count - 1;
169 ripcbinfo.ipi_zone = ripzone;
170
171 pcbinfo = &ripcbinfo;
172 /*
173 * allocate lock group attribute and group for udp pcb mutexes
174 */
175 pcbinfo->ipi_lock_grp = lck_grp_alloc_init("ripcb", LCK_GRP_ATTR_NULL);
176
177 /*
178 * allocate the lock attribute for udp pcb mutexes
179 */
180 lck_attr_setdefault(&pcbinfo->ipi_lock_attr);
181 lck_rw_init(&pcbinfo->ipi_lock, pcbinfo->ipi_lock_grp,
182 &pcbinfo->ipi_lock_attr);
183
184 in_pcbinfo_attach(&ripcbinfo);
185 }
186
187 static uint32_t
rip_inp_input(struct inpcb * inp,struct mbuf * m,int iphlen)188 rip_inp_input(struct inpcb *inp, struct mbuf *m, int iphlen)
189 {
190 struct ip *ip = mtod(m, struct ip *);
191 struct ifnet *ifp = m->m_pkthdr.rcvif;
192 struct sockaddr_in ripsrc = {
193 .sin_len = sizeof(ripsrc),
194 .sin_family = AF_INET,
195 .sin_port = 0,
196 .sin_addr = { .s_addr = 0 },
197 .sin_zero = {0, 0, 0, 0, 0, 0, 0, 0, }
198 };
199 mbuf_ref_t opts = NULL;
200 boolean_t is_wake_pkt = false;
201 uint32_t num_delivered = 0;
202
203 #if NECP
204 if (!necp_socket_is_allowed_to_send_recv_v4(inp, 0, 0,
205 &ip->ip_dst, &ip->ip_src, ifp, 0, NULL, NULL, NULL, NULL)) {
206 /* do not inject data to pcb */
207 m_freem(m);
208 goto done;
209 }
210 #endif /* NECP */
211
212 ripsrc.sin_addr = ip->ip_src;
213
214 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.pkt_flags & PKTF_WAKE_PKT)) {
215 is_wake_pkt = true;
216 }
217
218 if ((inp->inp_flags & INP_CONTROLOPTS) != 0 ||
219 SOFLOW_ENABLED(inp->inp_socket) ||
220 SO_RECV_CONTROL_OPTS(inp->inp_socket)) {
221 if (ip_savecontrol(inp, &opts, ip, m) != 0) {
222 m_freem(m);
223 m_freem(opts);
224 goto done;
225 }
226 }
227 if (inp->inp_flags & INP_STRIPHDR
228 #if CONTENT_FILTER
229 /*
230 * If socket is subject to Content Filter, delay stripping until reinject
231 */
232 && (!CFIL_DGRAM_FILTERED(inp->inp_socket))
233 #endif
234 ) {
235 m->m_len -= iphlen;
236 m->m_pkthdr.len -= iphlen;
237 m->m_data += iphlen;
238 }
239 so_recv_data_stat(inp->inp_socket, m, 0);
240 if (sbappendaddr(&inp->inp_socket->so_rcv,
241 (struct sockaddr *)&ripsrc, m, opts, NULL) != 0) {
242 num_delivered = 1;
243 sorwakeup(inp->inp_socket);
244 if (is_wake_pkt) {
245 soevent(inp->in6p_socket,
246 SO_FILT_HINT_LOCKED | SO_FILT_HINT_WAKE_PKT);
247 }
248 } else {
249 ipstat.ips_raw_sappend_fail++;
250 }
251 done:
252 return num_delivered;
253 }
254
255 /*
256 * The first pass is for IPv4 socket and the second pass for IPv6
257 */
258 static bool
rip_input_inner(struct mbuf * m,int iphlen,bool is_ipv4_pass,uint32_t * total_delivered)259 rip_input_inner(struct mbuf *m, int iphlen, bool is_ipv4_pass, uint32_t *total_delivered)
260 {
261 struct inpcb *inp;
262 struct inpcb *last = NULL;
263 struct ip *ip = mtod(m, struct ip *);
264 struct ifnet *ifp = m->m_pkthdr.rcvif;
265 bool need_ipv6_pass = false;
266 uint32_t num_delivered = 0;
267
268 lck_rw_lock_shared(&ripcbinfo.ipi_lock);
269 LIST_FOREACH(inp, &ripcb, inp_list) {
270 if (is_ipv4_pass) {
271 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) != INP_IPV4) {
272 /* Tell if we need to an IPv6 pass */
273 need_ipv6_pass = true;
274 continue;
275 }
276 } else {
277 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) != (INP_IPV4 | INP_IPV6)) {
278 continue;
279 }
280 }
281 if (inp->inp_ip_p && (inp->inp_ip_p != ip->ip_p)) {
282 continue;
283 }
284 if (inp->inp_laddr.s_addr &&
285 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) {
286 continue;
287 }
288 if (inp->inp_faddr.s_addr &&
289 inp->inp_faddr.s_addr != ip->ip_src.s_addr) {
290 continue;
291 }
292 if (inp_restricted_recv(inp, ifp)) {
293 continue;
294 }
295 if (last != NULL) {
296 struct mbuf *n = m_copym_mode(m, 0, (int)M_COPYALL, M_DONTWAIT, NULL, NULL, M_COPYM_MUST_COPY_HDR);
297
298 if (n == NULL) {
299 continue;
300 }
301 num_delivered += rip_inp_input(last, n, iphlen);
302 }
303 last = inp;
304 }
305
306 /*
307 * Consume the orignal mbuf 'm' if:
308 * - it is the first pass and there is no IPv6 raw socket
309 * - it is the second pass for IPv6
310 */
311 if (need_ipv6_pass == false || is_ipv4_pass == false) {
312 if (last != NULL) {
313 num_delivered += rip_inp_input(last, m, iphlen);
314 } else {
315 m_freem(m);
316 }
317 } else {
318 if (last != NULL) {
319 struct mbuf *n = m_copym_mode(m, 0, (int)M_COPYALL, M_DONTWAIT, NULL, NULL, M_COPYM_MUST_COPY_HDR);
320
321 if (n != NULL) {
322 num_delivered += rip_inp_input(last, n, iphlen);
323 }
324 }
325 }
326 /*
327 * Keep the list locked because socket filter may force the socket lock
328 * to be released when calling sbappendaddr() -- see rdar://7627704
329 */
330 lck_rw_done(&ripcbinfo.ipi_lock);
331
332 *total_delivered += num_delivered;
333
334 return need_ipv6_pass;
335 }
336
337
338 /*
339 * Setup generic address and protocol structures
340 * for raw_input routine, then pass them along with
341 * mbuf chain.
342 */
343 void
rip_input(struct mbuf * m,int iphlen)344 rip_input(struct mbuf *m, int iphlen)
345 {
346 uint32_t num_delivered = 0;
347 bool need_v6_pass = false;
348
349 /* Expect 32-bit aligned data pointer on strict-align platforms */
350 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
351
352 /*
353 * First pass for raw IPv4 sockets that are protected by the inet_domain_mutex lock
354 */
355 need_v6_pass = rip_input_inner(m, iphlen, true, &num_delivered);
356
357 /*
358 * For the IPv6 pass we need to switch to the inet6_domain_mutex lock
359 * to protect the raw IPv6 sockets
360 */
361 if (need_v6_pass) {
362 lck_mtx_unlock(inet_domain_mutex);
363
364 lck_mtx_lock(inet6_domain_mutex);
365 rip_input_inner(m, iphlen, false, &num_delivered);
366 lck_mtx_unlock(inet6_domain_mutex);
367
368 lck_mtx_lock(inet_domain_mutex);
369 }
370
371 if (num_delivered > 0) {
372 OSAddAtomic(1, &ipstat.ips_delivered);
373 } else {
374 OSAddAtomic(1, &ipstat.ips_noproto);
375 }
376 }
377
378 /*
379 * Generate IP header and pass packet to ip_output.
380 * Tack on options user may have setup with control call.
381 */
382 int
rip_output(struct mbuf * m,struct socket * so,u_int32_t dst,struct mbuf * control)383 rip_output(
384 struct mbuf *m,
385 struct socket *so,
386 u_int32_t dst,
387 struct mbuf *control)
388 {
389 struct ip *ip;
390 struct inpcb *inp = sotoinpcb(so);
391 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
392 int inp_flags = inp ? inp->inp_flags : 0;
393 struct sock_cm_info sockcminfo;
394 struct ip_out_args ipoa;
395 struct ip_moptions *imo;
396 int tos = IPTOS_UNSPEC;
397 int error = 0;
398 #if CONTENT_FILTER
399 struct m_tag *cfil_tag = NULL;
400 bool cfil_faddr_use = false;
401 uint32_t cfil_so_state_change_cnt = 0;
402 uint32_t cfil_so_options = 0;
403 int cfil_inp_flags = 0;
404 struct sockaddr *__single cfil_faddr = NULL;
405 struct sockaddr_in *__single cfil_sin;
406 u_int32_t cfil_dst = 0;
407 #endif
408
409 #if CONTENT_FILTER
410 /*
411 * If socket is subject to Content Filter and no addr is passed in,
412 * retrieve CFIL saved state from mbuf and use it if necessary.
413 */
414 if (CFIL_DGRAM_FILTERED(so) && dst == INADDR_ANY) {
415 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, &cfil_inp_flags);
416 if (cfil_tag) {
417 cfil_sin = SIN(cfil_faddr);
418 flags = (cfil_so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
419 inp_flags = cfil_inp_flags;
420 if (inp && inp->inp_faddr.s_addr == INADDR_ANY) {
421 /*
422 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
423 * the connect/disconnect logic.
424 */
425 dst = cfil_sin->sin_addr.s_addr;
426 } else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
427 (inp->inp_fport != cfil_sin->sin_port ||
428 inp->inp_faddr.s_addr != cfil_sin->sin_addr.s_addr)) {
429 /*
430 * Socket is connected but socket state and dest addr/port changed.
431 * We need to use the saved faddr and socket options.
432 */
433 cfil_faddr_use = true;
434 cfil_dst = cfil_sin->sin_addr.s_addr;
435 }
436 m_tag_free(cfil_tag);
437 }
438 }
439 #endif
440
441 if (so->so_state & SS_ISCONNECTED) {
442 if (dst != INADDR_ANY) {
443 if (m != NULL) {
444 m_freem(m);
445 }
446 if (control != NULL) {
447 m_freem(control);
448 }
449 return EISCONN;
450 }
451 dst = cfil_faddr_use ? cfil_dst : inp->inp_faddr.s_addr;
452 } else {
453 if (dst == INADDR_ANY) {
454 if (m != NULL) {
455 m_freem(m);
456 }
457 if (control != NULL) {
458 m_freem(control);
459 }
460 return ENOTCONN;
461 }
462 }
463
464 bzero(&ipoa, sizeof(ipoa));
465 ipoa.ipoa_boundif = IFSCOPE_NONE;
466 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF;
467
468 sock_init_cm_info(&sockcminfo, so);
469
470 if (control != NULL) {
471 tos = ip_tos_from_control(control);
472 sock_parse_cm_info(control, &sockcminfo);
473
474 m_freem(control);
475 control = NULL;
476 }
477 if (inp == NULL
478 #if NECP
479 || (necp_socket_should_use_flow_divert(inp))
480 #endif /* NECP */
481 ) {
482 if (m != NULL) {
483 m_freem(m);
484 }
485 VERIFY(control == NULL);
486 return inp == NULL ? EINVAL : EPROTOTYPE;
487 }
488
489 flags |= IP_OUTARGS;
490 /* If socket was bound to an ifindex, tell ip_output about it */
491 if (inp->inp_flags & INP_BOUND_IF) {
492 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
493 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
494 }
495 if (INP_NO_CELLULAR(inp)) {
496 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
497 }
498 if (INP_NO_EXPENSIVE(inp)) {
499 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
500 }
501 if (INP_NO_CONSTRAINED(inp)) {
502 ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
503 }
504 if (INP_AWDL_UNRESTRICTED(inp)) {
505 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
506 }
507 if (INP_MANAGEMENT_ALLOWED(inp)) {
508 ipoa.ipoa_flags |= IPOAF_MANAGEMENT_ALLOWED;
509 }
510 if (INP_ULTRA_CONSTRAINED_ALLOWED(inp)) {
511 ipoa.ipoa_flags |= IPOAF_ULTRA_CONSTRAINED_ALLOWED;
512 }
513 ipoa.ipoa_sotc = sockcminfo.sotc;
514 ipoa.ipoa_netsvctype = sockcminfo.netsvctype;
515
516 if (inp->inp_flowhash == 0) {
517 inp_calc_flowhash(inp);
518 ASSERT(inp->inp_flowhash != 0);
519 }
520
521 /*
522 * If the user handed us a complete IP packet, use it.
523 * Otherwise, allocate an mbuf for a header and fill it in.
524 */
525 if ((inp_flags & INP_HDRINCL) == 0) {
526 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
527 m_freem(m);
528 return EMSGSIZE;
529 }
530 M_PREPEND(m, sizeof(struct ip), M_WAIT, 1);
531 if (m == NULL) {
532 return ENOBUFS;
533 }
534 ip = mtod(m, struct ip *);
535 if (tos != IPTOS_UNSPEC) {
536 ip->ip_tos = (uint8_t)(tos & IPTOS_MASK);
537 } else {
538 ip->ip_tos = inp->inp_ip_tos;
539 }
540 if (inp->inp_flags2 & INP2_DONTFRAG) {
541 ip->ip_off = IP_DF;
542 } else {
543 ip->ip_off = 0;
544 }
545 ip->ip_p = inp->inp_ip_p;
546 ip->ip_len = (uint16_t)m->m_pkthdr.len;
547 ip->ip_src = inp->inp_laddr;
548 ip->ip_dst.s_addr = dst;
549 ip->ip_ttl = inp->inp_ip_ttl;
550 } else {
551 if (m->m_pkthdr.len > IP_MAXPACKET) {
552 m_freem(m);
553 return EMSGSIZE;
554 }
555 ip = mtod(m, struct ip *);
556 /*
557 * don't allow both user specified and setsockopt options,
558 * and don't allow packet length sizes that will crash
559 */
560 if (m->m_pkthdr.len < sizeof(struct ip) ||
561 ((IP_VHL_HL(ip->ip_vhl) != (sizeof(*ip) >> 2)) && inp->inp_options) ||
562 (ip->ip_len > m->m_pkthdr.len) ||
563 (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
564 m_freem(m);
565 return EINVAL;
566 }
567 if (ip->ip_id == 0 && !(rfc6864 && IP_OFF_IS_ATOMIC(ntohs(ip->ip_off)))) {
568 ip->ip_id = ip_randomid((uint64_t)m);
569 }
570 /* XXX prevent ip_output from overwriting header fields */
571 flags |= IP_RAWOUTPUT;
572 OSAddAtomic(1, &ipstat.ips_rawout);
573 }
574
575 if (inp->inp_laddr.s_addr != INADDR_ANY) {
576 ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
577 }
578
579 #if NECP
580 {
581 necp_kernel_policy_id policy_id;
582 necp_kernel_policy_id skip_policy_id;
583 u_int32_t route_rule_id;
584 u_int32_t pass_flags;
585
586 /*
587 * We need a route to perform NECP route rule checks
588 */
589 if ((net_qos_policy_restricted != 0 &&
590 ROUTE_UNUSABLE(&inp->inp_route))
591 #if CONTENT_FILTER
592 || cfil_faddr_use
593 #endif
594 ) {
595 struct sockaddr_in to;
596 struct sockaddr_in from;
597 struct in_addr laddr = ip->ip_src;
598
599 ROUTE_RELEASE(&inp->inp_route);
600
601 bzero(&from, sizeof(struct sockaddr_in));
602 from.sin_family = AF_INET;
603 from.sin_len = sizeof(struct sockaddr_in);
604 from.sin_addr = laddr;
605
606 bzero(&to, sizeof(struct sockaddr_in));
607 to.sin_family = AF_INET;
608 to.sin_len = sizeof(struct sockaddr_in);
609 to.sin_addr.s_addr = ip->ip_dst.s_addr;
610
611 if ((error = in_pcbladdr(inp, (struct sockaddr *)&to,
612 &laddr, ipoa.ipoa_boundif, NULL, 1)) != 0) {
613 printf("%s in_pcbladdr(%p) error %d\n",
614 __func__, inp, error);
615 m_freem(m);
616 return error;
617 }
618
619 inp_update_necp_policy(inp, (struct sockaddr *)&from,
620 (struct sockaddr *)&to, ipoa.ipoa_boundif);
621 inp->inp_policyresult.results.qos_marking_gencount = 0;
622 }
623
624 if (!necp_socket_is_allowed_to_send_recv_v4(inp, 0, 0,
625 &ip->ip_src, &ip->ip_dst, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
626 m_freem(m);
627 return EHOSTUNREACH;
628 }
629
630 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
631
632 if (net_qos_policy_restricted != 0) {
633 struct ifnet *rt_ifp = NULL;
634
635 if (inp->inp_route.ro_rt != NULL) {
636 rt_ifp = inp->inp_route.ro_rt->rt_ifp;
637 }
638
639 necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
640 }
641 }
642 #endif /* NECP */
643 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
644 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
645 }
646 #if IPSEC
647 if (inp->inp_sp != NULL && ipsec_setsocket(m, so) != 0) {
648 m_freem(m);
649 return ENOBUFS;
650 }
651 #endif /*IPSEC*/
652
653 if (ROUTE_UNUSABLE(&inp->inp_route)) {
654 ROUTE_RELEASE(&inp->inp_route);
655 }
656
657 set_packet_service_class(m, so, sockcminfo.sotc, 0);
658 if (sockcminfo.tx_time) {
659 mbuf_set_tx_time(m, sockcminfo.tx_time);
660 }
661 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
662 m->m_pkthdr.pkt_flowid = inp->inp_flowhash;
663 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
664 PKTF_FLOW_RAWSOCK);
665 m->m_pkthdr.pkt_proto = inp->inp_ip_p;
666 m->m_pkthdr.tx_rawip_pid = so->last_pid;
667 m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
668 if (so->so_flags & SOF_DELEGATED) {
669 m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
670 } else {
671 m->m_pkthdr.tx_rawip_e_pid = 0;
672 }
673 #if (DEBUG || DEVELOPMENT)
674 if (so->so_flags & SOF_MARK_WAKE_PKT) {
675 so->so_flags &= ~SOF_MARK_WAKE_PKT;
676 m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
677 }
678 #endif /* (DEBUG || DEVELOPMENT) */
679
680 imo = inp->inp_moptions;
681 if (imo != NULL) {
682 IMO_ADDREF(imo);
683 }
684 /*
685 * The domain lock is held across ip_output, so it is okay
686 * to pass the PCB cached route pointer directly to IP and
687 * the modules beneath it.
688 */
689 // TODO: PASS DOWN ROUTE RULE ID
690 error = ip_output(m, inp->inp_options, &inp->inp_route, flags,
691 imo, &ipoa);
692
693 if (imo != NULL) {
694 IMO_REMREF(imo);
695 }
696
697 if (inp->inp_route.ro_rt != NULL) {
698 struct rtentry *rt = inp->inp_route.ro_rt;
699 struct ifnet *outif;
700
701 if ((rt->rt_flags & (RTF_MULTICAST | RTF_BROADCAST)) ||
702 inp->inp_socket == NULL ||
703 #if CONTENT_FILTER
704 /* Discard temporary route for cfil case */
705 cfil_faddr_use ||
706 #endif
707 !(inp->inp_socket->so_state & SS_ISCONNECTED)) {
708 rt = NULL; /* unusable */
709 }
710 /*
711 * Always discard the cached route for unconnected
712 * socket or if it is a multicast route.
713 */
714 if (rt == NULL) {
715 ROUTE_RELEASE(&inp->inp_route);
716 }
717
718 /*
719 * If this is a connected socket and the destination
720 * route is unicast, update outif with that of the
721 * route interface used by IP.
722 */
723 if (rt != NULL &&
724 (outif = rt->rt_ifp) != inp->inp_last_outifp) {
725 inp->inp_last_outifp = outif;
726 }
727 } else {
728 ROUTE_RELEASE(&inp->inp_route);
729 }
730
731 /*
732 * If output interface was cellular/expensive/constrained, and this socket is
733 * denied access to it, generate an event.
734 */
735 if (error != 0 && (ipoa.ipoa_flags & IPOAF_R_IFDENIED) &&
736 (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp) || INP_NO_CONSTRAINED(inp))) {
737 soevent(so, (SO_FILT_HINT_LOCKED | SO_FILT_HINT_IFDENIED));
738 }
739
740 return error;
741 }
742
743
744 /*
745 * Raw IP socket option processing.
746 */
747 int
rip_ctloutput(struct socket * so,struct sockopt * sopt)748 rip_ctloutput(struct socket *so, struct sockopt *sopt)
749 {
750 struct inpcb *inp = sotoinpcb(so);
751 int error, optval;
752
753 /* Allow <SOL_SOCKET,SO_BINDTODEVICE> at this level */
754 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_BINDTODEVICE) {
755 return ip_ctloutput(so, sopt);
756 }
757
758 if (sopt->sopt_level != IPPROTO_IP) {
759 return EINVAL;
760 }
761
762 error = 0;
763
764 switch (sopt->sopt_dir) {
765 case SOPT_GET:
766 switch (sopt->sopt_name) {
767 case IP_HDRINCL:
768 optval = inp->inp_flags & INP_HDRINCL;
769 error = sooptcopyout(sopt, &optval, sizeof optval);
770 break;
771
772 case IP_STRIPHDR:
773 optval = inp->inp_flags & INP_STRIPHDR;
774 error = sooptcopyout(sopt, &optval, sizeof optval);
775 break;
776
777
778 #if DUMMYNET
779 case IP_DUMMYNET_GET:
780 if (!DUMMYNET_LOADED) {
781 ip_dn_init();
782 }
783 if (DUMMYNET_LOADED) {
784 error = ip_dn_ctl_ptr(sopt);
785 } else {
786 error = ENOPROTOOPT;
787 }
788 break;
789 #endif /* DUMMYNET */
790
791 default:
792 error = ip_ctloutput(so, sopt);
793 break;
794 }
795 break;
796
797 case SOPT_SET:
798 switch (sopt->sopt_name) {
799 case IP_HDRINCL:
800 error = sooptcopyin(sopt, &optval, sizeof optval,
801 sizeof optval);
802 if (error) {
803 break;
804 }
805 if (optval) {
806 inp->inp_flags |= INP_HDRINCL;
807 } else {
808 inp->inp_flags &= ~INP_HDRINCL;
809 }
810 break;
811
812 case IP_STRIPHDR:
813 error = sooptcopyin(sopt, &optval, sizeof optval,
814 sizeof optval);
815 if (error) {
816 break;
817 }
818 if (optval) {
819 inp->inp_flags |= INP_STRIPHDR;
820 } else {
821 inp->inp_flags &= ~INP_STRIPHDR;
822 }
823 break;
824
825
826 #if DUMMYNET
827 case IP_DUMMYNET_CONFIGURE:
828 case IP_DUMMYNET_DEL:
829 case IP_DUMMYNET_FLUSH:
830 if (!DUMMYNET_LOADED) {
831 ip_dn_init();
832 }
833 if (DUMMYNET_LOADED) {
834 error = ip_dn_ctl_ptr(sopt);
835 } else {
836 error = ENOPROTOOPT;
837 }
838 break;
839 #endif /* DUMMYNET */
840
841 case SO_FLUSH:
842 if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
843 sizeof(optval))) != 0) {
844 break;
845 }
846
847 error = inp_flush(inp, optval);
848 break;
849
850 default:
851 error = ip_ctloutput(so, sopt);
852 break;
853 }
854 break;
855 }
856
857 return error;
858 }
859
860 /*
861 * This function exists solely to receive the PRC_IFDOWN messages which
862 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa,
863 * and calls in_ifadown() to remove all routes corresponding to that address.
864 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
865 * interface routes.
866 */
867 void
rip_ctlinput(int cmd,struct sockaddr * sa,__unused void * vip,__unused struct ifnet * ifp)868 rip_ctlinput(
869 int cmd,
870 struct sockaddr *sa,
871 __unused void *vip,
872 __unused struct ifnet *ifp)
873 {
874 struct in_ifaddr *ia = NULL;
875 struct ifnet *iaifp = NULL;
876 int err = 0;
877 int flags, done = 0;
878
879 switch (cmd) {
880 case PRC_IFDOWN:
881 lck_rw_lock_shared(&in_ifaddr_rwlock);
882 for (ia = in_ifaddrhead.tqh_first; ia;
883 ia = ia->ia_link.tqe_next) {
884 IFA_LOCK(&ia->ia_ifa);
885 if (ia->ia_ifa.ifa_addr == sa &&
886 (ia->ia_flags & IFA_ROUTE)) {
887 done = 1;
888 ifa_addref(&ia->ia_ifa);
889 IFA_UNLOCK(&ia->ia_ifa);
890 lck_rw_done(&in_ifaddr_rwlock);
891 lck_mtx_lock(rnh_lock);
892 /*
893 * in_ifscrub kills the interface route.
894 */
895 in_ifscrub(ia->ia_ifp, ia, 1);
896 /*
897 * in_ifadown gets rid of all the rest of
898 * the routes. This is not quite the right
899 * thing to do, but at least if we are running
900 * a routing process they will come back.
901 */
902 in_ifadown(&ia->ia_ifa, 1);
903 lck_mtx_unlock(rnh_lock);
904 ifa_remref(&ia->ia_ifa);
905 break;
906 }
907 IFA_UNLOCK(&ia->ia_ifa);
908 }
909 if (!done) {
910 lck_rw_done(&in_ifaddr_rwlock);
911 }
912 break;
913
914 case PRC_IFUP:
915 lck_rw_lock_shared(&in_ifaddr_rwlock);
916 for (ia = in_ifaddrhead.tqh_first; ia;
917 ia = ia->ia_link.tqe_next) {
918 IFA_LOCK(&ia->ia_ifa);
919 if (ia->ia_ifa.ifa_addr == sa) {
920 /* keep it locked */
921 break;
922 }
923 IFA_UNLOCK(&ia->ia_ifa);
924 }
925 if (ia == NULL || (ia->ia_flags & IFA_ROUTE) ||
926 (ia->ia_ifa.ifa_debug & IFD_NOTREADY)) {
927 if (ia != NULL) {
928 IFA_UNLOCK(&ia->ia_ifa);
929 }
930 lck_rw_done(&in_ifaddr_rwlock);
931 return;
932 }
933 ifa_addref(&ia->ia_ifa);
934 IFA_UNLOCK(&ia->ia_ifa);
935 lck_rw_done(&in_ifaddr_rwlock);
936
937 flags = RTF_UP;
938 iaifp = ia->ia_ifa.ifa_ifp;
939
940 if ((iaifp->if_flags & IFF_LOOPBACK)
941 || (iaifp->if_flags & IFF_POINTOPOINT)) {
942 flags |= RTF_HOST;
943 }
944
945 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
946 if (err == 0) {
947 IFA_LOCK_SPIN(&ia->ia_ifa);
948 ia->ia_flags |= IFA_ROUTE;
949 IFA_UNLOCK(&ia->ia_ifa);
950 }
951 ifa_remref(&ia->ia_ifa);
952 break;
953 }
954 }
955
956 u_int32_t rip_sendspace = RIPSNDQ;
957 u_int32_t rip_recvspace = RIPRCVQ;
958
959 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW | CTLFLAG_LOCKED,
960 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
961 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW | CTLFLAG_LOCKED,
962 &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
963 SYSCTL_UINT(_net_inet_raw, OID_AUTO, pcbcount, CTLFLAG_RD | CTLFLAG_LOCKED,
964 &ripcbinfo.ipi_count, 0, "Number of active PCBs");
965
966 static int
rip_attach(struct socket * so,int proto,struct proc * p)967 rip_attach(struct socket *so, int proto, struct proc *p)
968 {
969 struct inpcb *inp;
970 int error;
971
972 inp = sotoinpcb(so);
973 if (inp) {
974 panic("rip_attach");
975 }
976 if ((so->so_state & SS_PRIV) == 0) {
977 return EPERM;
978 }
979 if (proto > UINT8_MAX) {
980 return EINVAL;
981 }
982
983 error = soreserve(so, rip_sendspace, rip_recvspace);
984 if (error) {
985 return error;
986 }
987 error = in_pcballoc(so, &ripcbinfo, p);
988 if (error) {
989 return error;
990 }
991 inp = (struct inpcb *)so->so_pcb;
992 inp->inp_vflag |= INP_IPV4;
993 VERIFY(proto <= UINT8_MAX);
994 inp->inp_ip_p = (u_char)proto;
995 inp->inp_ip_ttl = (u_char)ip_defttl;
996 return 0;
997 }
998
999 __private_extern__ int
rip_detach(struct socket * so)1000 rip_detach(struct socket *so)
1001 {
1002 struct inpcb *inp;
1003
1004 inp = sotoinpcb(so);
1005 if (inp == 0) {
1006 panic("rip_detach");
1007 }
1008 in_pcbdetach(inp);
1009 return 0;
1010 }
1011
1012 __private_extern__ int
rip_abort(struct socket * so)1013 rip_abort(struct socket *so)
1014 {
1015 soisdisconnected(so);
1016 return rip_detach(so);
1017 }
1018
1019 __private_extern__ int
rip_disconnect(struct socket * so)1020 rip_disconnect(struct socket *so)
1021 {
1022 if ((so->so_state & SS_ISCONNECTED) == 0) {
1023 return ENOTCONN;
1024 }
1025 return rip_abort(so);
1026 }
1027
1028 __private_extern__ int
rip_bind(struct socket * so,struct sockaddr * nam,struct proc * p)1029 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1030 {
1031 #pragma unused(p)
1032 struct inpcb *inp = sotoinpcb(so);
1033 struct sockaddr_in sin;
1034 struct ifaddr *ifa = NULL;
1035 struct ifnet *outif = NULL;
1036
1037 if (inp == NULL
1038 #if NECP
1039 || (necp_socket_should_use_flow_divert(inp))
1040 #endif /* NECP */
1041 ) {
1042 return inp == NULL ? EINVAL : EPROTOTYPE;
1043 }
1044
1045 if (nam->sa_len != sizeof(struct sockaddr_in)) {
1046 return EINVAL;
1047 }
1048
1049 /* Sanitized local copy for interface address searches */
1050 bzero(&sin, sizeof(sin));
1051 sin.sin_family = AF_INET;
1052 sin.sin_len = sizeof(struct sockaddr_in);
1053 sin.sin_addr.s_addr = SIN(nam)->sin_addr.s_addr;
1054
1055 if (TAILQ_EMPTY(&ifnet_head) ||
1056 (sin.sin_family != AF_INET && sin.sin_family != AF_IMPLINK) ||
1057 (sin.sin_addr.s_addr && (ifa = ifa_ifwithaddr(SA(&sin))) == 0)) {
1058 return EADDRNOTAVAIL;
1059 } else if (ifa) {
1060 /*
1061 * Opportunistically determine the outbound
1062 * interface that may be used; this may not
1063 * hold true if we end up using a route
1064 * going over a different interface, e.g.
1065 * when sending to a local address. This
1066 * will get updated again after sending.
1067 */
1068 IFA_LOCK(ifa);
1069 outif = ifa->ifa_ifp;
1070 IFA_UNLOCK(ifa);
1071 ifa_remref(ifa);
1072 }
1073 inp->inp_laddr = sin.sin_addr;
1074 inp->inp_last_outifp = outif;
1075
1076 return 0;
1077 }
1078
1079 __private_extern__ int
rip_connect(struct socket * so,struct sockaddr * nam,__unused struct proc * p)1080 rip_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1081 {
1082 struct inpcb *inp = sotoinpcb(so);
1083 struct sockaddr_in *addr = (struct sockaddr_in *)(void *)nam;
1084
1085 if (inp == NULL
1086 #if NECP
1087 || (necp_socket_should_use_flow_divert(inp))
1088 #endif /* NECP */
1089 ) {
1090 return inp == NULL ? EINVAL : EPROTOTYPE;
1091 }
1092 if (nam->sa_len != sizeof(*addr)) {
1093 return EINVAL;
1094 }
1095 if (TAILQ_EMPTY(&ifnet_head)) {
1096 return EADDRNOTAVAIL;
1097 }
1098 if ((addr->sin_family != AF_INET) &&
1099 (addr->sin_family != AF_IMPLINK)) {
1100 return EAFNOSUPPORT;
1101 }
1102
1103 if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
1104 so->so_flags1 |= SOF1_CONNECT_COUNTED;
1105 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected);
1106 }
1107
1108 inp->inp_faddr = addr->sin_addr;
1109 soisconnected(so);
1110
1111 return 0;
1112 }
1113
1114 __private_extern__ int
rip_shutdown(struct socket * so)1115 rip_shutdown(struct socket *so)
1116 {
1117 socantsendmore(so);
1118 return 0;
1119 }
1120
1121 __private_extern__ int
rip_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct proc * p)1122 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1123 struct mbuf *control, struct proc *p)
1124 {
1125 #pragma unused(flags, p)
1126 struct inpcb *inp = sotoinpcb(so);
1127 u_int32_t dst = INADDR_ANY;
1128 int error = 0;
1129
1130 if (inp == NULL
1131 #if NECP
1132 || (necp_socket_should_use_flow_divert(inp) && (error = EPROTOTYPE))
1133 #endif /* NECP */
1134 ) {
1135 if (inp == NULL) {
1136 error = EINVAL;
1137 } else {
1138 error = EPROTOTYPE;
1139 }
1140 goto bad;
1141 }
1142
1143 in_pcb_check_management_entitled(inp);
1144 in_pcb_check_ultra_constrained_entitled(inp);
1145
1146 so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
1147
1148 if (nam != NULL) {
1149 dst = ((struct sockaddr_in *)(void *)nam)->sin_addr.s_addr;
1150 }
1151 return rip_output(m, so, dst, control);
1152
1153 bad:
1154 VERIFY(error != 0);
1155
1156 if (m != NULL) {
1157 m_freem(m);
1158 }
1159 if (control != NULL) {
1160 m_freem(control);
1161 }
1162
1163 return error;
1164 }
1165
1166 /* note: rip_unlock is called from different protos instead of the generic socket_unlock,
1167 * it will handle the socket dealloc on last reference
1168 * */
1169 int
rip_unlock(struct socket * so,int refcount,void * debug)1170 rip_unlock(struct socket *so, int refcount, void *debug)
1171 {
1172 void *__single lr_saved;
1173 struct inpcb *inp = sotoinpcb(so);
1174
1175 if (debug == NULL) {
1176 lr_saved = __unsafe_forge_single(void *, __builtin_return_address(0));
1177 } else {
1178 lr_saved = debug;
1179 }
1180
1181 if (refcount) {
1182 if (so->so_usecount <= 0) {
1183 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s",
1184 so, so->so_usecount, solockhistory_nr(so));
1185 /* NOTREACHED */
1186 }
1187 so->so_usecount--;
1188 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
1189 /* cleanup after last reference */
1190 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
1191 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1192 if (inp->inp_state != INPCB_STATE_DEAD) {
1193 if (SOCK_CHECK_DOM(so, PF_INET6)) {
1194 in6_pcbdetach(inp);
1195 } else {
1196 in_pcbdetach(inp);
1197 }
1198 }
1199 in_pcbdispose(inp);
1200 lck_rw_done(&ripcbinfo.ipi_lock);
1201 return 0;
1202 }
1203 }
1204 so->unlock_lr[so->next_unlock_lr] = lr_saved;
1205 so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1206 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
1207 return 0;
1208 }
1209
1210 static int
1211 rip_pcblist SYSCTL_HANDLER_ARGS
1212 {
1213 #pragma unused(oidp, arg1, arg2)
1214 int error, i, n, sz;
1215 struct inpcb *inp, **inp_list;
1216 inp_gen_t gencnt;
1217 struct xinpgen xig;
1218
1219 /*
1220 * The process of preparing the TCB list is too time-consuming and
1221 * resource-intensive to repeat twice on every request.
1222 */
1223 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1224 if (req->oldptr == USER_ADDR_NULL) {
1225 n = ripcbinfo.ipi_count;
1226 req->oldidx = 2 * (sizeof xig)
1227 + (n + n / 8) * sizeof(struct xinpcb);
1228 lck_rw_done(&ripcbinfo.ipi_lock);
1229 return 0;
1230 }
1231
1232 if (req->newptr != USER_ADDR_NULL) {
1233 lck_rw_done(&ripcbinfo.ipi_lock);
1234 return EPERM;
1235 }
1236
1237 /*
1238 * OK, now we're committed to doing something.
1239 */
1240 gencnt = ripcbinfo.ipi_gencnt;
1241 sz = n = ripcbinfo.ipi_count;
1242
1243 bzero(&xig, sizeof(xig));
1244 xig.xig_len = sizeof xig;
1245 xig.xig_count = n;
1246 xig.xig_gen = gencnt;
1247 xig.xig_sogen = so_gencnt;
1248 error = SYSCTL_OUT(req, &xig, sizeof xig);
1249 if (error) {
1250 lck_rw_done(&ripcbinfo.ipi_lock);
1251 return error;
1252 }
1253 /*
1254 * We are done if there is no pcb
1255 */
1256 if (n == 0) {
1257 lck_rw_done(&ripcbinfo.ipi_lock);
1258 return 0;
1259 }
1260
1261 inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1262 if (inp_list == NULL) {
1263 lck_rw_done(&ripcbinfo.ipi_lock);
1264 return ENOMEM;
1265 }
1266
1267 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1268 inp = inp->inp_list.le_next) {
1269 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1270 inp_list[i++] = inp;
1271 }
1272 }
1273 n = i;
1274
1275 error = 0;
1276 for (i = 0; i < n; i++) {
1277 inp = inp_list[i];
1278 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1279 struct xinpcb xi;
1280
1281 bzero(&xi, sizeof(xi));
1282 xi.xi_len = sizeof xi;
1283 /* XXX should avoid extra copy */
1284 inpcb_to_compat(inp, &xi.xi_inp);
1285 if (inp->inp_socket) {
1286 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1287 }
1288 error = SYSCTL_OUT(req, &xi, sizeof xi);
1289 }
1290 }
1291 if (!error) {
1292 /*
1293 * Give the user an updated idea of our state.
1294 * If the generation differs from what we told
1295 * her before, she knows that something happened
1296 * while we were processing this request, and it
1297 * might be necessary to retry.
1298 */
1299 bzero(&xig, sizeof(xig));
1300 xig.xig_len = sizeof xig;
1301 xig.xig_gen = ripcbinfo.ipi_gencnt;
1302 xig.xig_sogen = so_gencnt;
1303 xig.xig_count = ripcbinfo.ipi_count;
1304 error = SYSCTL_OUT(req, &xig, sizeof xig);
1305 }
1306
1307 lck_rw_done(&ripcbinfo.ipi_lock);
1308 kfree_type(struct inpcb *, sz, inp_list);
1309 return error;
1310 }
1311
1312 SYSCTL_PROC(_net_inet_raw, OID_AUTO /*XXX*/, pcblist,
1313 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1314 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1315
1316 #if XNU_TARGET_OS_OSX
1317
1318 static int
1319 rip_pcblist64 SYSCTL_HANDLER_ARGS
1320 {
1321 #pragma unused(oidp, arg1, arg2)
1322 int error, i, n, sz;
1323 struct inpcb *inp, **inp_list;
1324 inp_gen_t gencnt;
1325 struct xinpgen xig;
1326
1327 /*
1328 * The process of preparing the TCB list is too time-consuming and
1329 * resource-intensive to repeat twice on every request.
1330 */
1331 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1332 if (req->oldptr == USER_ADDR_NULL) {
1333 n = ripcbinfo.ipi_count;
1334 req->oldidx = 2 * (sizeof xig)
1335 + (n + n / 8) * sizeof(struct xinpcb64);
1336 lck_rw_done(&ripcbinfo.ipi_lock);
1337 return 0;
1338 }
1339
1340 if (req->newptr != USER_ADDR_NULL) {
1341 lck_rw_done(&ripcbinfo.ipi_lock);
1342 return EPERM;
1343 }
1344
1345 /*
1346 * OK, now we're committed to doing something.
1347 */
1348 gencnt = ripcbinfo.ipi_gencnt;
1349 sz = n = ripcbinfo.ipi_count;
1350
1351 bzero(&xig, sizeof(xig));
1352 xig.xig_len = sizeof xig;
1353 xig.xig_count = n;
1354 xig.xig_gen = gencnt;
1355 xig.xig_sogen = so_gencnt;
1356 error = SYSCTL_OUT(req, &xig, sizeof xig);
1357 if (error) {
1358 lck_rw_done(&ripcbinfo.ipi_lock);
1359 return error;
1360 }
1361 /*
1362 * We are done if there is no pcb
1363 */
1364 if (n == 0) {
1365 lck_rw_done(&ripcbinfo.ipi_lock);
1366 return 0;
1367 }
1368
1369 inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1370 if (inp_list == NULL) {
1371 lck_rw_done(&ripcbinfo.ipi_lock);
1372 return ENOMEM;
1373 }
1374
1375 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1376 inp = inp->inp_list.le_next) {
1377 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1378 inp_list[i++] = inp;
1379 }
1380 }
1381 n = i;
1382
1383 error = 0;
1384 for (i = 0; i < n; i++) {
1385 inp = inp_list[i];
1386 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1387 struct xinpcb64 xi;
1388
1389 bzero(&xi, sizeof(xi));
1390 xi.xi_len = sizeof xi;
1391 inpcb_to_xinpcb64(inp, &xi);
1392 if (inp->inp_socket) {
1393 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1394 }
1395 error = SYSCTL_OUT(req, &xi, sizeof xi);
1396 }
1397 }
1398 if (!error) {
1399 /*
1400 * Give the user an updated idea of our state.
1401 * If the generation differs from what we told
1402 * her before, she knows that something happened
1403 * while we were processing this request, and it
1404 * might be necessary to retry.
1405 */
1406 bzero(&xig, sizeof(xig));
1407 xig.xig_len = sizeof xig;
1408 xig.xig_gen = ripcbinfo.ipi_gencnt;
1409 xig.xig_sogen = so_gencnt;
1410 xig.xig_count = ripcbinfo.ipi_count;
1411 error = SYSCTL_OUT(req, &xig, sizeof xig);
1412 }
1413
1414 lck_rw_done(&ripcbinfo.ipi_lock);
1415 kfree_type(struct inpcb *, sz, inp_list);
1416 return error;
1417 }
1418
1419 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist64,
1420 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1421 rip_pcblist64, "S,xinpcb64", "List of active raw IP sockets");
1422
1423 #endif /* XNU_TARGET_OS_OSX */
1424
1425
1426 static int
1427 rip_pcblist_n SYSCTL_HANDLER_ARGS
1428 {
1429 #pragma unused(oidp, arg1, arg2)
1430 int error = 0;
1431
1432 error = get_pcblist_n(IPPROTO_IP, req, &ripcbinfo);
1433
1434 return error;
1435 }
1436
1437 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist_n,
1438 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1439 rip_pcblist_n, "S,xinpcb_n", "List of active raw IP sockets");
1440
1441 struct pr_usrreqs rip_usrreqs = {
1442 .pru_abort = rip_abort,
1443 .pru_attach = rip_attach,
1444 .pru_bind = rip_bind,
1445 .pru_connect = rip_connect,
1446 .pru_control = in_control,
1447 .pru_detach = rip_detach,
1448 .pru_disconnect = rip_disconnect,
1449 .pru_peeraddr = in_getpeeraddr,
1450 .pru_send = rip_send,
1451 .pru_shutdown = rip_shutdown,
1452 .pru_sockaddr = in_getsockaddr,
1453 .pru_sosend = sosend,
1454 .pru_soreceive = soreceive,
1455 };
1456 /* DSEP Review Done pl-20051213-v02 @3253 */
1457