]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_divert.c
e92d7e6a35bb9b61b1d09cbdf0357987563d1f98
[apple/xnu.git] / bsd / netinet / ip_divert.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
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 *
54 * $FreeBSD: src/sys/netinet/ip_divert.c,v 1.98 2004/08/17 22:05:54 andre Exp $
55 */
56
57 #ifndef INET
58 #error "IPDIVERT requires INET."
59 #endif
60
61 #include <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/socket.h>
66 #include <sys/domain.h>
67 #include <sys/protosw.h>
68 #include <sys/socketvar.h>
69 #include <sys/sysctl.h>
70 #include <sys/systm.h>
71 #include <sys/proc.h>
72
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>
83 #include <netinet/ip_fw.h>
84 #include <netinet/ip_divert.h>
85
86 #include <kern/zalloc.h>
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
98 /*
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.
107 *
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.
114 *
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).
120 */
121
122 /* Internal variables */
123 static struct inpcbhead divcb;
124 static struct inpcbinfo divcbinfo;
125
126 static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */
127 static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */
128
129 /* Optimization: have this preinitialized */
130 static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET, };
131
132 /* Internal functions */
133 static int div_output(struct socket *so,
134 struct mbuf *m, struct sockaddr *addr, struct mbuf *control);
135
136 extern int load_ipfw(void);
137 /*
138 * Initialize divert connection block queue.
139 */
140 void
141 div_init(void)
142 {
143 struct inpcbinfo *pcbinfo;
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");
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();
160
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 }
175 }
176
177 /*
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.
180 */
181 void
182 div_input(struct mbuf *m, __unused int off)
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.
193 * ###LOCK called in ip_mutex from ip_output/ip_input
194 */
195 void
196 divert_packet(struct mbuf *m, int incoming, int port, int rule)
197 {
198 struct ip *ip;
199 struct inpcb *inp;
200 struct socket *sa;
201 u_int16_t nport;
202
203 /* Sanity check */
204 KASSERT(port != 0, ("%s: port=0", __FUNCTION__));
205
206 divsrc.sin_port = rule; /* record matching rule */
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
215 /*
216 * Record receive interface address, if any.
217 * But only for incoming packets.
218 */
219 divsrc.sin_addr.s_addr = 0;
220 if (incoming) {
221 struct ifaddr *ifa;
222
223 /* Sanity check */
224 KASSERT((m->m_flags & M_PKTHDR), ("%s: !PKTHDR", __FUNCTION__));
225
226 /* Find IP address for receive interface */
227 ifnet_lock_shared(m->m_pkthdr.rcvif);
228 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
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 }
237 ifnet_lock_done(m->m_pkthdr.rcvif);
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;
269 nport = htons((u_int16_t)port);
270 lck_rw_lock_shared(divcbinfo.mtx);
271 LIST_FOREACH(inp, &divcb, inp_list) {
272 if (inp->inp_lport == nport)
273 sa = inp->inp_socket;
274 }
275 if (sa) {
276 int error = 0;
277
278 socket_lock(sa, 1);
279 if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc,
280 m, (struct mbuf *)0, &error) != 0)
281 sorwakeup(sa);
282 socket_unlock(sa, 1);
283 } else {
284 m_freem(m);
285 ipstat.ips_noproto++;
286 ipstat.ips_delivered--;
287 }
288 lck_rw_done(divcbinfo.mtx);
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.
297 * ###LOCK called in inet_proto mutex when from div_send.
298 */
299 static int
300 div_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) {
316 struct m_tag *mtag;
317 struct divert_tag *dt;
318 int len = 0;
319 char *c = sin->sin_zero;
320
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);
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);
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 */
364 socket_unlock(so, 0);
365 error = ip_output(m,
366 inp->inp_options, &inp->inp_route,
367 (so->so_options & SO_DONTROUTE) |
368 IP_ALLOWBROADCAST | IP_RAWOUTPUT,
369 inp->inp_moptions);
370 socket_lock(so, 0);
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;
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);
413 }
414
415 /* Send packet to input processing */
416 proto_inject(PF_INET, m);
417 }
418
419 return error;
420
421 cantsend:
422 m_freem(m);
423 return error;
424 }
425
426 static int
427 div_attach(struct socket *so, int proto, struct proc *p)
428 {
429 struct inpcb *inp;
430 int error;
431
432
433 inp = sotoinpcb(so);
434 if (inp)
435 panic("div_attach");
436 if (p && (error = proc_suser(p)) != 0)
437 return error;
438
439 error = soreserve(so, div_sendspace, div_recvspace);
440 if (error)
441 return error;
442 error = in_pcballoc(so, &divcbinfo, p);
443 if (error)
444 return error;
445 inp = (struct inpcb *)so->so_pcb;
446 inp->inp_ip_p = proto;
447 inp->inp_vflag |= INP_IPV4;
448 inp->inp_flags |= INP_HDRINCL;
449 /* The socket is always "connected" because
450 we always know "where" to send the packet */
451 so->so_state |= SS_ISCONNECTED;
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
457 return 0;
458 }
459
460 static int
461 div_detach(struct socket *so)
462 {
463 struct inpcb *inp;
464
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
469 inp = sotoinpcb(so);
470 if (inp == 0)
471 panic("div_detach: so=%x null inp\n", so);
472 in_pcbdetach(inp);
473 inp->inp_state = INPCB_STATE_DEAD;
474 return 0;
475 }
476
477 static int
478 div_abort(struct socket *so)
479 {
480 soisdisconnected(so);
481 return div_detach(so);
482 }
483
484 static int
485 div_disconnect(struct socket *so)
486 {
487 if ((so->so_state & SS_ISCONNECTED) == 0)
488 return ENOTCONN;
489 return div_abort(so);
490 }
491
492 static int
493 div_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
494 {
495 struct inpcb *inp;
496 int error;
497
498 inp = sotoinpcb(so);
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 }
512 return error;
513 }
514
515 static int
516 div_shutdown(struct socket *so)
517 {
518 socantsendmore(so);
519 return 0;
520 }
521
522 static int
523 div_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam,
524 struct mbuf *control, __unused struct proc *p)
525 {
526 /* Packet must have a header (but that's about it) */
527 if (m->m_len < sizeof (struct ip) &&
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
538 static int
539 div_pcblist SYSCTL_HANDLER_ARGS
540 {
541 int error, i, n;
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 */
550 lck_rw_lock_exclusive(divcbinfo.mtx);
551 if (req->oldptr == USER_ADDR_NULL) {
552 n = divcbinfo.ipi_count;
553 req->oldidx = 2 * (sizeof xig)
554 + (n + n/8) * sizeof(struct xinpcb);
555 lck_rw_done(divcbinfo.mtx);
556 return 0;
557 }
558
559 if (req->newptr != USER_ADDR_NULL) {
560 lck_rw_done(divcbinfo.mtx);
561 return EPERM;
562 }
563
564 /*
565 * OK, now we're committed to doing something.
566 */
567 gencnt = divcbinfo.ipi_gencnt;
568 n = divcbinfo.ipi_count;
569
570 xig.xig_len = sizeof xig;
571 xig.xig_count = n;
572 xig.xig_gen = gencnt;
573 xig.xig_sogen = so_gencnt;
574 error = SYSCTL_OUT(req, &xig, sizeof xig);
575 if (error) {
576 lck_rw_done(divcbinfo.mtx);
577 return error;
578 }
579
580 inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK);
581 if (inp_list == 0) {
582 lck_rw_done(divcbinfo.mtx);
583 return ENOMEM;
584 }
585
586 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n;
587 inp = LIST_NEXT(inp, inp_list)) {
588 #ifdef __APPLE__
589 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD)
590 #else
591 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
592 #endif
593 inp_list[i++] = inp;
594 }
595 n = i;
596
597 error = 0;
598 for (i = 0; i < n; i++) {
599 inp = inp_list[i];
600 if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) {
601 struct xinpcb xi;
602 xi.xi_len = sizeof xi;
603 /* XXX should avoid extra copy */
604 inpcb_to_compat(inp, &xi.xi_inp);
605 if (inp->inp_socket)
606 sotoxsocket(inp->inp_socket, &xi.xi_socket);
607 error = SYSCTL_OUT(req, &xi, sizeof xi);
608 }
609 }
610 if (!error) {
611 /*
612 * Give the user an updated idea of our state.
613 * If the generation differs from what we told
614 * her before, she knows that something happened
615 * while we were processing this request, and it
616 * might be necessary to retry.
617 */
618 xig.xig_gen = divcbinfo.ipi_gencnt;
619 xig.xig_sogen = so_gencnt;
620 xig.xig_count = divcbinfo.ipi_count;
621 error = SYSCTL_OUT(req, &xig, sizeof xig);
622 }
623 FREE(inp_list, M_TEMP);
624 lck_rw_done(divcbinfo.mtx);
625 return error;
626 }
627
628 __private_extern__ int
629 div_lock(struct socket *so, int refcount, int lr)
630 {
631 int lr_saved;
632 #ifdef __ppc__
633 if (lr == 0) {
634 __asm__ volatile("mflr %0" : "=r" (lr_saved));
635 }
636 else lr_saved = lr;
637 #endif
638
639 #ifdef MORE_DICVLOCK_DEBUG
640 printf("div_lock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
641 so,
642 so->so_pcb,
643 so->so_pcb ? ((struct inpcb *)so->so_pcb)->inpcb_mtx : 0,
644 so->so_usecount,
645 lr_saved);
646 #endif
647 if (so->so_pcb) {
648 lck_mtx_lock(((struct inpcb *)so->so_pcb)->inpcb_mtx);
649 } else {
650 panic("div_lock: so=%x NO PCB! lr=%x\n", so, lr_saved);
651 lck_mtx_lock(so->so_proto->pr_domain->dom_mtx);
652 }
653
654 if (so->so_usecount < 0)
655 panic("div_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
656 so, so->so_pcb, lr_saved, so->so_usecount);
657
658 if (refcount)
659 so->so_usecount++;
660 so->reserved3 = (void *)lr_saved;
661
662 return (0);
663 }
664
665 __private_extern__ int
666 div_unlock(struct socket *so, int refcount, int lr)
667 {
668 int lr_saved;
669 lck_mtx_t * mutex_held;
670 struct inpcb *inp = sotoinpcb(so);
671 #ifdef __ppc__
672 if (lr == 0) {
673 __asm__ volatile("mflr %0" : "=r" (lr_saved));
674 }
675 else lr_saved = lr;
676 #endif
677
678 #ifdef MORE_DICVLOCK_DEBUG
679 printf("div_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
680 so,
681 so->so_pcb,
682 so->so_pcb ? ((struct inpcb *)so->so_pcb)->inpcb_mtx : 0,
683 so->so_usecount,
684 lr_saved);
685 #endif
686 if (refcount)
687 so->so_usecount--;
688
689 if (so->so_usecount < 0)
690 panic("div_unlock: so=%x usecount=%x\n", so, so->so_usecount);
691 if (so->so_pcb == NULL) {
692 panic("div_unlock: so=%x NO PCB usecount=%x lr=%x\n", so, so->so_usecount, lr_saved);
693 mutex_held = so->so_proto->pr_domain->dom_mtx;
694 } else {
695 mutex_held = ((struct inpcb *)so->so_pcb)->inpcb_mtx;
696 }
697
698 if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) {
699 lck_rw_lock_exclusive(divcbinfo.mtx);
700 in_pcbdispose(inp);
701 lck_rw_done(divcbinfo.mtx);
702 return (0);
703 }
704 lck_mtx_assert(mutex_held, LCK_MTX_ASSERT_OWNED);
705 lck_mtx_unlock(mutex_held);
706 so->reserved4 = (void *)lr_saved;
707 return (0);
708 }
709
710 __private_extern__ lck_mtx_t *
711 div_getlock(struct socket *so, __unused int locktype)
712 {
713 struct inpcb *inpcb = (struct inpcb *)so->so_pcb;
714
715 if (so->so_pcb) {
716 if (so->so_usecount < 0)
717 panic("div_getlock: so=%x usecount=%x\n", so, so->so_usecount);
718 return(inpcb->inpcb_mtx);
719 } else {
720 panic("div_getlock: so=%x NULL so_pcb\n", so);
721 return (so->so_proto->pr_domain->dom_mtx);
722 }
723 }
724
725
726 struct pr_usrreqs div_usrreqs = {
727 div_abort, pru_accept_notsupp, div_attach, div_bind,
728 pru_connect_notsupp, pru_connect2_notsupp, in_control, div_detach,
729 div_disconnect, pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
730 pru_rcvoob_notsupp, div_send, pru_sense_null, div_shutdown,
731 in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
732 };
733