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