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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
54 #include <net/if_clone.h>
55 #include <net/if_pflog.h>
56 #include <net/if_types.h>
57 #include <net/pfvar.h>
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
68 #include <netinet6/in6_var.h>
69 #include <netinet6/nd6.h>
73 #include <machine/in_cksum.h>
76 #define PFLOGMTU (32768 + MHLEN + MLEN)
79 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
84 static int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
86 static void pflogattach(int);
87 static int pflogioctl(struct ifnet *, u_long, caddr_t);
88 static void pflogstart(struct ifnet *);
89 static int pflog_clone_create(struct if_clone *, int, caddr_t);
90 static void pflog_clone_destroy(struct ifnet *);
92 IFC_SIMPLE_DECLARE(pflog, 1);
94 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
97 pflogattach(int npflog)
100 for (i = 0; i < PFLOGIFS_MAX; i++)
102 if_clone_attach(&pflog_cloner);
106 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
110 if (unit >= PFLOGIFS_MAX)
113 ifp = if_alloc(IFT_PFLOG);
117 if_initname(ifp, ifc->ifc_name, unit);
118 ifp->if_mtu = PFLOGMTU;
119 ifp->if_ioctl = pflogioctl;
120 ifp->if_output = pflogoutput;
121 ifp->if_start = pflogstart;
122 ifp->if_snd.ifq_maxlen = ifqmaxlen;
123 ifp->if_hdrlen = PFLOG_HDRLEN;
126 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
128 pflogifs[unit] = ifp;
134 pflog_clone_destroy(struct ifnet *ifp)
138 for (i = 0; i < PFLOGIFS_MAX; i++)
139 if (pflogifs[i] == ifp)
148 * Start output on the pflog interface.
151 pflogstart(struct ifnet *ifp)
156 IF_LOCK(&ifp->if_snd);
157 _IF_DROP(&ifp->if_snd);
158 _IF_DEQUEUE(&ifp->if_snd, m);
159 IF_UNLOCK(&ifp->if_snd);
169 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
178 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
182 if (ifp->if_flags & IFF_UP)
183 ifp->if_drv_flags |= IFF_DRV_RUNNING;
185 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
195 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
196 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
197 struct pf_ruleset *ruleset, struct pf_pdesc *pd, int lookupsafe)
202 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
205 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
208 bzero(&hdr, sizeof(hdr));
209 hdr.length = PFLOG_REAL_HDRLEN;
211 hdr.action = rm->action;
213 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
216 hdr.rulenr = htonl(rm->nr);
219 hdr.rulenr = htonl(am->nr);
220 hdr.subrulenr = htonl(rm->nr);
221 if (ruleset != NULL && ruleset->anchor != NULL)
222 strlcpy(hdr.ruleset, ruleset->anchor->name,
223 sizeof(hdr.ruleset));
226 * XXXGL: we avoid pf_socket_lookup() when we are holding
227 * state lock, since this leads to unsafe LOR.
228 * These conditions are very very rare, however.
230 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done && lookupsafe)
231 pd->lookup.done = pf_socket_lookup(dir, pd, m);
232 if (pd->lookup.done > 0)
233 hdr.uid = pd->lookup.uid;
237 hdr.rule_uid = rm->cuid;
238 hdr.rule_pid = rm->cpid;
242 if (af == AF_INET && dir == PF_OUT) {
245 ip = mtod(m, struct ip *);
247 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
252 ifn->if_obytes += m->m_pkthdr.len;
253 BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
259 pflog_modevent(module_t mod, int type, void *data)
267 pflog_packet_ptr = pflog_packet;
272 pflog_packet_ptr = NULL;
274 if_clone_detach(&pflog_cloner);
284 static moduledata_t pflog_mod = { "pflog", pflog_modevent, NULL };
286 #define PFLOG_MODVER 1
288 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
289 MODULE_VERSION(pflog, PFLOG_MODVER);
290 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);