1 /* $OpenBSD: if_pflog.c,v 1.27 2007/12/20 02:53:02 brad 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 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
22 * Permission to use, copy, and modify this software with or without fee
23 * is hereby granted, provided that this entire notice is included in
24 * all copies of any software which is or includes a copy or
25 * modification of this software.
26 * You may use this code under the GNU public license if you so wish. Please
27 * contribute changes back to the authors under this freer than GPL license
28 * so that we may further the use of strong encryption without limitations to
31 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
32 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
33 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
34 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/in_cksum.h>
46 #include <sys/socket.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/sockio.h>
51 #include <sys/thread2.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
67 #include <netinet/in.h>
69 #include <netinet6/nd6.h>
72 #include <net/pf/pfvar.h>
73 #include <net/pf/if_pflog.h>
75 #define PFLOGNAME "pflog"
77 #define PFLOGMTU (32768 + MHLEN + MLEN)
80 #define DPRINTF(x) do { if (pflogdebug) kprintf x ; } while (0)
85 void pflogattach(int);
86 int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
88 int pflogioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
89 void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
90 void pflogstart(struct ifnet *);
91 int pflog_clone_create(struct if_clone *, int, caddr_t);
92 int pflog_clone_destroy(struct ifnet *);
94 LIST_HEAD(, pflog_softc) pflogif_list;
95 struct if_clone pflog_cloner =
96 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy, 1, 1);
98 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
101 pflogattach(int npflog)
104 LIST_INIT(&pflogif_list);
105 for (i = 0; i < PFLOGIFS_MAX; i++)
107 if_clone_attach(&pflog_cloner);
111 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
114 struct pflog_softc *pflogif;
116 lwkt_gettoken(&pf_token);
118 if (unit >= PFLOGIFS_MAX) {
119 lwkt_reltoken(&pf_token);
123 if ((pflogif = kmalloc(sizeof(*pflogif),
124 M_DEVBUF, M_WAITOK|M_ZERO)) == NULL) {
125 lwkt_reltoken(&pf_token);
129 pflogif->sc_unit = unit;
130 lwkt_reltoken(&pf_token);
131 ifp = &pflogif->sc_if;
132 ksnprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
133 ifp->if_softc = pflogif;
134 ifp->if_mtu = PFLOGMTU;
135 ifp->if_ioctl = pflogioctl;
136 ifp->if_output = pflogoutput;
137 ifp->if_start = pflogstart;
138 ifp->if_type = IFT_PFLOG;
139 ifp->if_snd.ifq_maxlen = ifqmaxlen;
140 ifp->if_hdrlen = PFLOG_HDRLEN;
141 if_attach(ifp, NULL);
143 bpfattach(&pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
144 lwkt_gettoken(&pf_token);
147 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
148 pflogifs[unit] = ifp;
151 lwkt_reltoken(&pf_token);
156 pflog_clone_destroy(struct ifnet *ifp)
158 struct pflog_softc *pflogif = ifp->if_softc;
160 lwkt_gettoken(&pf_token);
163 pflogifs[pflogif->sc_unit] = NULL;
164 LIST_REMOVE(pflogif, sc_list);
167 lwkt_reltoken(&pf_token);
172 lwkt_gettoken(&pf_token);
173 kfree(pflogif, M_DEVBUF);
174 lwkt_reltoken(&pf_token);
180 * Start output on the pflog interface.
183 pflogstart(struct ifnet *ifp)
187 ASSERT_LWKT_TOKEN_HELD(&pf_token);
190 lwkt_reltoken(&pf_token);
192 IF_DROP(&ifp->if_snd);
193 IF_DEQUEUE(&ifp->if_snd, m);
195 lwkt_gettoken(&pf_token);
205 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
214 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
217 rt->rt_rmx.rmx_mtu = PFLOGMTU;
222 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
225 lwkt_gettoken(&pf_token);
229 lwkt_reltoken(&pf_token);
230 if (ifp->if_flags & IFF_UP)
231 ifp->if_flags |= IFF_RUNNING;
233 ifp->if_flags &= ~IFF_RUNNING;
234 lwkt_gettoken(&pf_token);
237 lwkt_reltoken(&pf_token);
241 lwkt_reltoken(&pf_token);
246 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
247 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
248 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
250 struct ifnet *ifn = NULL;
253 ASSERT_LWKT_TOKEN_HELD(&pf_token);
255 if (kif == NULL || m == NULL || rm == NULL)
258 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
261 bzero(&hdr, sizeof(hdr));
262 hdr.length = PFLOG_REAL_HDRLEN;
264 hdr.action = rm->action;
266 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
269 hdr.rulenr = htonl(rm->nr);
272 hdr.rulenr = htonl(am->nr);
273 hdr.subrulenr = htonl(rm->nr);
274 if (ruleset != NULL && ruleset->anchor != NULL)
275 strlcpy(hdr.ruleset, ruleset->anchor->name,
276 sizeof(hdr.ruleset));
278 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
279 pd->lookup.done = pf_socket_lookup(dir, pd);
280 if (pd->lookup.done > 0) {
281 hdr.uid = pd->lookup.uid;
282 hdr.pid = pd->lookup.pid;
287 hdr.rule_uid = rm->cuid;
288 hdr.rule_pid = rm->cpid;
294 ip = mtod(m, struct ip *);
295 ip->ip_len = htons(ip->ip_len);
296 ip->ip_off = htons(ip->ip_off);
300 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
306 ifn->if_obytes += m->m_pkthdr.len;
307 lwkt_reltoken(&pf_token);
308 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
310 lwkt_gettoken(&pf_token);
314 struct ip *ip = mtod(m, struct ip *);
316 ip->ip_len = ntohs(ip->ip_len);
317 ip->ip_off = ntohs(ip->ip_off);
325 pflog_modevent(module_t mod, int type, void *data)
329 struct pflog_softc *pflogif, *tmp;
331 lwkt_gettoken(&pf_token);
335 LIST_INIT(&pflogif_list);
336 lwkt_reltoken(&pf_token);
337 if_clone_attach(&pflog_cloner);
338 lwkt_gettoken(&pf_token);
342 lwkt_reltoken(&pf_token);
343 if_clone_detach(&pflog_cloner);
344 LIST_FOREACH_MUTABLE(pflogif, &pflogif_list, sc_list, tmp) {
345 pflog_clone_destroy(&pflogif->sc_if);
347 lwkt_gettoken(&pf_token);
354 lwkt_reltoken(&pf_token);
358 static moduledata_t pflog_mod = {
364 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
365 MODULE_VERSION(pflog, PFLOG_MODVER);