1 /* $OpenBSD: if_pflog.c,v 1.22 2006/12/15 09:31:20 otto 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>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
94 #include <netinet/in.h>
96 #include <netinet6/nd6.h>
99 #include <net/pfvar.h>
100 #include <net/if_pflog.h>
104 #include <machine/in_cksum.h>
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 *,
119 int pflogioctl(struct ifnet *, u_long, caddr_t);
120 void pflogstart(struct ifnet *);
122 static int pflog_clone_create(struct if_clone *, int, caddr_t);
123 static void pflog_clone_destroy(struct ifnet *);
125 int pflog_clone_create(struct if_clone *, int);
126 int pflog_clone_destroy(struct ifnet *);
129 LIST_HEAD(, pflog_softc) pflogif_list;
131 IFC_SIMPLE_DECLARE(pflog, 1);
133 struct if_clone pflog_cloner =
134 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
137 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
140 extern int ifqmaxlen;
144 pflogattach(int npflog)
147 LIST_INIT(&pflogif_list);
148 for (i = 0; i < PFLOGIFS_MAX; i++)
151 (void) pflog_clone_create(&pflog_cloner, 0);
153 if_clone_attach(&pflog_cloner);
158 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
161 pflog_clone_create(struct if_clone *ifc, int unit)
165 struct pflog_softc *pflogif;
168 if (unit >= PFLOGIFS_MAX)
171 if ((pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_NOWAIT)) == NULL)
173 bzero(pflogif, sizeof(*pflogif));
175 pflogif->sc_unit = unit;
177 ifp = pflogif->sc_ifp = if_alloc(IFT_PFLOG);
179 free(pflogif, M_DEVBUF);
182 if_initname(ifp, ifc->ifc_name, unit);
184 ifp = &pflogif->sc_if;
185 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
187 ifp->if_softc = pflogif;
188 ifp->if_mtu = PFLOGMTU;
189 ifp->if_ioctl = pflogioctl;
190 ifp->if_output = pflogoutput;
191 ifp->if_start = pflogstart;
193 ifp->if_type = IFT_PFLOG;
195 ifp->if_snd.ifq_maxlen = ifqmaxlen;
196 ifp->if_hdrlen = PFLOG_HDRLEN;
204 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
206 bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
214 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
215 pflogifs[unit] = ifp;
226 pflog_clone_destroy(struct ifnet *ifp)
229 pflog_clone_destroy(struct ifnet *ifp)
232 struct pflog_softc *pflogif = ifp->if_softc;
239 pflogifs[pflogif->sc_unit] = NULL;
240 LIST_REMOVE(pflogif, sc_list);
253 free(pflogif, M_DEVBUF);
260 * Start output on the pflog interface.
263 pflogstart(struct ifnet *ifp)
272 IF_LOCK(&ifp->if_snd);
273 _IF_DROP(&ifp->if_snd);
274 _IF_DEQUEUE(&ifp->if_snd, m);
275 IF_UNLOCK(&ifp->if_snd);
278 IF_DROP(&ifp->if_snd);
279 IF_DEQUEUE(&ifp->if_snd, m);
291 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
300 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
308 if (ifp->if_flags & IFF_UP)
309 ifp->if_drv_flags |= IFF_DRV_RUNNING;
311 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
313 if (ifp->if_flags & IFF_UP)
314 ifp->if_flags |= IFF_RUNNING;
316 ifp->if_flags &= ~IFF_RUNNING;
327 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
328 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
329 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
335 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
338 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
341 bzero(&hdr, sizeof(hdr));
342 hdr.length = PFLOG_REAL_HDRLEN;
344 hdr.action = rm->action;
346 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
349 hdr.rulenr = htonl(rm->nr);
352 hdr.rulenr = htonl(am->nr);
353 hdr.subrulenr = htonl(rm->nr);
354 if (ruleset != NULL && ruleset->anchor != NULL)
355 strlcpy(hdr.ruleset, ruleset->anchor->name,
356 sizeof(hdr.ruleset));
358 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
361 * XXX: This should not happen as we force an early lookup
362 * via debug.pfugidhack
366 pd->lookup.done = pf_socket_lookup(dir, pd);
368 if (pd->lookup.done > 0) {
369 hdr.uid = pd->lookup.uid;
370 hdr.pid = pd->lookup.pid;
375 hdr.rule_uid = rm->cuid;
376 hdr.rule_pid = rm->cpid;
380 if (af == AF_INET && dir == PF_OUT) {
383 ip = mtod(m, struct ip *);
385 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
390 ifn->if_obytes += m->m_pkthdr.len;
392 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
394 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
404 pflog_modevent(module_t mod, int type, void *data)
412 pflog_packet_ptr = pflog_packet;
417 pflog_packet_ptr = NULL;
419 if_clone_detach(&pflog_cloner);
429 static moduledata_t pflog_mod = { "pflog", pflog_modevent, 0 };
431 #define PFLOG_MODVER 1
433 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
434 MODULE_VERSION(pflog, PFLOG_MODVER);
435 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
436 #endif /* __FreeBSD__ */