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