]>
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@ |
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 | * | |
91447636 | 60 | * $FreeBSD: src/sys/netinet/ip_divert.c,v 1.98 2004/08/17 22:05:54 andre Exp $ |
1c79356b A |
61 | */ |
62 | ||
2d21ac55 | 63 | #if !INET |
1c79356b A |
64 | #error "IPDIVERT requires INET." |
65 | #endif | |
66 | ||
67 | #include <sys/param.h> | |
9bccf70c | 68 | #include <sys/kernel.h> |
1c79356b A |
69 | #include <sys/malloc.h> |
70 | #include <sys/mbuf.h> | |
71 | #include <sys/socket.h> | |
91447636 | 72 | #include <sys/domain.h> |
1c79356b A |
73 | #include <sys/protosw.h> |
74 | #include <sys/socketvar.h> | |
9bccf70c | 75 | #include <sys/sysctl.h> |
1c79356b A |
76 | #include <sys/systm.h> |
77 | #include <sys/proc.h> | |
78 | ||
b0d623f7 | 79 | #include <machine/endian.h> |
1c79356b A |
80 | |
81 | #include <net/if.h> | |
82 | #include <net/route.h> | |
2d21ac55 | 83 | #include <net/kpi_protocol.h> |
1c79356b A |
84 | |
85 | #include <netinet/in.h> | |
86 | #include <netinet/in_systm.h> | |
87 | #include <netinet/ip.h> | |
88 | #include <netinet/in_pcb.h> | |
89 | #include <netinet/in_var.h> | |
90 | #include <netinet/ip_var.h> | |
91447636 A |
91 | #include <netinet/ip_fw.h> |
92 | #include <netinet/ip_divert.h> | |
93 | ||
94 | #include <kern/zalloc.h> | |
2d21ac55 | 95 | #include <libkern/OSAtomic.h> |
1c79356b A |
96 | |
97 | /* | |
98 | * Divert sockets | |
99 | */ | |
100 | ||
101 | /* | |
102 | * Allocate enough space to hold a full IP packet | |
103 | */ | |
104 | #define DIVSNDQ (65536 + 100) | |
105 | #define DIVRCVQ (65536 + 100) | |
106 | ||
1c79356b | 107 | /* |
91447636 A |
108 | * Divert sockets work in conjunction with ipfw, see the divert(4) |
109 | * manpage for features. | |
110 | * Internally, packets selected by ipfw in ip_input() or ip_output(), | |
111 | * and never diverted before, are passed to the input queue of the | |
112 | * divert socket with a given 'divert_port' number (as specified in | |
113 | * the matching ipfw rule), and they are tagged with a 16 bit cookie | |
114 | * (representing the rule number of the matching ipfw rule), which | |
115 | * is passed to process reading from the socket. | |
1c79356b | 116 | * |
91447636 A |
117 | * Packets written to the divert socket are again tagged with a cookie |
118 | * (usually the same as above) and a destination address. | |
119 | * If the destination address is INADDR_ANY then the packet is | |
120 | * treated as outgoing and sent to ip_output(), otherwise it is | |
121 | * treated as incoming and sent to ip_input(). | |
122 | * In both cases, the packet is tagged with the cookie. | |
9bccf70c | 123 | * |
91447636 A |
124 | * On reinjection, processing in ip_input() and ip_output() |
125 | * will be exactly the same as for the original packet, except that | |
126 | * ipfw processing will start at the rule number after the one | |
127 | * written in the cookie (so, tagging a packet with a cookie of 0 | |
128 | * will cause it to be effectively considered as a standard packet). | |
1c79356b | 129 | */ |
1c79356b A |
130 | |
131 | /* Internal variables */ | |
1c79356b A |
132 | static struct inpcbhead divcb; |
133 | static struct inpcbinfo divcbinfo; | |
134 | ||
b0d623f7 A |
135 | static u_int32_t div_sendspace = DIVSNDQ; /* XXX sysctl ? */ |
136 | static u_int32_t div_recvspace = DIVRCVQ; /* XXX sysctl ? */ | |
1c79356b A |
137 | |
138 | /* Optimization: have this preinitialized */ | |
2d21ac55 | 139 | static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET, 0, { 0 }, { 0,0,0,0,0,0,0,0 } }; |
1c79356b A |
140 | |
141 | /* Internal functions */ | |
1c79356b | 142 | static int div_output(struct socket *so, |
39236c6e | 143 | struct mbuf *m, struct sockaddr_in *addr, struct mbuf *control); |
1c79356b | 144 | |
91447636 | 145 | extern int load_ipfw(void); |
1c79356b A |
146 | /* |
147 | * Initialize divert connection block queue. | |
148 | */ | |
149 | void | |
39236c6e | 150 | div_init(struct protosw *pp, struct domain *dp) |
1c79356b | 151 | { |
39236c6e A |
152 | #pragma unused(dp) |
153 | static int div_initialized = 0; | |
91447636 | 154 | struct inpcbinfo *pcbinfo; |
39236c6e A |
155 | |
156 | VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED); | |
157 | ||
158 | if (div_initialized) | |
159 | return; | |
160 | div_initialized = 1; | |
161 | ||
1c79356b | 162 | LIST_INIT(&divcb); |
39236c6e | 163 | divcbinfo.ipi_listhead = &divcb; |
1c79356b A |
164 | /* |
165 | * XXX We don't use the hash list for divert IP, but it's easier | |
166 | * to allocate a one entry hash list than it is to check all | |
39236c6e | 167 | * over the place for ipi_hashbase == NULL. |
1c79356b | 168 | */ |
39236c6e A |
169 | divcbinfo.ipi_hashbase = hashinit(1, M_PCB, &divcbinfo.ipi_hashmask); |
170 | divcbinfo.ipi_porthashbase = hashinit(1, M_PCB, &divcbinfo.ipi_porthashmask); | |
fe8ab488 | 171 | divcbinfo.ipi_zone = zinit(sizeof(struct inpcb),(512 * sizeof(struct inpcb)), |
1c79356b | 172 | 4096, "divzone"); |
91447636 A |
173 | pcbinfo = &divcbinfo; |
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 | ||
179 | pcbinfo->ipi_lock_grp = lck_grp_alloc_init("divcb", pcbinfo->ipi_lock_grp_attr); | |
1c79356b | 180 | |
91447636 A |
181 | /* |
182 | * allocate the lock attribute for divert pcb mutexes | |
183 | */ | |
39236c6e | 184 | pcbinfo->ipi_lock_attr = lck_attr_alloc_init(); |
91447636 | 185 | |
39236c6e A |
186 | if ((pcbinfo->ipi_lock = lck_rw_alloc_init(pcbinfo->ipi_lock_grp, |
187 | pcbinfo->ipi_lock_attr)) == NULL) { | |
188 | panic("%s: unable to allocate PCB lock\n", __func__); | |
189 | /* NOTREACHED */ | |
190 | } | |
191 | ||
192 | in_pcbinfo_attach(&divcbinfo); | |
91447636 | 193 | |
2d21ac55 | 194 | #if IPFIREWALL |
91447636 A |
195 | if (!IPFW_LOADED) { |
196 | load_ipfw(); | |
197 | } | |
2d21ac55 | 198 | #endif |
1c79356b A |
199 | } |
200 | ||
201 | /* | |
9bccf70c A |
202 | * IPPROTO_DIVERT is not a real IP protocol; don't allow any packets |
203 | * with that protocol number to enter the system from the outside. | |
1c79356b A |
204 | */ |
205 | void | |
91447636 | 206 | div_input(struct mbuf *m, __unused int off) |
9bccf70c | 207 | { |
b0d623f7 | 208 | OSAddAtomic(1, &ipstat.ips_noproto); |
9bccf70c A |
209 | m_freem(m); |
210 | } | |
211 | ||
212 | /* | |
213 | * Divert a packet by passing it up to the divert socket at port 'port'. | |
214 | * | |
215 | * Setup generic address and protocol structures for div_input routine, | |
216 | * then pass them along with mbuf chain. | |
217 | */ | |
218 | void | |
91447636 | 219 | divert_packet(struct mbuf *m, int incoming, int port, int rule) |
1c79356b A |
220 | { |
221 | struct ip *ip; | |
222 | struct inpcb *inp; | |
223 | struct socket *sa; | |
9bccf70c | 224 | u_int16_t nport; |
1c79356b A |
225 | |
226 | /* Sanity check */ | |
9bccf70c A |
227 | KASSERT(port != 0, ("%s: port=0", __FUNCTION__)); |
228 | ||
91447636 | 229 | divsrc.sin_port = rule; /* record matching rule */ |
1c79356b A |
230 | |
231 | /* Assure header */ | |
232 | if (m->m_len < sizeof(struct ip) && | |
233 | (m = m_pullup(m, sizeof(struct ip))) == 0) { | |
234 | return; | |
235 | } | |
236 | ip = mtod(m, struct ip *); | |
237 | ||
1c79356b | 238 | /* |
9bccf70c | 239 | * Record receive interface address, if any. |
1c79356b A |
240 | * But only for incoming packets. |
241 | */ | |
242 | divsrc.sin_addr.s_addr = 0; | |
9bccf70c | 243 | if (incoming) { |
1c79356b A |
244 | struct ifaddr *ifa; |
245 | ||
1c79356b | 246 | /* Sanity check */ |
9bccf70c | 247 | KASSERT((m->m_flags & M_PKTHDR), ("%s: !PKTHDR", __FUNCTION__)); |
1c79356b A |
248 | |
249 | /* Find IP address for receive interface */ | |
91447636 | 250 | ifnet_lock_shared(m->m_pkthdr.rcvif); |
9bccf70c | 251 | TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { |
6d2010ae A |
252 | IFA_LOCK(ifa); |
253 | if (ifa->ifa_addr->sa_family != AF_INET) { | |
254 | IFA_UNLOCK(ifa); | |
1c79356b | 255 | continue; |
6d2010ae | 256 | } |
1c79356b | 257 | divsrc.sin_addr = |
316670eb | 258 | ((struct sockaddr_in *)(void *) ifa->ifa_addr)->sin_addr; |
6d2010ae | 259 | IFA_UNLOCK(ifa); |
1c79356b A |
260 | break; |
261 | } | |
91447636 | 262 | ifnet_lock_done(m->m_pkthdr.rcvif); |
1c79356b A |
263 | } |
264 | /* | |
265 | * Record the incoming interface name whenever we have one. | |
266 | */ | |
267 | bzero(&divsrc.sin_zero, sizeof(divsrc.sin_zero)); | |
268 | if (m->m_pkthdr.rcvif) { | |
269 | /* | |
270 | * Hide the actual interface name in there in the | |
271 | * sin_zero array. XXX This needs to be moved to a | |
272 | * different sockaddr type for divert, e.g. | |
273 | * sockaddr_div with multiple fields like | |
274 | * sockaddr_dl. Presently we have only 7 bytes | |
275 | * but that will do for now as most interfaces | |
276 | * are 4 or less + 2 or less bytes for unit. | |
277 | * There is probably a faster way of doing this, | |
278 | * possibly taking it from the sockaddr_dl on the iface. | |
279 | * This solves the problem of a P2P link and a LAN interface | |
280 | * having the same address, which can result in the wrong | |
281 | * interface being assigned to the packet when fed back | |
282 | * into the divert socket. Theoretically if the daemon saves | |
283 | * and re-uses the sockaddr_in as suggested in the man pages, | |
284 | * this iface name will come along for the ride. | |
285 | * (see div_output for the other half of this.) | |
286 | */ | |
287 | snprintf(divsrc.sin_zero, sizeof(divsrc.sin_zero), | |
39236c6e | 288 | "%s", if_name(m->m_pkthdr.rcvif)); |
1c79356b A |
289 | } |
290 | ||
291 | /* Put packet on socket queue, if any */ | |
292 | sa = NULL; | |
9bccf70c | 293 | nport = htons((u_int16_t)port); |
39236c6e | 294 | lck_rw_lock_shared(divcbinfo.ipi_lock); |
9bccf70c A |
295 | LIST_FOREACH(inp, &divcb, inp_list) { |
296 | if (inp->inp_lport == nport) | |
1c79356b A |
297 | sa = inp->inp_socket; |
298 | } | |
1c79356b | 299 | if (sa) { |
91447636 A |
300 | int error = 0; |
301 | ||
302 | socket_lock(sa, 1); | |
1c79356b | 303 | if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc, |
91447636 | 304 | m, (struct mbuf *)0, &error) != 0) |
1c79356b | 305 | sorwakeup(sa); |
91447636 | 306 | socket_unlock(sa, 1); |
1c79356b A |
307 | } else { |
308 | m_freem(m); | |
b0d623f7 A |
309 | OSAddAtomic(1, &ipstat.ips_noproto); |
310 | OSAddAtomic(-1, &ipstat.ips_delivered); | |
1c79356b | 311 | } |
39236c6e | 312 | lck_rw_done(divcbinfo.ipi_lock); |
1c79356b A |
313 | } |
314 | ||
315 | /* | |
316 | * Deliver packet back into the IP processing machinery. | |
317 | * | |
318 | * If no address specified, or address is 0.0.0.0, send to ip_output(); | |
319 | * otherwise, send to ip_input() and mark as having been received on | |
320 | * the interface with that address. | |
91447636 | 321 | * ###LOCK called in inet_proto mutex when from div_send. |
1c79356b A |
322 | */ |
323 | static int | |
39236c6e | 324 | div_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin, |
2d21ac55 | 325 | struct mbuf *control) |
1c79356b | 326 | { |
2d21ac55 A |
327 | struct inpcb *const inp = sotoinpcb(so); |
328 | struct ip *const ip = mtod(m, struct ip *); | |
1c79356b | 329 | int error = 0; |
39037602 | 330 | int sotc = SO_TC_UNSPEC; |
1c79356b | 331 | |
d41d1dae | 332 | if (control != NULL) { |
39037602 A |
333 | int ignored; |
334 | ||
335 | (void) so_tc_from_control(contro, &sotc, &ignored); | |
1c79356b | 336 | |
d41d1dae | 337 | m_freem(control); /* XXX */ |
39236c6e | 338 | control = NULL; |
d41d1dae | 339 | } |
39037602 A |
340 | if (sotc == SO_TC_UNSPEC) |
341 | sotc = so->so_traffic_class; | |
342 | ||
1c79356b A |
343 | /* Loopback avoidance and state recovery */ |
344 | if (sin) { | |
91447636 A |
345 | struct m_tag *mtag; |
346 | struct divert_tag *dt; | |
1c79356b A |
347 | int len = 0; |
348 | char *c = sin->sin_zero; | |
349 | ||
6d2010ae A |
350 | mtag = m_tag_create(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DIVERT, |
351 | sizeof(struct divert_tag), M_NOWAIT, m); | |
91447636 A |
352 | if (mtag == NULL) { |
353 | error = ENOBUFS; | |
354 | goto cantsend; | |
355 | } | |
356 | dt = (struct divert_tag *)(mtag+1); | |
357 | dt->info = 0; | |
358 | dt->cookie = sin->sin_port; | |
359 | m_tag_prepend(m, mtag); | |
1c79356b A |
360 | |
361 | /* | |
362 | * Find receive interface with the given name or IP address. | |
363 | * The name is user supplied data so don't trust it's size or | |
364 | * that it is zero terminated. The name has priority. | |
365 | * We are presently assuming that the sockaddr_in | |
366 | * has not been replaced by a sockaddr_div, so we limit it | |
367 | * to 16 bytes in total. the name is stuffed (if it exists) | |
368 | * in the sin_zero[] field. | |
369 | */ | |
370 | while (*c++ && (len++ < sizeof(sin->sin_zero))); | |
371 | if ((len > 0) && (len < sizeof(sin->sin_zero))) | |
372 | m->m_pkthdr.rcvif = ifunit(sin->sin_zero); | |
1c79356b A |
373 | } |
374 | ||
375 | /* Reinject packet into the system as incoming or outgoing */ | |
376 | if (!sin || sin->sin_addr.s_addr == 0) { | |
316670eb | 377 | struct ip_out_args ipoa = |
39037602 A |
378 | { IFSCOPE_NONE, { 0 }, IPOAF_SELECT_SRCIF, 0, SO_TC_UNSPEC, |
379 | _NET_SERVICE_TYPE_UNSPEC }; | |
b0d623f7 | 380 | struct route ro; |
6d2010ae | 381 | struct ip_moptions *imo; |
c910b4d9 | 382 | |
1c79356b A |
383 | /* |
384 | * Don't allow both user specified and setsockopt options, | |
385 | * and don't allow packet length sizes that will crash | |
386 | */ | |
387 | if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || | |
388 | ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { | |
389 | error = EINVAL; | |
390 | goto cantsend; | |
391 | } | |
392 | ||
393 | /* Convert fields to host order for ip_output() */ | |
b0d623f7 | 394 | #if BYTE_ORDER != BIG_ENDIAN |
1c79356b A |
395 | NTOHS(ip->ip_len); |
396 | NTOHS(ip->ip_off); | |
b0d623f7 A |
397 | #endif |
398 | ||
399 | OSAddAtomic(1, &ipstat.ips_rawout); | |
400 | /* Copy the cached route and take an extra reference */ | |
401 | inp_route_copyout(inp, &ro); | |
1c79356b | 402 | |
39037602 A |
403 | if (sotc != SO_TC_UNSPEC) { |
404 | ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED; | |
405 | ipoa.ipoa_sotc = sotc; | |
406 | } | |
407 | set_packet_service_class(m, so, sotc, 0); | |
d1ecb069 | 408 | |
6d2010ae A |
409 | imo = inp->inp_moptions; |
410 | if (imo != NULL) | |
411 | IMO_ADDREF(imo); | |
91447636 | 412 | socket_unlock(so, 0); |
2d21ac55 A |
413 | #if CONFIG_MACF_NET |
414 | mac_mbuf_label_associate_inpcb(inp, m); | |
593a1d5f | 415 | #endif |
b0d623f7 A |
416 | /* Send packet to output processing */ |
417 | error = ip_output(m, inp->inp_options, &ro, | |
1c79356b | 418 | (so->so_options & SO_DONTROUTE) | |
c910b4d9 | 419 | IP_ALLOWBROADCAST | IP_RAWOUTPUT | IP_OUTARGS, |
6d2010ae | 420 | imo, &ipoa); |
b0d623f7 | 421 | |
91447636 | 422 | socket_lock(so, 0); |
6d2010ae A |
423 | if (imo != NULL) |
424 | IMO_REMREF(imo); | |
b0d623f7 A |
425 | /* Synchronize cached PCB route */ |
426 | inp_route_copyin(inp, &ro); | |
1c79356b A |
427 | } else { |
428 | struct ifaddr *ifa; | |
429 | ||
430 | /* If no luck with the name above. check by IP address. */ | |
431 | if (m->m_pkthdr.rcvif == NULL) { | |
39236c6e | 432 | struct sockaddr_in _sin; |
1c79356b | 433 | /* |
39236c6e A |
434 | * Make sure there are no distractions for |
435 | * ifa_ifwithaddr; use sanitized version. | |
1c79356b | 436 | */ |
39236c6e A |
437 | bzero(&_sin, sizeof (_sin)); |
438 | _sin.sin_family = AF_INET; | |
439 | _sin.sin_len = sizeof (struct sockaddr_in); | |
440 | _sin.sin_addr.s_addr = sin->sin_addr.s_addr; | |
441 | if (!(ifa = ifa_ifwithaddr(SA(&_sin)))) { | |
1c79356b A |
442 | error = EADDRNOTAVAIL; |
443 | goto cantsend; | |
444 | } | |
445 | m->m_pkthdr.rcvif = ifa->ifa_ifp; | |
6d2010ae | 446 | IFA_REMREF(ifa); |
91447636 | 447 | } |
2d21ac55 A |
448 | #if CONFIG_MACF_NET |
449 | mac_mbuf_label_associate_socket(so, m); | |
450 | #endif | |
1c79356b | 451 | /* Send packet to input processing */ |
91447636 | 452 | proto_inject(PF_INET, m); |
1c79356b A |
453 | } |
454 | ||
1c79356b A |
455 | return error; |
456 | ||
457 | cantsend: | |
1c79356b A |
458 | m_freem(m); |
459 | return error; | |
460 | } | |
461 | ||
462 | static int | |
463 | div_attach(struct socket *so, int proto, struct proc *p) | |
464 | { | |
465 | struct inpcb *inp; | |
91447636 A |
466 | int error; |
467 | ||
1c79356b A |
468 | |
469 | inp = sotoinpcb(so); | |
470 | if (inp) | |
471 | panic("div_attach"); | |
b0d623f7 | 472 | if ((error = proc_suser(p)) != 0) |
1c79356b A |
473 | return error; |
474 | ||
9bccf70c A |
475 | error = soreserve(so, div_sendspace, div_recvspace); |
476 | if (error) | |
477 | return error; | |
1c79356b | 478 | error = in_pcballoc(so, &divcbinfo, p); |
1c79356b A |
479 | if (error) |
480 | return error; | |
481 | inp = (struct inpcb *)so->so_pcb; | |
482 | inp->inp_ip_p = proto; | |
9bccf70c A |
483 | inp->inp_vflag |= INP_IPV4; |
484 | inp->inp_flags |= INP_HDRINCL; | |
1c79356b A |
485 | /* The socket is always "connected" because |
486 | we always know "where" to send the packet */ | |
487 | so->so_state |= SS_ISCONNECTED; | |
91447636 A |
488 | |
489 | #ifdef MORE_DICVLOCK_DEBUG | |
39236c6e A |
490 | printf("div_attach: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x\n", |
491 | (uint64_t)VM_KERNEL_ADDRPERM(so), | |
492 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb), | |
493 | (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so)->inpcb_mtx)), | |
494 | so->so_usecount); | |
91447636 | 495 | #endif |
1c79356b A |
496 | return 0; |
497 | } | |
498 | ||
499 | static int | |
500 | div_detach(struct socket *so) | |
501 | { | |
502 | struct inpcb *inp; | |
503 | ||
91447636 | 504 | #ifdef MORE_DICVLOCK_DEBUG |
39236c6e A |
505 | printf("div_detach: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x\n", |
506 | (uint64_t)VM_KERNEL_ADDRPERM(so), | |
507 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb), | |
508 | (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so)->inpcb_mtx)), | |
509 | so->so_usecount); | |
91447636 | 510 | #endif |
1c79356b A |
511 | inp = sotoinpcb(so); |
512 | if (inp == 0) | |
2d21ac55 | 513 | panic("div_detach: so=%p null inp\n", so); |
1c79356b | 514 | in_pcbdetach(inp); |
91447636 | 515 | inp->inp_state = INPCB_STATE_DEAD; |
1c79356b A |
516 | return 0; |
517 | } | |
518 | ||
519 | static int | |
520 | div_abort(struct socket *so) | |
521 | { | |
522 | soisdisconnected(so); | |
523 | return div_detach(so); | |
524 | } | |
525 | ||
526 | static int | |
527 | div_disconnect(struct socket *so) | |
528 | { | |
529 | if ((so->so_state & SS_ISCONNECTED) == 0) | |
530 | return ENOTCONN; | |
531 | return div_abort(so); | |
532 | } | |
533 | ||
534 | static int | |
535 | div_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
536 | { | |
537 | struct inpcb *inp; | |
1c79356b A |
538 | int error; |
539 | ||
1c79356b | 540 | inp = sotoinpcb(so); |
9bccf70c A |
541 | /* in_pcbbind assumes that the socket is a sockaddr_in |
542 | * and in_pcbbind requires a valid address. Since divert | |
543 | * sockets don't we need to make sure the address is | |
544 | * filled in properly. | |
545 | * XXX -- divert should not be abusing in_pcbind | |
546 | * and should probably have its own family. | |
547 | */ | |
548 | if (nam->sa_family != AF_INET) { | |
549 | error = EAFNOSUPPORT; | |
550 | } else { | |
316670eb | 551 | ((struct sockaddr_in *)(void *)nam)->sin_addr.s_addr = INADDR_ANY; |
9bccf70c A |
552 | error = in_pcbbind(inp, nam, p); |
553 | } | |
9bccf70c | 554 | return error; |
1c79356b A |
555 | } |
556 | ||
557 | static int | |
558 | div_shutdown(struct socket *so) | |
559 | { | |
560 | socantsendmore(so); | |
561 | return 0; | |
562 | } | |
563 | ||
564 | static int | |
91447636 A |
565 | div_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam, |
566 | struct mbuf *control, __unused struct proc *p) | |
1c79356b A |
567 | { |
568 | /* Packet must have a header (but that's about it) */ | |
9bccf70c | 569 | if (m->m_len < sizeof (struct ip) && |
1c79356b | 570 | (m = m_pullup(m, sizeof (struct ip))) == 0) { |
b0d623f7 | 571 | OSAddAtomic(1, &ipstat.ips_toosmall); |
1c79356b A |
572 | m_freem(m); |
573 | return EINVAL; | |
574 | } | |
575 | ||
576 | /* Send packet */ | |
39236c6e | 577 | return div_output(so, m, SIN(nam), control); |
1c79356b A |
578 | } |
579 | ||
b0d623f7 | 580 | #if 0 |
9bccf70c A |
581 | static int |
582 | div_pcblist SYSCTL_HANDLER_ARGS | |
583 | { | |
b0d623f7 | 584 | #pragma unused(oidp, arg1, arg2) |
91447636 | 585 | int error, i, n; |
9bccf70c A |
586 | struct inpcb *inp, **inp_list; |
587 | inp_gen_t gencnt; | |
588 | struct xinpgen xig; | |
589 | ||
590 | /* | |
591 | * The process of preparing the TCB list is too time-consuming and | |
592 | * resource-intensive to repeat twice on every request. | |
593 | */ | |
39236c6e | 594 | lck_rw_lock_exclusive(divcbinfo.ipi_lock); |
91447636 | 595 | if (req->oldptr == USER_ADDR_NULL) { |
9bccf70c A |
596 | n = divcbinfo.ipi_count; |
597 | req->oldidx = 2 * (sizeof xig) | |
598 | + (n + n/8) * sizeof(struct xinpcb); | |
39236c6e | 599 | lck_rw_done(divcbinfo.ipi_lock); |
9bccf70c A |
600 | return 0; |
601 | } | |
602 | ||
91447636 | 603 | if (req->newptr != USER_ADDR_NULL) { |
39236c6e | 604 | lck_rw_done(divcbinfo.ipi_lock); |
9bccf70c | 605 | return EPERM; |
91447636 | 606 | } |
9bccf70c A |
607 | |
608 | /* | |
609 | * OK, now we're committed to doing something. | |
610 | */ | |
9bccf70c A |
611 | gencnt = divcbinfo.ipi_gencnt; |
612 | n = divcbinfo.ipi_count; | |
9bccf70c | 613 | |
3a60a9f5 | 614 | bzero(&xig, sizeof(xig)); |
9bccf70c A |
615 | xig.xig_len = sizeof xig; |
616 | xig.xig_count = n; | |
617 | xig.xig_gen = gencnt; | |
618 | xig.xig_sogen = so_gencnt; | |
619 | error = SYSCTL_OUT(req, &xig, sizeof xig); | |
91447636 | 620 | if (error) { |
39236c6e | 621 | lck_rw_done(divcbinfo.ipi_lock); |
9bccf70c | 622 | return error; |
91447636 | 623 | } |
9bccf70c A |
624 | |
625 | inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK); | |
91447636 | 626 | if (inp_list == 0) { |
39236c6e | 627 | lck_rw_done(divcbinfo.ipi_lock); |
9bccf70c | 628 | return ENOMEM; |
91447636 | 629 | } |
9bccf70c | 630 | |
39236c6e | 631 | for (inp = LIST_FIRST(divcbinfo.ipi_listhead), i = 0; inp && i < n; |
9bccf70c A |
632 | inp = LIST_NEXT(inp, inp_list)) { |
633 | #ifdef __APPLE__ | |
91447636 | 634 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) |
9bccf70c A |
635 | #else |
636 | if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) | |
637 | #endif | |
638 | inp_list[i++] = inp; | |
639 | } | |
9bccf70c A |
640 | n = i; |
641 | ||
642 | error = 0; | |
643 | for (i = 0; i < n; i++) { | |
644 | inp = inp_list[i]; | |
91447636 | 645 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) { |
9bccf70c | 646 | struct xinpcb xi; |
3a60a9f5 A |
647 | |
648 | bzero(&xi, sizeof(xi)); | |
9bccf70c A |
649 | xi.xi_len = sizeof xi; |
650 | /* XXX should avoid extra copy */ | |
91447636 | 651 | inpcb_to_compat(inp, &xi.xi_inp); |
9bccf70c A |
652 | if (inp->inp_socket) |
653 | sotoxsocket(inp->inp_socket, &xi.xi_socket); | |
654 | error = SYSCTL_OUT(req, &xi, sizeof xi); | |
655 | } | |
656 | } | |
657 | if (!error) { | |
658 | /* | |
659 | * Give the user an updated idea of our state. | |
660 | * If the generation differs from what we told | |
661 | * her before, she knows that something happened | |
662 | * while we were processing this request, and it | |
663 | * might be necessary to retry. | |
664 | */ | |
3a60a9f5 A |
665 | bzero(&xig, sizeof(xig)); |
666 | xig.xig_len = sizeof xig; | |
9bccf70c A |
667 | xig.xig_gen = divcbinfo.ipi_gencnt; |
668 | xig.xig_sogen = so_gencnt; | |
669 | xig.xig_count = divcbinfo.ipi_count; | |
9bccf70c A |
670 | error = SYSCTL_OUT(req, &xig, sizeof xig); |
671 | } | |
672 | FREE(inp_list, M_TEMP); | |
39236c6e | 673 | lck_rw_done(divcbinfo.ipi_lock); |
9bccf70c A |
674 | return error; |
675 | } | |
b0d623f7 | 676 | #endif |
9bccf70c | 677 | |
91447636 | 678 | __private_extern__ int |
b0d623f7 A |
679 | div_lock(struct socket *so, int refcount, void *lr) |
680 | { | |
681 | void *lr_saved; | |
682 | ||
683 | if (lr == NULL) | |
684 | lr_saved = __builtin_return_address(0); | |
685 | else | |
686 | lr_saved = lr; | |
687 | ||
91447636 | 688 | #ifdef MORE_DICVLOCK_DEBUG |
39236c6e A |
689 | printf("div_lock: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x " |
690 | "lr=0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(so), | |
691 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb), so->so_pcb ? | |
692 | (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so)->inpcb_mtx)) : NULL, | |
693 | so->so_usecount, (uint64_t)VM_KERNEL_ADDRPERM(lr_saved)); | |
9bccf70c | 694 | #endif |
91447636 | 695 | if (so->so_pcb) { |
6d2010ae | 696 | lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); |
91447636 | 697 | } else { |
b0d623f7 A |
698 | panic("div_lock: so=%p NO PCB! lr=%p lrh= lrh= %s\n", |
699 | so, lr_saved, solockhistory_nr(so)); | |
700 | /* NOTREACHED */ | |
91447636 | 701 | } |
b0d623f7 A |
702 | |
703 | if (so->so_usecount < 0) { | |
704 | panic("div_lock: so=%p so_pcb=%p lr=%p ref=%x lrh= %s\n", | |
705 | so, so->so_pcb, lr_saved, so->so_usecount, | |
706 | solockhistory_nr(so)); | |
707 | /* NOTREACHED */ | |
708 | } | |
709 | ||
91447636 A |
710 | if (refcount) |
711 | so->so_usecount++; | |
b0d623f7 | 712 | so->lock_lr[so->next_lock_lr] = lr_saved; |
0c530ab8 | 713 | so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
714 | |
715 | return (0); | |
716 | } | |
717 | ||
718 | __private_extern__ int | |
b0d623f7 | 719 | div_unlock(struct socket *so, int refcount, void *lr) |
91447636 | 720 | { |
b0d623f7 | 721 | void *lr_saved; |
91447636 | 722 | lck_mtx_t * mutex_held; |
b0d623f7 | 723 | struct inpcb *inp = sotoinpcb(so); |
0c530ab8 | 724 | |
b0d623f7 A |
725 | if (lr == NULL) |
726 | lr_saved = __builtin_return_address(0); | |
727 | else | |
728 | lr_saved = lr; | |
0c530ab8 | 729 | |
91447636 | 730 | #ifdef MORE_DICVLOCK_DEBUG |
39236c6e A |
731 | printf("div_unlock: so=0x%llx sopcb=0x%llx lock=0x%llx ref=%x " |
732 | "lr=0x%llx\n", (uint64_t)VM_KERNEL_ADDRPERM(so), | |
733 | (uint64_t)VM_KERNEL_ADDRPERM(so->so_pcb), so->so_pcb ? | |
734 | (uint64_t)VM_KERNEL_ADDRPERM(&(sotoinpcb(so)->inpcb_mtx)) : NULL, | |
b0d623f7 | 735 | so->so_usecount, lr_saved); |
91447636 A |
736 | #endif |
737 | if (refcount) | |
738 | so->so_usecount--; | |
b0d623f7 A |
739 | |
740 | if (so->so_usecount < 0) { | |
741 | panic("div_unlock: so=%p usecount=%x lrh= %s\n", | |
742 | so, so->so_usecount, solockhistory_nr(so)); | |
743 | /* NOTREACHED */ | |
744 | } | |
91447636 | 745 | if (so->so_pcb == NULL) { |
b0d623f7 A |
746 | panic("div_unlock: so=%p NO PCB usecount=%x lr=%p lrh= %s\n", |
747 | so, so->so_usecount, lr_saved, solockhistory_nr(so)); | |
748 | /* NOTREACHED */ | |
91447636 | 749 | } |
6d2010ae | 750 | mutex_held = &((struct inpcb *)so->so_pcb)->inpcb_mtx; |
91447636 A |
751 | |
752 | if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) { | |
39236c6e | 753 | lck_rw_lock_exclusive(divcbinfo.ipi_lock); |
b0d623f7 A |
754 | if (inp->inp_state != INPCB_STATE_DEAD) |
755 | in_pcbdetach(inp); | |
91447636 | 756 | in_pcbdispose(inp); |
39236c6e | 757 | lck_rw_done(divcbinfo.ipi_lock); |
91447636 A |
758 | return (0); |
759 | } | |
760 | lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED); | |
b0d623f7 | 761 | so->unlock_lr[so->next_unlock_lr] = lr_saved; |
0c530ab8 | 762 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
91447636 | 763 | lck_mtx_unlock(mutex_held); |
91447636 A |
764 | return (0); |
765 | } | |
766 | ||
767 | __private_extern__ lck_mtx_t * | |
768 | div_getlock(struct socket *so, __unused int locktype) | |
769 | { | |
770 | struct inpcb *inpcb = (struct inpcb *)so->so_pcb; | |
771 | ||
772 | if (so->so_pcb) { | |
773 | if (so->so_usecount < 0) | |
b0d623f7 A |
774 | panic("div_getlock: so=%p usecount=%x lrh= %s\n", |
775 | so, so->so_usecount, solockhistory_nr(so)); | |
6d2010ae | 776 | return(&inpcb->inpcb_mtx); |
91447636 | 777 | } else { |
b0d623f7 A |
778 | panic("div_getlock: so=%p NULL NO PCB lrh= %s\n", |
779 | so, solockhistory_nr(so)); | |
91447636 A |
780 | return (so->so_proto->pr_domain->dom_mtx); |
781 | } | |
782 | } | |
783 | ||
1c79356b | 784 | struct pr_usrreqs div_usrreqs = { |
39236c6e A |
785 | .pru_abort = div_abort, |
786 | .pru_attach = div_attach, | |
787 | .pru_bind = div_bind, | |
788 | .pru_control = in_control, | |
789 | .pru_detach = div_detach, | |
790 | .pru_disconnect = div_disconnect, | |
791 | .pru_peeraddr = in_getpeeraddr, | |
792 | .pru_send = div_send, | |
793 | .pru_shutdown = div_shutdown, | |
794 | .pru_sockaddr = in_getsockaddr, | |
795 | .pru_sosend = sosend, | |
796 | .pru_soreceive = soreceive, | |
1c79356b | 797 | }; |
91447636 | 798 |