1 /*
2 * Copyright (c) 2000-2023 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/zalloc.h>
83
84 #include <pexpert/pexpert.h>
85
86 #include <net/if.h>
87 #include <net/net_api_stats.h>
88 #include <net/route.h>
89 #include <net/content_filter.h>
90 #include <net/sockaddr_utils.h>
91
92 #define _IP_VHL
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_tclass.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100
101 #include <netinet6/in6_pcb.h>
102
103
104 #if IPSEC
105 #include <netinet6/ipsec.h>
106 #endif /*IPSEC*/
107
108 #if DUMMYNET
109 #include <netinet/ip_dummynet.h>
110 #endif /* DUMMYNET */
111
112 int rip_detach(struct socket *);
113 int rip_abort(struct socket *);
114 int rip_disconnect(struct socket *);
115 int rip_bind(struct socket *, struct sockaddr *, struct proc *);
116 int rip_connect(struct socket *, struct sockaddr *, struct proc *);
117 int rip_shutdown(struct socket *);
118
119 struct inpcbhead ripcb;
120 struct inpcbinfo ripcbinfo;
121
122 /* control hooks for dummynet */
123 #if DUMMYNET
124 ip_dn_ctl_t *ip_dn_ctl_ptr;
125 #endif /* DUMMYNET */
126
127 /*
128 * Nominal space allocated to a raw ip socket.
129 */
130 #define RIPSNDQ 8192
131 #define RIPRCVQ 8192
132
133 static KALLOC_TYPE_DEFINE(ripzone, struct inpcb, NET_KT_DEFAULT);
134
135 /*
136 * Raw interface to IP protocol.
137 */
138
139 /*
140 * Initialize raw connection block q.
141 */
142 void
rip_init(struct protosw * pp,struct domain * dp)143 rip_init(struct protosw *pp, struct domain *dp)
144 {
145 #pragma unused(dp)
146 static int rip_initialized = 0;
147 struct inpcbinfo *pcbinfo;
148
149 VERIFY((pp->pr_flags & (PR_INITIALIZED | PR_ATTACHED)) == PR_ATTACHED);
150
151 if (rip_initialized) {
152 return;
153 }
154 rip_initialized = 1;
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 ip_out_args ipoa;
394 struct ip_moptions *imo;
395 int tos = IPTOS_UNSPEC;
396 int error = 0;
397 #if CONTENT_FILTER
398 struct m_tag *cfil_tag = NULL;
399 bool cfil_faddr_use = false;
400 uint32_t cfil_so_state_change_cnt = 0;
401 uint32_t cfil_so_options = 0;
402 int cfil_inp_flags = 0;
403 struct sockaddr *__single cfil_faddr = NULL;
404 struct sockaddr_in *__single cfil_sin;
405 u_int32_t cfil_dst = 0;
406 #endif
407
408 #if CONTENT_FILTER
409 /*
410 * If socket is subject to Content Filter and no addr is passed in,
411 * retrieve CFIL saved state from mbuf and use it if necessary.
412 */
413 if (CFIL_DGRAM_FILTERED(so) && dst == INADDR_ANY) {
414 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, &cfil_inp_flags);
415 if (cfil_tag) {
416 cfil_sin = SIN(cfil_faddr);
417 flags = (cfil_so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
418 inp_flags = cfil_inp_flags;
419 if (inp && inp->inp_faddr.s_addr == INADDR_ANY) {
420 /*
421 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
422 * the connect/disconnect logic.
423 */
424 dst = cfil_sin->sin_addr.s_addr;
425 } else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
426 (inp->inp_fport != cfil_sin->sin_port ||
427 inp->inp_faddr.s_addr != cfil_sin->sin_addr.s_addr)) {
428 /*
429 * Socket is connected but socket state and dest addr/port changed.
430 * We need to use the saved faddr and socket options.
431 */
432 cfil_faddr_use = true;
433 cfil_dst = cfil_sin->sin_addr.s_addr;
434 }
435 m_tag_free(cfil_tag);
436 }
437 }
438 #endif
439
440 if (so->so_state & SS_ISCONNECTED) {
441 if (dst != INADDR_ANY) {
442 if (m != NULL) {
443 m_freem(m);
444 }
445 if (control != NULL) {
446 m_freem(control);
447 }
448 return EISCONN;
449 }
450 dst = cfil_faddr_use ? cfil_dst : inp->inp_faddr.s_addr;
451 } else {
452 if (dst == INADDR_ANY) {
453 if (m != NULL) {
454 m_freem(m);
455 }
456 if (control != NULL) {
457 m_freem(control);
458 }
459 return ENOTCONN;
460 }
461 }
462
463 bzero(&ipoa, sizeof(ipoa));
464 ipoa.ipoa_boundif = IFSCOPE_NONE;
465 ipoa.ipoa_flags = IPOAF_SELECT_SRCIF;
466
467 int sotc = SO_TC_UNSPEC;
468 int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
469
470
471 if (control != NULL) {
472 tos = so_tos_from_control(control);
473 sotc = so_tc_from_control(control, &netsvctype);
474
475 m_freem(control);
476 control = NULL;
477 }
478 if (sotc == SO_TC_UNSPEC) {
479 sotc = so->so_traffic_class;
480 netsvctype = so->so_netsvctype;
481 }
482
483 if (inp == NULL
484 #if NECP
485 || (necp_socket_should_use_flow_divert(inp))
486 #endif /* NECP */
487 ) {
488 if (m != NULL) {
489 m_freem(m);
490 }
491 VERIFY(control == NULL);
492 return inp == NULL ? EINVAL : EPROTOTYPE;
493 }
494
495 flags |= IP_OUTARGS;
496 /* If socket was bound to an ifindex, tell ip_output about it */
497 if (inp->inp_flags & INP_BOUND_IF) {
498 ipoa.ipoa_boundif = inp->inp_boundifp->if_index;
499 ipoa.ipoa_flags |= IPOAF_BOUND_IF;
500 }
501 if (INP_NO_CELLULAR(inp)) {
502 ipoa.ipoa_flags |= IPOAF_NO_CELLULAR;
503 }
504 if (INP_NO_EXPENSIVE(inp)) {
505 ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE;
506 }
507 if (INP_NO_CONSTRAINED(inp)) {
508 ipoa.ipoa_flags |= IPOAF_NO_CONSTRAINED;
509 }
510 if (INP_AWDL_UNRESTRICTED(inp)) {
511 ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED;
512 }
513 if (INP_MANAGEMENT_ALLOWED(inp)) {
514 ipoa.ipoa_flags |= IPOAF_MANAGEMENT_ALLOWED;
515 }
516 ipoa.ipoa_sotc = sotc;
517 ipoa.ipoa_netsvctype = netsvctype;
518
519 if (inp->inp_flowhash == 0) {
520 inp_calc_flowhash(inp);
521 ASSERT(inp->inp_flowhash != 0);
522 }
523
524 /*
525 * If the user handed us a complete IP packet, use it.
526 * Otherwise, allocate an mbuf for a header and fill it in.
527 */
528 if ((inp_flags & INP_HDRINCL) == 0) {
529 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
530 m_freem(m);
531 return EMSGSIZE;
532 }
533 M_PREPEND(m, sizeof(struct ip), M_WAIT, 1);
534 if (m == NULL) {
535 return ENOBUFS;
536 }
537 ip = mtod(m, struct ip *);
538 if (tos != IPTOS_UNSPEC) {
539 ip->ip_tos = (uint8_t)(tos & IPTOS_MASK);
540 } else {
541 ip->ip_tos = inp->inp_ip_tos;
542 }
543 if (inp->inp_flags2 & INP2_DONTFRAG) {
544 ip->ip_off = IP_DF;
545 } else {
546 ip->ip_off = 0;
547 }
548 ip->ip_p = inp->inp_ip_p;
549 ip->ip_len = (uint16_t)m->m_pkthdr.len;
550 ip->ip_src = inp->inp_laddr;
551 ip->ip_dst.s_addr = dst;
552 ip->ip_ttl = inp->inp_ip_ttl;
553 } else {
554 if (m->m_pkthdr.len > IP_MAXPACKET) {
555 m_freem(m);
556 return EMSGSIZE;
557 }
558 ip = mtod(m, struct ip *);
559 /*
560 * don't allow both user specified and setsockopt options,
561 * and don't allow packet length sizes that will crash
562 */
563 if (m->m_pkthdr.len < sizeof(struct ip) ||
564 ((IP_VHL_HL(ip->ip_vhl) != (sizeof(*ip) >> 2)) && inp->inp_options) ||
565 (ip->ip_len > m->m_pkthdr.len) ||
566 (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
567 m_freem(m);
568 return EINVAL;
569 }
570 if (ip->ip_id == 0 && !(rfc6864 && IP_OFF_IS_ATOMIC(ntohs(ip->ip_off)))) {
571 ip->ip_id = ip_randomid((uint64_t)m);
572 }
573 /* XXX prevent ip_output from overwriting header fields */
574 flags |= IP_RAWOUTPUT;
575 OSAddAtomic(1, &ipstat.ips_rawout);
576 }
577
578 if (inp->inp_laddr.s_addr != INADDR_ANY) {
579 ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR;
580 }
581
582 #if NECP
583 {
584 necp_kernel_policy_id policy_id;
585 necp_kernel_policy_id skip_policy_id;
586 u_int32_t route_rule_id;
587 u_int32_t pass_flags;
588
589 /*
590 * We need a route to perform NECP route rule checks
591 */
592 if ((net_qos_policy_restricted != 0 &&
593 ROUTE_UNUSABLE(&inp->inp_route))
594 #if CONTENT_FILTER
595 || cfil_faddr_use
596 #endif
597 ) {
598 struct sockaddr_in to;
599 struct sockaddr_in from;
600 struct in_addr laddr = ip->ip_src;
601
602 ROUTE_RELEASE(&inp->inp_route);
603
604 bzero(&from, sizeof(struct sockaddr_in));
605 from.sin_family = AF_INET;
606 from.sin_len = sizeof(struct sockaddr_in);
607 from.sin_addr = laddr;
608
609 bzero(&to, sizeof(struct sockaddr_in));
610 to.sin_family = AF_INET;
611 to.sin_len = sizeof(struct sockaddr_in);
612 to.sin_addr.s_addr = ip->ip_dst.s_addr;
613
614 if ((error = in_pcbladdr(inp, (struct sockaddr *)&to,
615 &laddr, ipoa.ipoa_boundif, NULL, 1)) != 0) {
616 printf("%s in_pcbladdr(%p) error %d\n",
617 __func__, inp, error);
618 m_freem(m);
619 return error;
620 }
621
622 inp_update_necp_policy(inp, (struct sockaddr *)&from,
623 (struct sockaddr *)&to, ipoa.ipoa_boundif);
624 inp->inp_policyresult.results.qos_marking_gencount = 0;
625 }
626
627 if (!necp_socket_is_allowed_to_send_recv_v4(inp, 0, 0,
628 &ip->ip_src, &ip->ip_dst, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
629 m_freem(m);
630 return EHOSTUNREACH;
631 }
632
633 necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id, skip_policy_id, pass_flags);
634
635 if (net_qos_policy_restricted != 0) {
636 struct ifnet *rt_ifp = NULL;
637
638 if (inp->inp_route.ro_rt != NULL) {
639 rt_ifp = inp->inp_route.ro_rt->rt_ifp;
640 }
641
642 necp_socket_update_qos_marking(inp, inp->inp_route.ro_rt, route_rule_id);
643 }
644 }
645 #endif /* NECP */
646 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
647 ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED;
648 }
649 #if IPSEC
650 if (inp->inp_sp != NULL && ipsec_setsocket(m, so) != 0) {
651 m_freem(m);
652 return ENOBUFS;
653 }
654 #endif /*IPSEC*/
655
656 if (ROUTE_UNUSABLE(&inp->inp_route)) {
657 ROUTE_RELEASE(&inp->inp_route);
658 }
659
660 set_packet_service_class(m, so, sotc, 0);
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 so_update_tx_data_stats(so, 1, m->m_pkthdr.len);
1143
1144 if (nam != NULL) {
1145 dst = ((struct sockaddr_in *)(void *)nam)->sin_addr.s_addr;
1146 }
1147 return rip_output(m, so, dst, control);
1148
1149 bad:
1150 VERIFY(error != 0);
1151
1152 if (m != NULL) {
1153 m_freem(m);
1154 }
1155 if (control != NULL) {
1156 m_freem(control);
1157 }
1158
1159 return error;
1160 }
1161
1162 /* note: rip_unlock is called from different protos instead of the generic socket_unlock,
1163 * it will handle the socket dealloc on last reference
1164 * */
1165 int
rip_unlock(struct socket * so,int refcount,void * debug)1166 rip_unlock(struct socket *so, int refcount, void *debug)
1167 {
1168 void *__single lr_saved;
1169 struct inpcb *inp = sotoinpcb(so);
1170
1171 if (debug == NULL) {
1172 lr_saved = __unsafe_forge_single(void *, __builtin_return_address(0));
1173 } else {
1174 lr_saved = debug;
1175 }
1176
1177 if (refcount) {
1178 if (so->so_usecount <= 0) {
1179 panic("rip_unlock: bad refoucnt so=%p val=%x lrh= %s",
1180 so, so->so_usecount, solockhistory_nr(so));
1181 /* NOTREACHED */
1182 }
1183 so->so_usecount--;
1184 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
1185 /* cleanup after last reference */
1186 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
1187 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1188 if (inp->inp_state != INPCB_STATE_DEAD) {
1189 if (SOCK_CHECK_DOM(so, PF_INET6)) {
1190 in6_pcbdetach(inp);
1191 } else {
1192 in_pcbdetach(inp);
1193 }
1194 }
1195 in_pcbdispose(inp);
1196 lck_rw_done(&ripcbinfo.ipi_lock);
1197 return 0;
1198 }
1199 }
1200 so->unlock_lr[so->next_unlock_lr] = lr_saved;
1201 so->next_unlock_lr = (so->next_unlock_lr + 1) % SO_LCKDBG_MAX;
1202 lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx);
1203 return 0;
1204 }
1205
1206 static int
1207 rip_pcblist SYSCTL_HANDLER_ARGS
1208 {
1209 #pragma unused(oidp, arg1, arg2)
1210 int error, i, n, sz;
1211 struct inpcb *inp, **inp_list;
1212 inp_gen_t gencnt;
1213 struct xinpgen xig;
1214
1215 /*
1216 * The process of preparing the TCB list is too time-consuming and
1217 * resource-intensive to repeat twice on every request.
1218 */
1219 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1220 if (req->oldptr == USER_ADDR_NULL) {
1221 n = ripcbinfo.ipi_count;
1222 req->oldidx = 2 * (sizeof xig)
1223 + (n + n / 8) * sizeof(struct xinpcb);
1224 lck_rw_done(&ripcbinfo.ipi_lock);
1225 return 0;
1226 }
1227
1228 if (req->newptr != USER_ADDR_NULL) {
1229 lck_rw_done(&ripcbinfo.ipi_lock);
1230 return EPERM;
1231 }
1232
1233 /*
1234 * OK, now we're committed to doing something.
1235 */
1236 gencnt = ripcbinfo.ipi_gencnt;
1237 sz = n = ripcbinfo.ipi_count;
1238
1239 bzero(&xig, sizeof(xig));
1240 xig.xig_len = sizeof xig;
1241 xig.xig_count = n;
1242 xig.xig_gen = gencnt;
1243 xig.xig_sogen = so_gencnt;
1244 error = SYSCTL_OUT(req, &xig, sizeof xig);
1245 if (error) {
1246 lck_rw_done(&ripcbinfo.ipi_lock);
1247 return error;
1248 }
1249 /*
1250 * We are done if there is no pcb
1251 */
1252 if (n == 0) {
1253 lck_rw_done(&ripcbinfo.ipi_lock);
1254 return 0;
1255 }
1256
1257 inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1258 if (inp_list == NULL) {
1259 lck_rw_done(&ripcbinfo.ipi_lock);
1260 return ENOMEM;
1261 }
1262
1263 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1264 inp = inp->inp_list.le_next) {
1265 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1266 inp_list[i++] = inp;
1267 }
1268 }
1269 n = i;
1270
1271 error = 0;
1272 for (i = 0; i < n; i++) {
1273 inp = inp_list[i];
1274 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1275 struct xinpcb xi;
1276
1277 bzero(&xi, sizeof(xi));
1278 xi.xi_len = sizeof xi;
1279 /* XXX should avoid extra copy */
1280 inpcb_to_compat(inp, &xi.xi_inp);
1281 if (inp->inp_socket) {
1282 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1283 }
1284 error = SYSCTL_OUT(req, &xi, sizeof xi);
1285 }
1286 }
1287 if (!error) {
1288 /*
1289 * Give the user an updated idea of our state.
1290 * If the generation differs from what we told
1291 * her before, she knows that something happened
1292 * while we were processing this request, and it
1293 * might be necessary to retry.
1294 */
1295 bzero(&xig, sizeof(xig));
1296 xig.xig_len = sizeof xig;
1297 xig.xig_gen = ripcbinfo.ipi_gencnt;
1298 xig.xig_sogen = so_gencnt;
1299 xig.xig_count = ripcbinfo.ipi_count;
1300 error = SYSCTL_OUT(req, &xig, sizeof xig);
1301 }
1302
1303 lck_rw_done(&ripcbinfo.ipi_lock);
1304 kfree_type(struct inpcb *, sz, inp_list);
1305 return error;
1306 }
1307
1308 SYSCTL_PROC(_net_inet_raw, OID_AUTO /*XXX*/, pcblist,
1309 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1310 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1311
1312 #if XNU_TARGET_OS_OSX
1313
1314 static int
1315 rip_pcblist64 SYSCTL_HANDLER_ARGS
1316 {
1317 #pragma unused(oidp, arg1, arg2)
1318 int error, i, n, sz;
1319 struct inpcb *inp, **inp_list;
1320 inp_gen_t gencnt;
1321 struct xinpgen xig;
1322
1323 /*
1324 * The process of preparing the TCB list is too time-consuming and
1325 * resource-intensive to repeat twice on every request.
1326 */
1327 lck_rw_lock_exclusive(&ripcbinfo.ipi_lock);
1328 if (req->oldptr == USER_ADDR_NULL) {
1329 n = ripcbinfo.ipi_count;
1330 req->oldidx = 2 * (sizeof xig)
1331 + (n + n / 8) * sizeof(struct xinpcb64);
1332 lck_rw_done(&ripcbinfo.ipi_lock);
1333 return 0;
1334 }
1335
1336 if (req->newptr != USER_ADDR_NULL) {
1337 lck_rw_done(&ripcbinfo.ipi_lock);
1338 return EPERM;
1339 }
1340
1341 /*
1342 * OK, now we're committed to doing something.
1343 */
1344 gencnt = ripcbinfo.ipi_gencnt;
1345 sz = n = ripcbinfo.ipi_count;
1346
1347 bzero(&xig, sizeof(xig));
1348 xig.xig_len = sizeof xig;
1349 xig.xig_count = n;
1350 xig.xig_gen = gencnt;
1351 xig.xig_sogen = so_gencnt;
1352 error = SYSCTL_OUT(req, &xig, sizeof xig);
1353 if (error) {
1354 lck_rw_done(&ripcbinfo.ipi_lock);
1355 return error;
1356 }
1357 /*
1358 * We are done if there is no pcb
1359 */
1360 if (n == 0) {
1361 lck_rw_done(&ripcbinfo.ipi_lock);
1362 return 0;
1363 }
1364
1365 inp_list = kalloc_type(struct inpcb *, n, Z_WAITOK);
1366 if (inp_list == NULL) {
1367 lck_rw_done(&ripcbinfo.ipi_lock);
1368 return ENOMEM;
1369 }
1370
1371 for (inp = ripcbinfo.ipi_listhead->lh_first, i = 0; inp && i < n;
1372 inp = inp->inp_list.le_next) {
1373 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1374 inp_list[i++] = inp;
1375 }
1376 }
1377 n = i;
1378
1379 error = 0;
1380 for (i = 0; i < n; i++) {
1381 inp = inp_list[i];
1382 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
1383 struct xinpcb64 xi;
1384
1385 bzero(&xi, sizeof(xi));
1386 xi.xi_len = sizeof xi;
1387 inpcb_to_xinpcb64(inp, &xi);
1388 if (inp->inp_socket) {
1389 sotoxsocket64(inp->inp_socket, &xi.xi_socket);
1390 }
1391 error = SYSCTL_OUT(req, &xi, sizeof xi);
1392 }
1393 }
1394 if (!error) {
1395 /*
1396 * Give the user an updated idea of our state.
1397 * If the generation differs from what we told
1398 * her before, she knows that something happened
1399 * while we were processing this request, and it
1400 * might be necessary to retry.
1401 */
1402 bzero(&xig, sizeof(xig));
1403 xig.xig_len = sizeof xig;
1404 xig.xig_gen = ripcbinfo.ipi_gencnt;
1405 xig.xig_sogen = so_gencnt;
1406 xig.xig_count = ripcbinfo.ipi_count;
1407 error = SYSCTL_OUT(req, &xig, sizeof xig);
1408 }
1409
1410 lck_rw_done(&ripcbinfo.ipi_lock);
1411 kfree_type(struct inpcb *, sz, inp_list);
1412 return error;
1413 }
1414
1415 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist64,
1416 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1417 rip_pcblist64, "S,xinpcb64", "List of active raw IP sockets");
1418
1419 #endif /* XNU_TARGET_OS_OSX */
1420
1421
1422 static int
1423 rip_pcblist_n SYSCTL_HANDLER_ARGS
1424 {
1425 #pragma unused(oidp, arg1, arg2)
1426 int error = 0;
1427
1428 error = get_pcblist_n(IPPROTO_IP, req, &ripcbinfo);
1429
1430 return error;
1431 }
1432
1433 SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist_n,
1434 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0,
1435 rip_pcblist_n, "S,xinpcb_n", "List of active raw IP sockets");
1436
1437 struct pr_usrreqs rip_usrreqs = {
1438 .pru_abort = rip_abort,
1439 .pru_attach = rip_attach,
1440 .pru_bind = rip_bind,
1441 .pru_connect = rip_connect,
1442 .pru_control = in_control,
1443 .pru_detach = rip_detach,
1444 .pru_disconnect = rip_disconnect,
1445 .pru_peeraddr = in_getpeeraddr,
1446 .pru_send = rip_send,
1447 .pru_shutdown = rip_shutdown,
1448 .pru_sockaddr = in_getsockaddr,
1449 .pru_sosend = sosend,
1450 .pru_soreceive = soreceive,
1451 };
1452 /* DSEP Review Done pl-20051213-v02 @3253 */
1453