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