The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netpfil/pf/if_pflog.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: ISC
    3  *
    4  * The authors of this code are John Ioannidis (ji@tla.org),
    5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
    6  * Niels Provos (provos@physnet.uni-hamburg.de).
    7  *
    8  * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
    9  * in November 1995.
   10  *
   11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
   12  * by Angelos D. Keromytis.
   13  *
   14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
   15  * and Niels Provos.
   16  *
   17  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
   18  * and Niels Provos.
   19  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
   20  *
   21  * Permission to use, copy, and modify this software with or without fee
   22  * is hereby granted, provided that this entire notice is included in
   23  * all copies of any software which is or includes a copy or
   24  * modification of this software.
   25  * You may use this code under the GNU public license if you so wish. Please
   26  * contribute changes back to the authors under this freer than GPL license
   27  * so that we may further the use of strong encryption without limitations to
   28  * all.
   29  *
   30  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
   31  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
   32  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
   33  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
   34  * PURPOSE.
   35  *
   36  *      $OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $
   37  */
   38 
   39 #include <sys/cdefs.h>
   40 __FBSDID("$FreeBSD$");
   41 
   42 #include "opt_inet.h"
   43 #include "opt_inet6.h"
   44 #include "opt_bpf.h"
   45 #include "opt_pf.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/kernel.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/module.h>
   51 #include <sys/proc.h>
   52 #include <sys/socket.h>
   53 #include <sys/sockio.h>
   54 
   55 #include <net/bpf.h>
   56 #include <net/if.h>
   57 #include <net/if_var.h>
   58 #include <net/if_clone.h>
   59 #include <net/if_pflog.h>
   60 #include <net/if_types.h>
   61 #include <net/vnet.h>
   62 #include <net/pfvar.h>
   63 
   64 #if defined(INET) || defined(INET6)
   65 #include <netinet/in.h>
   66 #endif
   67 #ifdef  INET
   68 #include <netinet/in_var.h>
   69 #include <netinet/ip.h>
   70 #endif
   71 
   72 #ifdef INET6
   73 #include <netinet6/in6_var.h>
   74 #include <netinet6/nd6.h>
   75 #endif /* INET6 */
   76 
   77 #ifdef INET
   78 #include <machine/in_cksum.h>
   79 #endif /* INET */
   80 
   81 #define PFLOGMTU        (32768 + MHLEN + MLEN)
   82 
   83 #ifdef PFLOGDEBUG
   84 #define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
   85 #else
   86 #define DPRINTF(x)
   87 #endif
   88 
   89 static int      pflogoutput(struct ifnet *, struct mbuf *,
   90                     const struct sockaddr *, struct route *);
   91 static void     pflogattach(int);
   92 static int      pflogioctl(struct ifnet *, u_long, caddr_t);
   93 static void     pflogstart(struct ifnet *);
   94 static int      pflog_clone_create(struct if_clone *, char *, size_t,
   95                     struct ifc_data *, struct ifnet **);
   96 static int      pflog_clone_destroy(struct if_clone *, struct ifnet *, uint32_t);
   97 
   98 static const char pflogname[] = "pflog";
   99 
  100 VNET_DEFINE_STATIC(struct if_clone *, pflog_cloner);
  101 #define V_pflog_cloner          VNET(pflog_cloner)
  102 
  103 VNET_DEFINE(struct ifnet *, pflogifs[PFLOGIFS_MAX]);    /* for fast access */
  104 #define V_pflogifs              VNET(pflogifs)
  105 
  106 static void
  107 pflogattach(int npflog __unused)
  108 {
  109         int     i;
  110         for (i = 0; i < PFLOGIFS_MAX; i++)
  111                 V_pflogifs[i] = NULL;
  112 
  113         struct if_clone_addreq req = {
  114                 .create_f = pflog_clone_create,
  115                 .destroy_f = pflog_clone_destroy,
  116                 .flags = IFC_F_AUTOUNIT,
  117         };
  118         V_pflog_cloner = ifc_attach_cloner(pflogname, &req);
  119         struct ifc_data ifd = { .unit = 0 };
  120         ifc_create_ifp(pflogname, &ifd, NULL);
  121 }
  122 
  123 static int
  124 pflog_clone_create(struct if_clone *ifc, char *name, size_t maxlen,
  125     struct ifc_data *ifd, struct ifnet **ifpp)
  126 {
  127         struct ifnet *ifp;
  128 
  129         if (ifd->unit >= PFLOGIFS_MAX)
  130                 return (EINVAL);
  131 
  132         ifp = if_alloc(IFT_PFLOG);
  133         if (ifp == NULL) {
  134                 return (ENOSPC);
  135         }
  136         if_initname(ifp, pflogname, ifd->unit);
  137         ifp->if_mtu = PFLOGMTU;
  138         ifp->if_ioctl = pflogioctl;
  139         ifp->if_output = pflogoutput;
  140         ifp->if_start = pflogstart;
  141         ifp->if_snd.ifq_maxlen = ifqmaxlen;
  142         ifp->if_hdrlen = PFLOG_HDRLEN;
  143         if_attach(ifp);
  144 
  145         bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
  146 
  147         V_pflogifs[ifd->unit] = ifp;
  148         *ifpp = ifp;
  149 
  150         return (0);
  151 }
  152 
  153 static int
  154 pflog_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
  155 {
  156         int i;
  157 
  158         if (ifp->if_dunit == 0 && (flags & IFC_F_FORCE) == 0)
  159                 return (EINVAL);
  160 
  161         for (i = 0; i < PFLOGIFS_MAX; i++)
  162                 if (V_pflogifs[i] == ifp)
  163                         V_pflogifs[i] = NULL;
  164 
  165         bpfdetach(ifp);
  166         if_detach(ifp);
  167         if_free(ifp);
  168 
  169         return (0);
  170 }
  171 
  172 /*
  173  * Start output on the pflog interface.
  174  */
  175 static void
  176 pflogstart(struct ifnet *ifp)
  177 {
  178         struct mbuf *m;
  179 
  180         for (;;) {
  181                 IF_LOCK(&ifp->if_snd);
  182                 _IF_DEQUEUE(&ifp->if_snd, m);
  183                 IF_UNLOCK(&ifp->if_snd);
  184 
  185                 if (m == NULL)
  186                         return;
  187                 else
  188                         m_freem(m);
  189         }
  190 }
  191 
  192 static int
  193 pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
  194         struct route *rt)
  195 {
  196         m_freem(m);
  197         return (0);
  198 }
  199 
  200 /* ARGSUSED */
  201 static int
  202 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
  203 {
  204         switch (cmd) {
  205         case SIOCSIFFLAGS:
  206                 if (ifp->if_flags & IFF_UP)
  207                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
  208                 else
  209                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  210                 break;
  211         default:
  212                 return (ENOTTY);
  213         }
  214 
  215         return (0);
  216 }
  217 
  218 static int
  219 pflog_packet(struct pfi_kkif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
  220     u_int8_t reason, struct pf_krule *rm, struct pf_krule *am,
  221     struct pf_kruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
  222 {
  223         struct ifnet *ifn;
  224         struct pfloghdr hdr;
  225 
  226         if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
  227                 return ( 1);
  228 
  229         if ((ifn = V_pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
  230                 return (0);
  231 
  232         bzero(&hdr, sizeof(hdr));
  233         hdr.length = PFLOG_REAL_HDRLEN;
  234         hdr.af = af;
  235         hdr.action = rm->action;
  236         hdr.reason = reason;
  237         memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
  238 
  239         if (am == NULL) {
  240                 hdr.rulenr = htonl(rm->nr);
  241                 hdr.subrulenr = -1;
  242         } else {
  243                 hdr.rulenr = htonl(am->nr);
  244                 hdr.subrulenr = htonl(rm->nr);
  245                 if (ruleset != NULL && ruleset->anchor != NULL)
  246                         strlcpy(hdr.ruleset, ruleset->anchor->name,
  247                             sizeof(hdr.ruleset));
  248         }
  249         hdr.ridentifier = htonl(rm->ridentifier);
  250         /*
  251          * XXXGL: we avoid pf_socket_lookup() when we are holding
  252          * state lock, since this leads to unsafe LOR.
  253          * These conditions are very very rare, however.
  254          */
  255         if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
  256                 pd->lookup.done = pf_socket_lookup(dir, pd, m);
  257         if (pd->lookup.done > 0)
  258                 hdr.uid = pd->lookup.uid;
  259         else
  260                 hdr.uid = UID_MAX;
  261         hdr.pid = NO_PID;
  262         hdr.rule_uid = rm->cuid;
  263         hdr.rule_pid = rm->cpid;
  264         hdr.dir = dir;
  265 
  266 #ifdef INET
  267         if (af == AF_INET && dir == PF_OUT) {
  268                 struct ip *ip;
  269 
  270                 ip = mtod(m, struct ip *);
  271                 ip->ip_sum = 0;
  272                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
  273         }
  274 #endif /* INET */
  275 
  276         if_inc_counter(ifn, IFCOUNTER_OPACKETS, 1);
  277         if_inc_counter(ifn, IFCOUNTER_OBYTES, m->m_pkthdr.len);
  278         BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
  279 
  280         return (0);
  281 }
  282 
  283 static void
  284 vnet_pflog_init(const void *unused __unused)
  285 {
  286 
  287         pflogattach(1);
  288 }
  289 VNET_SYSINIT(vnet_pflog_init, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY,
  290     vnet_pflog_init, NULL);
  291 
  292 static void
  293 vnet_pflog_uninit(const void *unused __unused)
  294 {
  295 
  296         ifc_detach_cloner(V_pflog_cloner);
  297 }
  298 /*
  299  * Detach after pf is gone; otherwise we might touch pflog memory
  300  * from within pf after freeing pflog.
  301  */
  302 VNET_SYSUNINIT(vnet_pflog_uninit, SI_SUB_INIT_IF, SI_ORDER_SECOND,
  303     vnet_pflog_uninit, NULL);
  304 
  305 static int
  306 pflog_modevent(module_t mod, int type, void *data)
  307 {
  308         int error = 0;
  309 
  310         switch (type) {
  311         case MOD_LOAD:
  312                 PF_RULES_WLOCK();
  313                 pflog_packet_ptr = pflog_packet;
  314                 PF_RULES_WUNLOCK();
  315                 break;
  316         case MOD_UNLOAD:
  317                 PF_RULES_WLOCK();
  318                 pflog_packet_ptr = NULL;
  319                 PF_RULES_WUNLOCK();
  320                 break;
  321         default:
  322                 error = EOPNOTSUPP;
  323                 break;
  324         }
  325 
  326         return error;
  327 }
  328 
  329 static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 };
  330 
  331 #define PFLOG_MODVER 1
  332 
  333 /* Do not run before pf is initialized as we depend on its locks. */
  334 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_FIREWALL, SI_ORDER_ANY);
  335 MODULE_VERSION(pflog, PFLOG_MODVER);
  336 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);

Cache object: ef6478f64a5cb030ed7440576554b854


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


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