1 /* $OpenBSD: if_pflog.c,v 1.26 2007/10/18 21:58:18 mpf Exp $ */
3 * The authors of this code are John Ioannidis (ji@tla.org),
4 * Angelos D. Keromytis (kermit@csd.uch.gr) and
5 * Niels Provos (provos@physnet.uni-hamburg.de).
7 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11 * by Angelos D. Keromytis.
13 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
18 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
20 * Permission to use, copy, and modify this software with or without fee
21 * is hereby granted, provided that this entire notice is included in
22 * all copies of any software which is or includes a copy or
23 * modification of this software.
24 * You may use this code under the GNU public license if you so wish. Please
25 * contribute changes back to the authors under this freer than GPL license
26 * so that we may further the use of strong encryption without limitations to
29 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
30 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
31 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
32 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38 #include "opt_inet6.h"
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 #define NBPFILTER DEV_BPF
52 #define NPFLOG DEV_PFLOG
57 #else /* ! __FreeBSD__ */
60 #endif /* __FreeBSD__ */
62 #include <sys/param.h>
63 #include <sys/systm.h>
66 #include <sys/socket.h>
68 #include <sys/kernel.h>
69 #include <sys/limits.h>
70 #include <sys/malloc.h>
71 #include <sys/module.h>
72 #include <sys/sockio.h>
74 #include <sys/ioctl.h>
79 #include <net/if_clone.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
85 #if defined(INET) || defined(INET6)
86 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/nd6.h>
99 #include <net/pfvar.h>
100 #include <net/if_pflog.h>
104 #include <machine/in_cksum.h>
106 #endif /* __FreeBSD__ */
108 #define PFLOGMTU (32768 + MHLEN + MLEN)
111 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
116 void pflogattach(int);
117 int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
123 int pflogioctl(struct ifnet *, u_long, caddr_t);
124 void pflogstart(struct ifnet *);
126 static int pflog_clone_create(struct if_clone *, int, caddr_t);
127 static void pflog_clone_destroy(struct ifnet *);
129 int pflog_clone_create(struct if_clone *, int);
130 int pflog_clone_destroy(struct ifnet *);
133 LIST_HEAD(, pflog_softc) pflogif_list;
135 IFC_SIMPLE_DECLARE(pflog, 1);
137 struct if_clone pflog_cloner =
138 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
141 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
144 pflogattach(int npflog)
147 LIST_INIT(&pflogif_list);
148 for (i = 0; i < PFLOGIFS_MAX; i++)
150 if_clone_attach(&pflog_cloner);
155 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
158 pflog_clone_create(struct if_clone *ifc, int unit)
162 struct pflog_softc *pflogif;
165 if (unit >= PFLOGIFS_MAX)
168 if ((pflogif = malloc(sizeof(*pflogif),
169 M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
172 pflogif->sc_unit = unit;
174 ifp = pflogif->sc_ifp = if_alloc(IFT_PFLOG);
176 free(pflogif, M_DEVBUF);
179 if_initname(ifp, ifc->ifc_name, unit);
181 ifp = &pflogif->sc_if;
182 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
184 ifp->if_softc = pflogif;
185 ifp->if_mtu = PFLOGMTU;
186 ifp->if_ioctl = pflogioctl;
187 ifp->if_output = pflogoutput;
188 ifp->if_start = pflogstart;
190 ifp->if_type = IFT_PFLOG;
192 ifp->if_snd.ifq_maxlen = ifqmaxlen;
193 ifp->if_hdrlen = PFLOG_HDRLEN;
201 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
203 bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
209 /* XXX: Why pf(4) lock?! Better add a pflog lock?! */
212 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
213 pflogifs[unit] = ifp;
224 pflog_clone_destroy(struct ifnet *ifp)
227 pflog_clone_destroy(struct ifnet *ifp)
230 struct pflog_softc *pflogif = ifp->if_softc;
237 pflogifs[pflogif->sc_unit] = NULL;
238 LIST_REMOVE(pflogif, sc_list);
251 free(pflogif, M_DEVBUF);
258 * Start output on the pflog interface.
261 pflogstart(struct ifnet *ifp)
270 IF_LOCK(&ifp->if_snd);
271 _IF_DROP(&ifp->if_snd);
272 _IF_DEQUEUE(&ifp->if_snd, m);
273 IF_UNLOCK(&ifp->if_snd);
276 IF_DROP(&ifp->if_snd);
277 IF_DEQUEUE(&ifp->if_snd, m);
289 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
302 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
307 if (ifp->if_flags & IFF_UP)
308 ifp->if_drv_flags |= IFF_DRV_RUNNING;
310 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
312 if (ifp->if_flags & IFF_UP)
313 ifp->if_flags |= IFF_RUNNING;
315 ifp->if_flags &= ~IFF_RUNNING;
326 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
327 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
328 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
334 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
337 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
340 bzero(&hdr, sizeof(hdr));
341 hdr.length = PFLOG_REAL_HDRLEN;
343 hdr.action = rm->action;
345 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
348 hdr.rulenr = htonl(rm->nr);
351 hdr.rulenr = htonl(am->nr);
352 hdr.subrulenr = htonl(rm->nr);
353 if (ruleset != NULL && ruleset->anchor != NULL)
354 strlcpy(hdr.ruleset, ruleset->anchor->name,
355 sizeof(hdr.ruleset));
357 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
360 * XXX: This should not happen as we force an early lookup
361 * via debug.pfugidhack
365 pd->lookup.done = pf_socket_lookup(dir, pd);
367 if (pd->lookup.done > 0) {
368 hdr.uid = pd->lookup.uid;
369 hdr.pid = pd->lookup.pid;
374 hdr.rule_uid = rm->cuid;
375 hdr.rule_pid = rm->cpid;
379 if (af == AF_INET && dir == PF_OUT) {
382 ip = mtod(m, struct ip *);
384 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
389 ifn->if_obytes += m->m_pkthdr.len;
391 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
393 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
403 pflog_modevent(module_t mod, int type, void *data)
411 pflog_packet_ptr = pflog_packet;
416 pflog_packet_ptr = NULL;
418 if_clone_detach(&pflog_cloner);
428 static moduledata_t pflog_mod = { "pflog", pflog_modevent, 0 };
430 #define PFLOG_MODVER 1
432 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
433 MODULE_VERSION(pflog, PFLOG_MODVER);
434 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
435 #endif /* __FreeBSD__ */