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