1 /* $FreeBSD: src/sys/contrib/pf/net/if_pflog.c,v 1.9 2004/06/22 20:13:24 brooks Exp $ */
2 /* $OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric Exp $ */
3 /* $DragonFly: src/sys/net/pf/if_pflog.c,v 1.2 2004/11/14 17:27:31 joerg Exp $ */
6 * The authors of this code are John Ioannidis (ji@tla.org),
7 * Angelos D. Keromytis (kermit@csd.uch.gr) and
8 * Niels Provos (provos@physnet.uni-hamburg.de).
10 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
13 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 * by Angelos D. Keromytis.
16 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
19 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
21 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
23 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
25 * Permission to use, copy, and modify this software with or without fee
26 * is hereby granted, provided that this entire notice is included in
27 * all copies of any software which is or includes a copy or
28 * modification of this software.
29 * You may use this code under the GNU public license if you so wish. Please
30 * contribute changes back to the authors under this freer than GPL license
31 * so that we may further the use of strong encryption without limitations to
34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/in_cksum.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 #include <sys/sockio.h>
53 #include <vm/vm_zone.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
69 #include <netinet/in.h>
71 #include <netinet6/nd6.h>
74 #include <net/pf/pfvar.h>
75 #include <net/pf/if_pflog.h>
77 #define PFLOGNAME "pflog"
79 #define PFLOGMTU (32768 + MHLEN + MLEN)
82 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
88 static void pflog_clone_destroy(struct ifnet *);
89 static int pflog_clone_create(struct if_clone *, int);
90 int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
92 int pflogioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
93 void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
94 void pflogstart(struct ifnet *);
96 static MALLOC_DEFINE(M_PFLOG, PFLOGNAME, "Packet Filter Logging Interface");
97 static LIST_HEAD(pflog_list, pflog_softc) pflog_list;
98 struct if_clone pflog_cloner = IF_CLONE_INITIALIZER("pflog", pflog_clone_create,
99 pflog_clone_destroy, 1, 1);
102 pflog_clone_destroy(struct ifnet *ifp)
104 struct pflog_softc *sc;
109 * Do we really need this?
111 IF_DRAIN(&ifp->if_snd);
115 LIST_REMOVE(sc, sc_next);
120 pflog_clone_create(struct if_clone *ifc, int unit)
122 struct pflog_softc *sc;
124 MALLOC(sc, struct pflog_softc *, sizeof(*sc), M_PFLOG, M_WAITOK|M_ZERO);
126 if_initname(&sc->sc_if, ifc->ifc_name, unit);
127 sc->sc_if.if_mtu = PFLOGMTU;
128 sc->sc_if.if_ioctl = pflogioctl;
129 sc->sc_if.if_output = pflogoutput;
130 sc->sc_if.if_start = pflogstart;
131 sc->sc_if.if_type = IFT_PFLOG;
132 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
133 sc->sc_if.if_hdrlen = PFLOG_HDRLEN;
134 sc->sc_if.if_softc = sc;
135 if_attach(&sc->sc_if);
137 LIST_INSERT_HEAD(&pflog_list, sc, sc_next);
138 bpfattach(&sc->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
144 * Start output on the pflog interface.
147 pflogstart(struct ifnet *ifp)
152 IF_DROP(&ifp->if_snd);
153 IF_DRAIN(&ifp->if_snd);
158 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
167 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
170 rt->rt_rmx.rmx_mtu = PFLOGMTU;
175 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
182 if (ifp->if_flags & IFF_UP)
183 ifp->if_flags |= IFF_RUNNING;
185 ifp->if_flags &= ~IFF_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)
203 if (kif == NULL || m == NULL || rm == NULL)
206 bzero(&hdr, sizeof(hdr));
207 hdr.length = PFLOG_REAL_HDRLEN;
209 hdr.action = rm->action;
211 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
214 hdr.rulenr = htonl(rm->nr);
217 hdr.rulenr = htonl(am->nr);
218 hdr.subrulenr = htonl(rm->nr);
220 memcpy(hdr.ruleset, ruleset->name,
221 sizeof(hdr.ruleset));
229 struct ip *ip = mtod(m, struct ip *);
231 ip->ip_len = htons(ip->ip_len);
232 ip->ip_off = htons(ip->ip_off);
236 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
242 m1.m_len = PFLOG_HDRLEN;
243 m1.m_data = (char *) &hdr;
245 KASSERT((!LIST_EMPTY(&pflog_list)), ("pflog: no interface"));
246 ifn = &LIST_FIRST(&pflog_list)->sc_if;
252 struct ip *ip = mtod(m, struct ip *);
254 ip->ip_len = ntohs(ip->ip_len);
255 ip->ip_off = ntohs(ip->ip_off);
263 pflog_modevent(module_t mod, int type, void *data)
269 LIST_INIT(&pflog_list);
270 if_clone_attach(&pflog_cloner);
274 if_clone_detach(&pflog_cloner);
275 while (!LIST_EMPTY(&pflog_list))
277 &LIST_FIRST(&pflog_list)->sc_if);
288 static moduledata_t pflog_mod = {
294 #define PFLOG_MODVER 1
296 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
297 MODULE_VERSION(pflog, PFLOG_MODVER);