]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/ip_divert.c
xnu-792.21.3.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 *
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
129static struct inpcbhead divcb;
130static struct inpcbinfo divcbinfo;
131
132static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */
133static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */
134
135/* Optimization: have this preinitialized */
91447636 136static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET, };
1c79356b
A
137
138/* Internal functions */
1c79356b
A
139static int div_output(struct socket *so,
140 struct mbuf *m, struct sockaddr *addr, struct mbuf *control);
141
91447636 142extern int load_ipfw(void);
1c79356b
A
143/*
144 * Initialize divert connection block queue.
145 */
146void
147div_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();
21362eb3 173 lck_attr_setdefault(pcbinfo->mtx_attr);
91447636
A
174
175 if ((pcbinfo->mtx = lck_rw_alloc_init(pcbinfo->mtx_grp, pcbinfo->mtx_attr)) == NULL)
176 return; /* pretty much dead if this fails... */
177
178 if (!IPFW_LOADED) {
179 load_ipfw();
180 }
1c79356b
A
181}
182
183/*
9bccf70c
A
184 * IPPROTO_DIVERT is not a real IP protocol; don't allow any packets
185 * with that protocol number to enter the system from the outside.
1c79356b
A
186 */
187void
91447636 188div_input(struct mbuf *m, __unused int off)
9bccf70c
A
189{
190 ipstat.ips_noproto++;
191 m_freem(m);
192}
193
194/*
195 * Divert a packet by passing it up to the divert socket at port 'port'.
196 *
197 * Setup generic address and protocol structures for div_input routine,
198 * then pass them along with mbuf chain.
91447636 199 * ###LOCK called in ip_mutex from ip_output/ip_input
9bccf70c
A
200 */
201void
91447636 202divert_packet(struct mbuf *m, int incoming, int port, int rule)
1c79356b
A
203{
204 struct ip *ip;
205 struct inpcb *inp;
206 struct socket *sa;
9bccf70c 207 u_int16_t nport;
1c79356b
A
208
209 /* Sanity check */
9bccf70c
A
210 KASSERT(port != 0, ("%s: port=0", __FUNCTION__));
211
91447636 212 divsrc.sin_port = rule; /* record matching rule */
1c79356b
A
213
214 /* Assure header */
215 if (m->m_len < sizeof(struct ip) &&
216 (m = m_pullup(m, sizeof(struct ip))) == 0) {
217 return;
218 }
219 ip = mtod(m, struct ip *);
220
1c79356b 221 /*
9bccf70c 222 * Record receive interface address, if any.
1c79356b
A
223 * But only for incoming packets.
224 */
225 divsrc.sin_addr.s_addr = 0;
9bccf70c 226 if (incoming) {
1c79356b
A
227 struct ifaddr *ifa;
228
1c79356b 229 /* Sanity check */
9bccf70c 230 KASSERT((m->m_flags & M_PKTHDR), ("%s: !PKTHDR", __FUNCTION__));
1c79356b
A
231
232 /* Find IP address for receive interface */
91447636 233 ifnet_lock_shared(m->m_pkthdr.rcvif);
9bccf70c 234 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
1c79356b
A
235 if (ifa->ifa_addr == NULL)
236 continue;
237 if (ifa->ifa_addr->sa_family != AF_INET)
238 continue;
239 divsrc.sin_addr =
240 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
241 break;
242 }
91447636 243 ifnet_lock_done(m->m_pkthdr.rcvif);
1c79356b
A
244 }
245 /*
246 * Record the incoming interface name whenever we have one.
247 */
248 bzero(&divsrc.sin_zero, sizeof(divsrc.sin_zero));
249 if (m->m_pkthdr.rcvif) {
250 /*
251 * Hide the actual interface name in there in the
252 * sin_zero array. XXX This needs to be moved to a
253 * different sockaddr type for divert, e.g.
254 * sockaddr_div with multiple fields like
255 * sockaddr_dl. Presently we have only 7 bytes
256 * but that will do for now as most interfaces
257 * are 4 or less + 2 or less bytes for unit.
258 * There is probably a faster way of doing this,
259 * possibly taking it from the sockaddr_dl on the iface.
260 * This solves the problem of a P2P link and a LAN interface
261 * having the same address, which can result in the wrong
262 * interface being assigned to the packet when fed back
263 * into the divert socket. Theoretically if the daemon saves
264 * and re-uses the sockaddr_in as suggested in the man pages,
265 * this iface name will come along for the ride.
266 * (see div_output for the other half of this.)
267 */
268 snprintf(divsrc.sin_zero, sizeof(divsrc.sin_zero),
269 "%s%d", m->m_pkthdr.rcvif->if_name,
270 m->m_pkthdr.rcvif->if_unit);
271 }
272
273 /* Put packet on socket queue, if any */
274 sa = NULL;
9bccf70c 275 nport = htons((u_int16_t)port);
91447636 276 lck_rw_lock_shared(divcbinfo.mtx);
9bccf70c
A
277 LIST_FOREACH(inp, &divcb, inp_list) {
278 if (inp->inp_lport == nport)
1c79356b
A
279 sa = inp->inp_socket;
280 }
1c79356b 281 if (sa) {
91447636
A
282 int error = 0;
283
284 socket_lock(sa, 1);
1c79356b 285 if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc,
91447636 286 m, (struct mbuf *)0, &error) != 0)
1c79356b 287 sorwakeup(sa);
91447636 288 socket_unlock(sa, 1);
1c79356b
A
289 } else {
290 m_freem(m);
291 ipstat.ips_noproto++;
292 ipstat.ips_delivered--;
293 }
91447636 294 lck_rw_done(divcbinfo.mtx);
1c79356b
A
295}
296
297/*
298 * Deliver packet back into the IP processing machinery.
299 *
300 * If no address specified, or address is 0.0.0.0, send to ip_output();
301 * otherwise, send to ip_input() and mark as having been received on
302 * the interface with that address.
91447636 303 * ###LOCK called in inet_proto mutex when from div_send.
1c79356b
A
304 */
305static int
306div_output(so, m, addr, control)
307 struct socket *so;
308 register struct mbuf *m;
309 struct sockaddr *addr;
310 struct mbuf *control;
311{
312 register struct inpcb *const inp = sotoinpcb(so);
313 register struct ip *const ip = mtod(m, struct ip *);
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) {
354 /*
355 * Don't allow both user specified and setsockopt options,
356 * and don't allow packet length sizes that will crash
357 */
358 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
359 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
360 error = EINVAL;
361 goto cantsend;
362 }
363
364 /* Convert fields to host order for ip_output() */
365 NTOHS(ip->ip_len);
366 NTOHS(ip->ip_off);
367
368 /* Send packet to output processing */
369 ipstat.ips_rawout++; /* XXX */
91447636
A
370 socket_unlock(so, 0);
371 error = ip_output(m,
372 inp->inp_options, &inp->inp_route,
1c79356b 373 (so->so_options & SO_DONTROUTE) |
9bccf70c
A
374 IP_ALLOWBROADCAST | IP_RAWOUTPUT,
375 inp->inp_moptions);
91447636 376 socket_lock(so, 0);
1c79356b
A
377 } else {
378 struct ifaddr *ifa;
379
380 /* If no luck with the name above. check by IP address. */
381 if (m->m_pkthdr.rcvif == NULL) {
382 /*
383 * Make sure there are no distractions
384 * for ifa_ifwithaddr. Clear the port and the ifname.
385 * Maybe zap all 8 bytes at once using a 64bit write?
386 */
387 bzero(sin->sin_zero, sizeof(sin->sin_zero));
388 /* *((u_int64_t *)sin->sin_zero) = 0; */ /* XXX ?? */
389 sin->sin_port = 0;
390 if (!(ifa = ifa_ifwithaddr((struct sockaddr *) sin))) {
391 error = EADDRNOTAVAIL;
392 goto cantsend;
393 }
394 m->m_pkthdr.rcvif = ifa->ifa_ifp;
91447636
A
395 ifafree(ifa);
396 }
397
398 if ((~IF_HWASSIST_CSUM_FLAGS(m->m_pkthdr.rcvif->if_hwassist) &
399 m->m_pkthdr.csum_flags) == 0) {
400 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
401 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
402 }
403 m->m_pkthdr.csum_flags |=
404 CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
405 CSUM_IP_CHECKED | CSUM_IP_VALID;
406 m->m_pkthdr.csum_data = 0xffff;
407 }
408 else if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
409 int hlen;
410
411#ifdef _IP_VHL
412 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
413#else
414 hlen = ip->ip_hl << 2;
415#endif
416 in_delayed_cksum(m);
417 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
418 ip->ip_sum = in_cksum(m, hlen);
1c79356b
A
419 }
420
421 /* Send packet to input processing */
91447636 422 proto_inject(PF_INET, m);
1c79356b
A
423 }
424
1c79356b
A
425 return error;
426
427cantsend:
1c79356b
A
428 m_freem(m);
429 return error;
430}
431
432static int
433div_attach(struct socket *so, int proto, struct proc *p)
434{
435 struct inpcb *inp;
91447636
A
436 int error;
437
1c79356b
A
438
439 inp = sotoinpcb(so);
440 if (inp)
441 panic("div_attach");
91447636 442 if (p && (error = proc_suser(p)) != 0)
1c79356b
A
443 return error;
444
9bccf70c
A
445 error = soreserve(so, div_sendspace, div_recvspace);
446 if (error)
447 return error;
1c79356b 448 error = in_pcballoc(so, &divcbinfo, p);
1c79356b
A
449 if (error)
450 return error;
451 inp = (struct inpcb *)so->so_pcb;
452 inp->inp_ip_p = proto;
9bccf70c
A
453 inp->inp_vflag |= INP_IPV4;
454 inp->inp_flags |= INP_HDRINCL;
1c79356b
A
455 /* The socket is always "connected" because
456 we always know "where" to send the packet */
457 so->so_state |= SS_ISCONNECTED;
91447636
A
458
459#ifdef MORE_DICVLOCK_DEBUG
460 printf("div_attach: so=%x sopcb=%x lock=%x ref=%x\n",
461 so, so->so_pcb, ((struct inpcb *)so->so_pcb)->inpcb_mtx, so->so_usecount);
462#endif
1c79356b
A
463 return 0;
464}
465
466static int
467div_detach(struct socket *so)
468{
469 struct inpcb *inp;
470
91447636
A
471#ifdef MORE_DICVLOCK_DEBUG
472 printf("div_detach: so=%x sopcb=%x lock=%x ref=%x\n",
473 so, so->so_pcb, ((struct inpcb *)so->so_pcb)->inpcb_mtx, so->so_usecount);
474#endif
1c79356b
A
475 inp = sotoinpcb(so);
476 if (inp == 0)
91447636 477 panic("div_detach: so=%x null inp\n", so);
1c79356b 478 in_pcbdetach(inp);
91447636 479 inp->inp_state = INPCB_STATE_DEAD;
1c79356b
A
480 return 0;
481}
482
483static int
484div_abort(struct socket *so)
485{
486 soisdisconnected(so);
487 return div_detach(so);
488}
489
490static int
491div_disconnect(struct socket *so)
492{
493 if ((so->so_state & SS_ISCONNECTED) == 0)
494 return ENOTCONN;
495 return div_abort(so);
496}
497
498static int
499div_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
500{
501 struct inpcb *inp;
1c79356b
A
502 int error;
503
1c79356b 504 inp = sotoinpcb(so);
9bccf70c
A
505 /* in_pcbbind assumes that the socket is a sockaddr_in
506 * and in_pcbbind requires a valid address. Since divert
507 * sockets don't we need to make sure the address is
508 * filled in properly.
509 * XXX -- divert should not be abusing in_pcbind
510 * and should probably have its own family.
511 */
512 if (nam->sa_family != AF_INET) {
513 error = EAFNOSUPPORT;
514 } else {
515 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY;
516 error = in_pcbbind(inp, nam, p);
517 }
9bccf70c 518 return error;
1c79356b
A
519}
520
521static int
522div_shutdown(struct socket *so)
523{
524 socantsendmore(so);
525 return 0;
526}
527
528static int
91447636
A
529div_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam,
530 struct mbuf *control, __unused struct proc *p)
1c79356b
A
531{
532 /* Packet must have a header (but that's about it) */
9bccf70c 533 if (m->m_len < sizeof (struct ip) &&
1c79356b
A
534 (m = m_pullup(m, sizeof (struct ip))) == 0) {
535 ipstat.ips_toosmall++;
536 m_freem(m);
537 return EINVAL;
538 }
539
540 /* Send packet */
541 return div_output(so, m, nam, control);
542}
543
9bccf70c
A
544static int
545div_pcblist SYSCTL_HANDLER_ARGS
546{
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}
638
91447636
A
639__private_extern__ int
640div_lock(struct socket *so, int refcount, int lr)
641 {
642 int lr_saved;
21362eb3
A
643#ifdef __ppc__
644 if (lr == 0) {
645 __asm__ volatile("mflr %0" : "=r" (lr_saved));
646 }
91447636 647 else lr_saved = lr;
21362eb3 648#endif
91447636
A
649
650#ifdef MORE_DICVLOCK_DEBUG
651 printf("div_lock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
652 so,
653 so->so_pcb,
654 so->so_pcb ? ((struct inpcb *)so->so_pcb)->inpcb_mtx : 0,
655 so->so_usecount,
656 lr_saved);
9bccf70c 657#endif
91447636
A
658 if (so->so_pcb) {
659 lck_mtx_lock(((struct inpcb *)so->so_pcb)->inpcb_mtx);
660 } else {
661 panic("div_lock: so=%x NO PCB! lr=%x\n", so, lr_saved);
662 lck_mtx_lock(so->so_proto->pr_domain->dom_mtx);
663 }
664
665 if (so->so_usecount < 0)
666 panic("div_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
667 so, so->so_pcb, lr_saved, so->so_usecount);
668
669 if (refcount)
670 so->so_usecount++;
21362eb3 671 so->reserved3 = (void *)lr_saved;
91447636
A
672
673 return (0);
674}
675
676__private_extern__ int
677div_unlock(struct socket *so, int refcount, int lr)
678{
679 int lr_saved;
680 lck_mtx_t * mutex_held;
681 struct inpcb *inp = sotoinpcb(so);
21362eb3
A
682#ifdef __ppc__
683 if (lr == 0) {
684 __asm__ volatile("mflr %0" : "=r" (lr_saved));
685 }
91447636 686 else lr_saved = lr;
21362eb3 687#endif
91447636
A
688
689#ifdef MORE_DICVLOCK_DEBUG
690 printf("div_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
691 so,
692 so->so_pcb,
693 so->so_pcb ? ((struct inpcb *)so->so_pcb)->inpcb_mtx : 0,
694 so->so_usecount,
695 lr_saved);
696#endif
697 if (refcount)
698 so->so_usecount--;
699
700 if (so->so_usecount < 0)
701 panic("div_unlock: so=%x usecount=%x\n", so, so->so_usecount);
702 if (so->so_pcb == NULL) {
703 panic("div_unlock: so=%x NO PCB usecount=%x lr=%x\n", so, so->so_usecount, lr_saved);
704 mutex_held = so->so_proto->pr_domain->dom_mtx;
705 } else {
706 mutex_held = ((struct inpcb *)so->so_pcb)->inpcb_mtx;
707 }
708
709 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
710 lck_rw_lock_exclusive(divcbinfo.mtx);
711 in_pcbdispose(inp);
712 lck_rw_done(divcbinfo.mtx);
713 return (0);
714 }
715 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
716 lck_mtx_unlock(mutex_held);
21362eb3 717 so->reserved4 = (void *)lr_saved;
91447636
A
718 return (0);
719}
720
721__private_extern__ lck_mtx_t *
722div_getlock(struct socket *so, __unused int locktype)
723{
724 struct inpcb *inpcb = (struct inpcb *)so->so_pcb;
725
726 if (so->so_pcb) {
727 if (so->so_usecount < 0)
728 panic("div_getlock: so=%x usecount=%x\n", so, so->so_usecount);
729 return(inpcb->inpcb_mtx);
730 } else {
731 panic("div_getlock: so=%x NULL so_pcb\n", so);
732 return (so->so_proto->pr_domain->dom_mtx);
733 }
734}
735
9bccf70c 736
1c79356b
A
737struct pr_usrreqs div_usrreqs = {
738 div_abort, pru_accept_notsupp, div_attach, div_bind,
739 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach,
740 div_disconnect, pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
741 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown,
91447636 742 in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
1c79356b 743};
91447636 744