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