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