[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_divert.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

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

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.