1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32 #if !defined(KLD_MODULE)
33 #include "opt_inet.h"
34 #include "opt_ipfw.h"
35 #include "opt_mac.h"
36 #ifndef INET
37 #error "IPDIVERT requires INET."
38 #endif
39 #ifndef IPFIREWALL
40 #error "IPDIVERT requires IPFIREWALL"
41 #endif
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mac.h>
49 #include <sys/mbuf.h>
50 #include <sys/module.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sx.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60
61 #include <vm/uma.h>
62
63 #include <net/if.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_divert.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_fw.h>
75
76 /*
77 * Divert sockets
78 */
79
80 /*
81 * Allocate enough space to hold a full IP packet
82 */
83 #define DIVSNDQ (65536 + 100)
84 #define DIVRCVQ (65536 + 100)
85
86 /*
87 * Divert sockets work in conjunction with ipfw, see the divert(4)
88 * manpage for features.
89 * Internally, packets selected by ipfw in ip_input() or ip_output(),
90 * and never diverted before, are passed to the input queue of the
91 * divert socket with a given 'divert_port' number (as specified in
92 * the matching ipfw rule), and they are tagged with a 16 bit cookie
93 * (representing the rule number of the matching ipfw rule), which
94 * is passed to process reading from the socket.
95 *
96 * Packets written to the divert socket are again tagged with a cookie
97 * (usually the same as above) and a destination address.
98 * If the destination address is INADDR_ANY then the packet is
99 * treated as outgoing and sent to ip_output(), otherwise it is
100 * treated as incoming and sent to ip_input().
101 * In both cases, the packet is tagged with the cookie.
102 *
103 * On reinjection, processing in ip_input() and ip_output()
104 * will be exactly the same as for the original packet, except that
105 * ipfw processing will start at the rule number after the one
106 * written in the cookie (so, tagging a packet with a cookie of 0
107 * will cause it to be effectively considered as a standard packet).
108 */
109
110 /* Internal variables. */
111 static struct inpcbhead divcb;
112 static struct inpcbinfo divcbinfo;
113
114 static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */
115 static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */
116
117 /*
118 * Initialize divert connection block queue.
119 */
120 static void
121 div_zone_change(void *tag)
122 {
123
124 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets);
125 }
126
127 static int
128 div_inpcb_init(void *mem, int size, int flags)
129 {
130 struct inpcb *inp = mem;
131
132 INP_LOCK_INIT(inp, "inp", "divinp");
133 return (0);
134 }
135
136 static void
137 div_inpcb_fini(void *mem, int size)
138 {
139 struct inpcb *inp = mem;
140
141 INP_LOCK_DESTROY(inp);
142 }
143
144 void
145 div_init(void)
146 {
147 INP_INFO_LOCK_INIT(&divcbinfo, "div");
148 LIST_INIT(&divcb);
149 divcbinfo.listhead = &divcb;
150 /*
151 * XXX We don't use the hash list for divert IP, but it's easier
152 * to allocate a one entry hash list than it is to check all
153 * over the place for hashbase == NULL.
154 */
155 divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask);
156 divcbinfo.porthashbase = hashinit(1, M_PCB, &divcbinfo.porthashmask);
157 divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb),
158 NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR,
159 UMA_ZONE_NOFREE);
160 uma_zone_set_max(divcbinfo.ipi_zone, maxsockets);
161 EVENTHANDLER_REGISTER(maxsockets_change, div_zone_change,
162 NULL, EVENTHANDLER_PRI_ANY);
163 }
164
165 /*
166 * IPPROTO_DIVERT is not in the real IP protocol number space; this
167 * function should never be called. Just in case, drop any packets.
168 */
169 void
170 div_input(struct mbuf *m, int off)
171 {
172 ipstat.ips_noproto++;
173 m_freem(m);
174 }
175
176 /*
177 * Divert a packet by passing it up to the divert socket at port 'port'.
178 *
179 * Setup generic address and protocol structures for div_input routine,
180 * then pass them along with mbuf chain.
181 */
182 static void
183 divert_packet(struct mbuf *m, int incoming)
184 {
185 struct ip *ip;
186 struct inpcb *inp;
187 struct socket *sa;
188 u_int16_t nport;
189 struct sockaddr_in divsrc;
190 struct m_tag *mtag;
191
192 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL);
193 if (mtag == NULL) {
194 printf("%s: no divert tag\n", __func__);
195 m_freem(m);
196 return;
197 }
198 /* Assure header */
199 if (m->m_len < sizeof(struct ip) &&
200 (m = m_pullup(m, sizeof(struct ip))) == 0)
201 return;
202 ip = mtod(m, struct ip *);
203
204 /* Delayed checksums are currently not compatible with divert. */
205 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
206 ip->ip_len = ntohs(ip->ip_len);
207 in_delayed_cksum(m);
208 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
209 ip->ip_len = htons(ip->ip_len);
210 }
211
212 /*
213 * Record receive interface address, if any.
214 * But only for incoming packets.
215 */
216 bzero(&divsrc, sizeof(divsrc));
217 divsrc.sin_len = sizeof(divsrc);
218 divsrc.sin_family = AF_INET;
219 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */
220 if (incoming) {
221 struct ifaddr *ifa;
222
223 /* Sanity check */
224 M_ASSERTPKTHDR(m);
225
226 /* Find IP address for receive interface */
227 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
228 if (ifa->ifa_addr == NULL)
229 continue;
230 if (ifa->ifa_addr->sa_family != AF_INET)
231 continue;
232 divsrc.sin_addr =
233 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
234 break;
235 }
236 }
237 /*
238 * Record the incoming interface name whenever we have one.
239 */
240 if (m->m_pkthdr.rcvif) {
241 /*
242 * Hide the actual interface name in there in the
243 * sin_zero array. XXX This needs to be moved to a
244 * different sockaddr type for divert, e.g.
245 * sockaddr_div with multiple fields like
246 * sockaddr_dl. Presently we have only 7 bytes
247 * but that will do for now as most interfaces
248 * are 4 or less + 2 or less bytes for unit.
249 * There is probably a faster way of doing this,
250 * possibly taking it from the sockaddr_dl on the iface.
251 * This solves the problem of a P2P link and a LAN interface
252 * having the same address, which can result in the wrong
253 * interface being assigned to the packet when fed back
254 * into the divert socket. Theoretically if the daemon saves
255 * and re-uses the sockaddr_in as suggested in the man pages,
256 * this iface name will come along for the ride.
257 * (see div_output for the other half of this.)
258 */
259 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname,
260 sizeof(divsrc.sin_zero));
261 }
262
263 /* Put packet on socket queue, if any */
264 sa = NULL;
265 nport = htons((u_int16_t)divert_info(mtag));
266 INP_INFO_RLOCK(&divcbinfo);
267 LIST_FOREACH(inp, &divcb, inp_list) {
268 INP_LOCK(inp);
269 /* XXX why does only one socket match? */
270 if (inp->inp_lport == nport) {
271 sa = inp->inp_socket;
272 SOCKBUF_LOCK(&sa->so_rcv);
273 if (sbappendaddr_locked(&sa->so_rcv,
274 (struct sockaddr *)&divsrc, m,
275 (struct mbuf *)0) == 0) {
276 SOCKBUF_UNLOCK(&sa->so_rcv);
277 sa = NULL; /* force mbuf reclaim below */
278 } else
279 sorwakeup_locked(sa);
280 INP_UNLOCK(inp);
281 break;
282 }
283 INP_UNLOCK(inp);
284 }
285 INP_INFO_RUNLOCK(&divcbinfo);
286 if (sa == NULL) {
287 m_freem(m);
288 ipstat.ips_noproto++;
289 ipstat.ips_delivered--;
290 }
291 }
292
293 /*
294 * Deliver packet back into the IP processing machinery.
295 *
296 * If no address specified, or address is 0.0.0.0, send to ip_output();
297 * otherwise, send to ip_input() and mark as having been received on
298 * the interface with that address.
299 */
300 static int
301 div_output(struct socket *so, struct mbuf *m,
302 struct sockaddr_in *sin, struct mbuf *control)
303 {
304 struct m_tag *mtag;
305 struct divert_tag *dt;
306 int error = 0;
307 struct mbuf *options;
308
309 /*
310 * An mbuf may hasn't come from userland, but we pretend
311 * that it has.
312 */
313 m->m_pkthdr.rcvif = NULL;
314 m->m_nextpkt = NULL;
315
316 if (control)
317 m_freem(control); /* XXX */
318
319 if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) {
320 mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag),
321 M_NOWAIT | M_ZERO);
322 if (mtag == NULL) {
323 error = ENOBUFS;
324 goto cantsend;
325 }
326 dt = (struct divert_tag *)(mtag+1);
327 m_tag_prepend(m, mtag);
328 } else
329 dt = (struct divert_tag *)(mtag+1);
330
331 /* Loopback avoidance and state recovery */
332 if (sin) {
333 int i;
334
335 dt->cookie = sin->sin_port;
336 /*
337 * Find receive interface with the given name, stuffed
338 * (if it exists) in the sin_zero[] field.
339 * The name is user supplied data so don't trust its size
340 * or that it is zero terminated.
341 */
342 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++)
343 ;
344 if ( i > 0 && i < sizeof(sin->sin_zero))
345 m->m_pkthdr.rcvif = ifunit(sin->sin_zero);
346 }
347
348 /* Reinject packet into the system as incoming or outgoing */
349 if (!sin || sin->sin_addr.s_addr == 0) {
350 struct ip *const ip = mtod(m, struct ip *);
351 struct inpcb *inp;
352
353 dt->info |= IP_FW_DIVERT_OUTPUT_FLAG;
354 INP_INFO_WLOCK(&divcbinfo);
355 inp = sotoinpcb(so);
356 INP_LOCK(inp);
357 /*
358 * Don't allow both user specified and setsockopt options,
359 * and don't allow packet length sizes that will crash
360 */
361 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
362 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
363 error = EINVAL;
364 INP_UNLOCK(inp);
365 INP_INFO_WUNLOCK(&divcbinfo);
366 m_freem(m);
367 } else {
368 /* Convert fields to host order for ip_output() */
369 ip->ip_len = ntohs(ip->ip_len);
370 ip->ip_off = ntohs(ip->ip_off);
371
372 /* Send packet to output processing */
373 ipstat.ips_rawout++; /* XXX */
374
375 #ifdef MAC
376 mac_create_mbuf_from_inpcb(inp, m);
377 #endif
378 /*
379 * Get ready to inject the packet into ip_output().
380 * Just in case socket options were specified on the
381 * divert socket, we duplicate them. This is done
382 * to avoid having to hold the PCB locks over the call
383 * to ip_output(), as doing this results in a number of
384 * lock ordering complexities.
385 *
386 * Note that we set the multicast options argument for
387 * ip_output() to NULL since it should be invariant that
388 * they are not present.
389 */
390 KASSERT(inp->inp_moptions == NULL,
391 ("multicast options set on a divert socket"));
392 options = NULL;
393 /*
394 * XXXCSJP: It is unclear to me whether or not it makes
395 * sense for divert sockets to have options. However,
396 * for now we will duplicate them with the INP locks
397 * held so we can use them in ip_output() without
398 * requring a reference to the pcb.
399 */
400 if (inp->inp_options != NULL) {
401 options = m_dup(inp->inp_options, M_DONTWAIT);
402 if (options == NULL)
403 error = ENOBUFS;
404 }
405 INP_UNLOCK(inp);
406 INP_INFO_WUNLOCK(&divcbinfo);
407 if (error == ENOBUFS) {
408 m_freem(m);
409 return (error);
410 }
411 error = ip_output(m, options, NULL,
412 ((so->so_options & SO_DONTROUTE) ?
413 IP_ROUTETOIF : 0) | IP_ALLOWBROADCAST |
414 IP_RAWOUTPUT, NULL, NULL);
415 if (options != NULL)
416 m_freem(options);
417 }
418 } else {
419 dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG;
420 if (m->m_pkthdr.rcvif == NULL) {
421 /*
422 * No luck with the name, check by IP address.
423 * Clear the port and the ifname to make sure
424 * there are no distractions for ifa_ifwithaddr.
425 */
426 struct ifaddr *ifa;
427
428 bzero(sin->sin_zero, sizeof(sin->sin_zero));
429 sin->sin_port = 0;
430 ifa = ifa_ifwithaddr((struct sockaddr *) sin);
431 if (ifa == NULL) {
432 error = EADDRNOTAVAIL;
433 goto cantsend;
434 }
435 m->m_pkthdr.rcvif = ifa->ifa_ifp;
436 }
437 #ifdef MAC
438 SOCK_LOCK(so);
439 mac_create_mbuf_from_socket(so, m);
440 SOCK_UNLOCK(so);
441 #endif
442 /* Send packet to input processing via netisr */
443 netisr_queue(NETISR_IP, m);
444 }
445
446 return error;
447
448 cantsend:
449 m_freem(m);
450 return error;
451 }
452
453 static int
454 div_attach(struct socket *so, int proto, struct thread *td)
455 {
456 struct inpcb *inp;
457 int error;
458
459 INP_INFO_WLOCK(&divcbinfo);
460 inp = sotoinpcb(so);
461 if (inp != 0) {
462 INP_INFO_WUNLOCK(&divcbinfo);
463 return EINVAL;
464 }
465 if (td && (error = suser(td)) != 0) {
466 INP_INFO_WUNLOCK(&divcbinfo);
467 return error;
468 }
469 error = soreserve(so, div_sendspace, div_recvspace);
470 if (error) {
471 INP_INFO_WUNLOCK(&divcbinfo);
472 return error;
473 }
474 error = in_pcballoc(so, &divcbinfo);
475 if (error) {
476 INP_INFO_WUNLOCK(&divcbinfo);
477 return error;
478 }
479 inp = (struct inpcb *)so->so_pcb;
480 INP_INFO_WUNLOCK(&divcbinfo);
481 inp->inp_ip_p = proto;
482 inp->inp_vflag |= INP_IPV4;
483 inp->inp_flags |= INP_HDRINCL;
484 INP_UNLOCK(inp);
485 return 0;
486 }
487
488 static int
489 div_detach(struct socket *so)
490 {
491 struct inpcb *inp;
492
493 INP_INFO_WLOCK(&divcbinfo);
494 inp = sotoinpcb(so);
495 if (inp == 0) {
496 INP_INFO_WUNLOCK(&divcbinfo);
497 return EINVAL;
498 }
499 INP_LOCK(inp);
500 in_pcbdetach(inp);
501 INP_INFO_WUNLOCK(&divcbinfo);
502 return 0;
503 }
504
505 static int
506 div_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
507 {
508 struct inpcb *inp;
509 int error;
510
511 INP_INFO_WLOCK(&divcbinfo);
512 inp = sotoinpcb(so);
513 if (inp == 0) {
514 INP_INFO_WUNLOCK(&divcbinfo);
515 return EINVAL;
516 }
517 /* in_pcbbind assumes that nam is a sockaddr_in
518 * and in_pcbbind requires a valid address. Since divert
519 * sockets don't we need to make sure the address is
520 * filled in properly.
521 * XXX -- divert should not be abusing in_pcbind
522 * and should probably have its own family.
523 */
524 if (nam->sa_family != AF_INET)
525 error = EAFNOSUPPORT;
526 else {
527 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY;
528 INP_LOCK(inp);
529 error = in_pcbbind(inp, nam, td->td_ucred);
530 INP_UNLOCK(inp);
531 }
532 INP_INFO_WUNLOCK(&divcbinfo);
533 return error;
534 }
535
536 static int
537 div_shutdown(struct socket *so)
538 {
539 struct inpcb *inp;
540
541 INP_INFO_RLOCK(&divcbinfo);
542 inp = sotoinpcb(so);
543 if (inp == 0) {
544 INP_INFO_RUNLOCK(&divcbinfo);
545 return EINVAL;
546 }
547 INP_LOCK(inp);
548 INP_INFO_RUNLOCK(&divcbinfo);
549 socantsendmore(so);
550 INP_UNLOCK(inp);
551 return 0;
552 }
553
554 static int
555 div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
556 struct mbuf *control, struct thread *td)
557 {
558 /* Packet must have a header (but that's about it) */
559 if (m->m_len < sizeof (struct ip) &&
560 (m = m_pullup(m, sizeof (struct ip))) == 0) {
561 ipstat.ips_toosmall++;
562 m_freem(m);
563 return EINVAL;
564 }
565
566 /* Send packet */
567 return div_output(so, m, (struct sockaddr_in *)nam, control);
568 }
569
570 void
571 div_ctlinput(int cmd, struct sockaddr *sa, void *vip)
572 {
573 struct in_addr faddr;
574
575 faddr = ((struct sockaddr_in *)sa)->sin_addr;
576 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
577 return;
578 if (PRC_IS_REDIRECT(cmd))
579 return;
580 }
581
582 static int
583 div_pcblist(SYSCTL_HANDLER_ARGS)
584 {
585 int error, i, n;
586 struct inpcb *inp, **inp_list;
587 inp_gen_t gencnt;
588 struct xinpgen xig;
589
590 /*
591 * The process of preparing the TCB list is too time-consuming and
592 * resource-intensive to repeat twice on every request.
593 */
594 if (req->oldptr == 0) {
595 n = divcbinfo.ipi_count;
596 req->oldidx = 2 * (sizeof xig)
597 + (n + n/8) * sizeof(struct xinpcb);
598 return 0;
599 }
600
601 if (req->newptr != 0)
602 return EPERM;
603
604 /*
605 * OK, now we're committed to doing something.
606 */
607 INP_INFO_RLOCK(&divcbinfo);
608 gencnt = divcbinfo.ipi_gencnt;
609 n = divcbinfo.ipi_count;
610 INP_INFO_RUNLOCK(&divcbinfo);
611
612 error = sysctl_wire_old_buffer(req,
613 2 * sizeof(xig) + n*sizeof(struct xinpcb));
614 if (error != 0)
615 return (error);
616
617 xig.xig_len = sizeof xig;
618 xig.xig_count = n;
619 xig.xig_gen = gencnt;
620 xig.xig_sogen = so_gencnt;
621 error = SYSCTL_OUT(req, &xig, sizeof xig);
622 if (error)
623 return error;
624
625 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
626 if (inp_list == 0)
627 return ENOMEM;
628
629 INP_INFO_RLOCK(&divcbinfo);
630 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n;
631 inp = LIST_NEXT(inp, inp_list)) {
632 INP_LOCK(inp);
633 if (inp->inp_gencnt <= gencnt &&
634 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
635 inp_list[i++] = inp;
636 INP_UNLOCK(inp);
637 }
638 INP_INFO_RUNLOCK(&divcbinfo);
639 n = i;
640
641 error = 0;
642 for (i = 0; i < n; i++) {
643 inp = inp_list[i];
644 INP_LOCK(inp);
645 if (inp->inp_gencnt <= gencnt) {
646 struct xinpcb xi;
647 bzero(&xi, sizeof(xi));
648 xi.xi_len = sizeof xi;
649 /* XXX should avoid extra copy */
650 bcopy(inp, &xi.xi_inp, sizeof *inp);
651 if (inp->inp_socket)
652 sotoxsocket(inp->inp_socket, &xi.xi_socket);
653 INP_UNLOCK(inp);
654 error = SYSCTL_OUT(req, &xi, sizeof xi);
655 } else
656 INP_UNLOCK(inp);
657 }
658 if (!error) {
659 /*
660 * Give the user an updated idea of our state.
661 * If the generation differs from what we told
662 * her before, she knows that something happened
663 * while we were processing this request, and it
664 * might be necessary to retry.
665 */
666 INP_INFO_RLOCK(&divcbinfo);
667 xig.xig_gen = divcbinfo.ipi_gencnt;
668 xig.xig_sogen = so_gencnt;
669 xig.xig_count = divcbinfo.ipi_count;
670 INP_INFO_RUNLOCK(&divcbinfo);
671 error = SYSCTL_OUT(req, &xig, sizeof xig);
672 }
673 free(inp_list, M_TEMP);
674 return error;
675 }
676
677 /*
678 * This is the wrapper function for in_setsockaddr. We just pass down
679 * the pcbinfo for in_setpeeraddr to lock.
680 */
681 static int
682 div_sockaddr(struct socket *so, struct sockaddr **nam)
683 {
684 return (in_setsockaddr(so, nam, &divcbinfo));
685 }
686
687 /*
688 * This is the wrapper function for in_setpeeraddr. We just pass down
689 * the pcbinfo for in_setpeeraddr to lock.
690 */
691 static int
692 div_peeraddr(struct socket *so, struct sockaddr **nam)
693 {
694 return (in_setpeeraddr(so, nam, &divcbinfo));
695 }
696
697 #ifdef SYSCTL_NODE
698 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT");
699 SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0,
700 div_pcblist, "S,xinpcb", "List of active divert sockets");
701 #endif
702
703 struct pr_usrreqs div_usrreqs = {
704 .pru_attach = div_attach,
705 .pru_bind = div_bind,
706 .pru_control = in_control,
707 .pru_detach = div_detach,
708 .pru_peeraddr = div_peeraddr,
709 .pru_send = div_send,
710 .pru_shutdown = div_shutdown,
711 .pru_sockaddr = div_sockaddr,
712 .pru_sosetlabel = in_pcbsosetlabel
713 };
714
715 struct protosw div_protosw = {
716 .pr_type = SOCK_RAW,
717 .pr_protocol = IPPROTO_DIVERT,
718 .pr_flags = PR_ATOMIC|PR_ADDR,
719 .pr_input = div_input,
720 .pr_ctlinput = div_ctlinput,
721 .pr_ctloutput = ip_ctloutput,
722 .pr_init = div_init,
723 .pr_usrreqs = &div_usrreqs
724 };
725
726 static int
727 div_modevent(module_t mod, int type, void *unused)
728 {
729 int err = 0;
730 int n;
731
732 switch (type) {
733 case MOD_LOAD:
734 /*
735 * Protocol will be initialized by pf_proto_register().
736 * We don't have to register ip_protox because we are not
737 * a true IP protocol that goes over the wire.
738 */
739 err = pf_proto_register(PF_INET, &div_protosw);
740 ip_divert_ptr = divert_packet;
741 break;
742 case MOD_QUIESCE:
743 /*
744 * IPDIVERT may normally not be unloaded because of the
745 * potential race conditions. Tell kldunload we can't be
746 * unloaded unless the unload is forced.
747 */
748 err = EPERM;
749 break;
750 case MOD_UNLOAD:
751 /*
752 * Forced unload.
753 *
754 * Module ipdivert can only be unloaded if no sockets are
755 * connected. Maybe this can be changed later to forcefully
756 * disconnect any open sockets.
757 *
758 * XXXRW: Note that there is a slight race here, as a new
759 * socket open request could be spinning on the lock and then
760 * we destroy the lock.
761 */
762 INP_INFO_WLOCK(&divcbinfo);
763 n = divcbinfo.ipi_count;
764 if (n != 0) {
765 err = EBUSY;
766 INP_INFO_WUNLOCK(&divcbinfo);
767 break;
768 }
769 ip_divert_ptr = NULL;
770 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW);
771 INP_INFO_WUNLOCK(&divcbinfo);
772 INP_INFO_LOCK_DESTROY(&divcbinfo);
773 uma_zdestroy(divcbinfo.ipi_zone);
774 break;
775 default:
776 err = EOPNOTSUPP;
777 break;
778 }
779 return err;
780 }
781
782 static moduledata_t ipdivertmod = {
783 "ipdivert",
784 div_modevent,
785 0
786 };
787
788 DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
789 MODULE_DEPEND(dummynet, ipfw, 2, 2, 2);
790 MODULE_VERSION(ipdivert, 1);
Cache object: 93da637364dd2685545f9d8df7042c11
|