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