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