2 * The authors of this code are John Ioannidis (ji@tla.org),
3 * Angelos D. Keromytis (kermit@csd.uch.gr) and
4 * Niels Provos (provos@physnet.uni-hamburg.de).
6 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
10 * by Angelos D. Keromytis.
12 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
17 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
19 * Permission to use, copy, and modify this software with or without fee
20 * is hereby granted, provided that this entire notice is included in
21 * all copies of any software which is or includes a copy or
22 * modification of this software.
23 * You may use this code under the GNU public license if you so wish. Please
24 * contribute changes back to the authors under this freer than GPL license
25 * so that we may further the use of strong encryption without limitations to
28 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
29 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
30 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
31 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
34 * $OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/kernel.h>
48 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
55 #include <net/if_clone.h>
56 #include <net/if_pflog.h>
57 #include <net/if_types.h>
58 #include <net/pfvar.h>
60 #if defined(INET) || defined(INET6)
61 #include <netinet/in.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet6/nd6.h>
74 #include <machine/in_cksum.h>
77 #define PFLOGMTU (32768 + MHLEN + MLEN)
80 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
85 static int pflogoutput(struct ifnet *, struct mbuf *,
86 const struct sockaddr *, struct route *);
87 static void pflogattach(int);
88 static int pflogioctl(struct ifnet *, u_long, caddr_t);
89 static void pflogstart(struct ifnet *);
90 static int pflog_clone_create(struct if_clone *, int, caddr_t);
91 static void pflog_clone_destroy(struct ifnet *);
92 static struct if_clone *pflog_cloner;
94 static const char pflogname[] = "pflog";
96 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
99 pflogattach(int npflog)
102 for (i = 0; i < PFLOGIFS_MAX; i++)
104 pflog_cloner = if_clone_simple(pflogname, pflog_clone_create,
105 pflog_clone_destroy, 1);
109 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
113 if (unit >= PFLOGIFS_MAX)
116 ifp = if_alloc(IFT_PFLOG);
120 if_initname(ifp, pflogname, unit);
121 ifp->if_mtu = PFLOGMTU;
122 ifp->if_ioctl = pflogioctl;
123 ifp->if_output = pflogoutput;
124 ifp->if_start = pflogstart;
125 ifp->if_snd.ifq_maxlen = ifqmaxlen;
126 ifp->if_hdrlen = PFLOG_HDRLEN;
129 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
131 pflogifs[unit] = ifp;
137 pflog_clone_destroy(struct ifnet *ifp)
141 for (i = 0; i < PFLOGIFS_MAX; i++)
142 if (pflogifs[i] == ifp)
151 * Start output on the pflog interface.
154 pflogstart(struct ifnet *ifp)
159 IF_LOCK(&ifp->if_snd);
160 _IF_DROP(&ifp->if_snd);
161 _IF_DEQUEUE(&ifp->if_snd, m);
162 IF_UNLOCK(&ifp->if_snd);
172 pflogoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
181 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
185 if (ifp->if_flags & IFF_UP)
186 ifp->if_drv_flags |= IFF_DRV_RUNNING;
188 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
198 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
199 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
200 struct pf_ruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
205 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
208 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
211 bzero(&hdr, sizeof(hdr));
212 hdr.length = PFLOG_REAL_HDRLEN;
214 hdr.action = rm->action;
216 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
219 hdr.rulenr = htonl(rm->nr);
222 hdr.rulenr = htonl(am->nr);
223 hdr.subrulenr = htonl(rm->nr);
224 if (ruleset != NULL && ruleset->anchor != NULL)
225 strlcpy(hdr.ruleset, ruleset->anchor->name,
226 sizeof(hdr.ruleset));
229 * XXXGL: we avoid pf_socket_lookup() when we are holding
230 * state lock, since this leads to unsafe LOR.
231 * These conditions are very very rare, however.
233 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
234 pd->lookup.done = pf_socket_lookup(dir, pd, m);
235 if (pd->lookup.done > 0)
236 hdr.uid = pd->lookup.uid;
240 hdr.rule_uid = rm->cuid;
241 hdr.rule_pid = rm->cpid;
245 if (af == AF_INET && dir == PF_OUT) {
248 ip = mtod(m, struct ip *);
250 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
255 ifn->if_obytes += m->m_pkthdr.len;
256 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
262 pflog_modevent(module_t mod, int type, void *data)
270 pflog_packet_ptr = pflog_packet;
275 pflog_packet_ptr = NULL;
277 if_clone_detach(pflog_cloner);
287 static moduledata_t pflog_mod = { pflogname, pflog_modevent, 0 };
289 #define PFLOG_MODVER 1
291 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
292 MODULE_VERSION(pflog, PFLOG_MODVER);
293 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);