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