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