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