Deleted Added
sdiff udiff text old ( 130613 ) new ( 130933 )
full compact
1/* $FreeBSD: head/sys/contrib/pf/net/if_pflog.c 130613 2004-06-16 23:24:02Z mlaier $ */
2/* $OpenBSD: if_pflog.c,v 1.11 2003/12/31 11:18:25 cedric Exp $ */
3/*
4 * The authors of this code are John Ioannidis (ji@tla.org),
5 * Angelos D. Keromytis (kermit@csd.uch.gr) and
6 * Niels Provos (provos@physnet.uni-hamburg.de).
7 *
8 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
9 * in November 1995.
10 *
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
13 *
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
15 * and Niels Provos.
16 *
17 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
18 * and Niels Provos.
19 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
20 *
21 * Permission to use, copy, and modify this software with or without fee
22 * is hereby granted, provided that this entire notice is included in
23 * all copies of any software which is or includes a copy or
24 * modification of this software.
25 * You may use this code under the GNU public license if you so wish. Please
26 * contribute changes back to the authors under this freer than GPL license
27 * so that we may further the use of strong encryption without limitations to
28 * all.
29 *
30 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
31 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
32 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
33 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
34 * PURPOSE.
35 */
36
37#ifdef __FreeBSD__
38#include "opt_inet.h"
39#include "opt_inet6.h"
40#endif
41
42#ifndef __FreeBSD__
43#include "bpfilter.h"
44#include "pflog.h"
45#elif __FreeBSD__ >= 5
46#include "opt_bpf.h"
47#include "opt_pf.h"
48#define NBPFILTER DEV_BPF
49#define NPFLOG DEV_PFLOG
50#endif
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#ifdef __FreeBSD__
57#include <sys/kernel.h>
58#include <sys/malloc.h>
59#include <sys/module.h>
60#include <sys/sockio.h>
61#else
62#include <sys/ioctl.h>
63#endif
64
65#include <net/if.h>
66#include <net/if_types.h>
67#include <net/route.h>
68#include <net/bpf.h>
69
70#ifdef INET
71#include <netinet/in.h>
72#include <netinet/in_var.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#endif
76
77#ifdef __FreeBSD__
78#include <machine/in_cksum.h>
79#endif
80
81#ifdef INET6
82#ifndef INET
83#include <netinet/in.h>
84#endif
85#include <netinet6/nd6.h>
86#endif /* INET6 */
87
88#include <net/pfvar.h>
89#include <net/if_pflog.h>
90
91#ifdef __FreeBSD__
92#define PFLOGNAME "pflog"
93#endif
94
95#define PFLOGMTU (32768 + MHLEN + MLEN)
96
97#ifdef PFLOGDEBUG
98#define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
99#else
100#define DPRINTF(x)
101#endif
102
103#ifndef __FreeBSD__
104struct pflog_softc pflogif[NPFLOG];
105#endif
106
107#ifdef __FreeBSD__
108static void pflog_clone_destroy(struct ifnet *);
109static int pflog_clone_create(struct if_clone *, int);
110#else
111void pflogattach(int);
112#endif
113int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
114 struct rtentry *);
115int pflogioctl(struct ifnet *, u_long, caddr_t);
116void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
117void pflogstart(struct ifnet *);
118
119#ifndef __FreeBSD__
120extern int ifqmaxlen;
121#endif
122
123#ifdef __FreeBSD__
124static MALLOC_DEFINE(M_PFLOG, PFLOGNAME, "Packet Filter Logging Interface");
125static LIST_HEAD(pflog_list, pflog_softc) pflog_list;
126struct if_clone pflog_cloner = IF_CLONE_INITIALIZER(PFLOGNAME,
127 pflog_clone_create, pflog_clone_destroy, 1, IF_MAXUNIT);
128
129static void
130pflog_clone_destroy(struct ifnet *ifp)
131{
132 struct pflog_softc *sc;
133
134 sc = ifp->if_softc;
135
136 /*
137 * Does we really need this?
138 */
139 IF_DRAIN(&ifp->if_snd);
140
141 bpfdetach(ifp);
142 if_detach(ifp);
143 LIST_REMOVE(sc, sc_next);
144 free(sc, M_PFLOG);
145}
146
147static int
148pflog_clone_create(struct if_clone *ifc, int unit)
149{
150 struct pflog_softc *sc;
151
152 MALLOC(sc, struct pflog_softc *, sizeof(*sc), M_PFLOG, M_WAITOK|M_ZERO);
153
154 if_initname(&sc->sc_if, ifc->ifc_name, unit);
155 sc->sc_if.if_mtu = PFLOGMTU;
156 sc->sc_if.if_ioctl = pflogioctl;
157 sc->sc_if.if_output = pflogoutput;
158 sc->sc_if.if_start = pflogstart;
159 sc->sc_if.if_type = IFT_PFLOG;
160 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
161 sc->sc_if.if_hdrlen = PFLOG_HDRLEN;
162 sc->sc_if.if_softc = sc;
163 if_attach(&sc->sc_if);
164
165 LIST_INSERT_HEAD(&pflog_list, sc, sc_next);
166#if NBPFILTER > 0
167 bpfattach(&sc->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
168#endif
169
170 return (0);
171}
172#else /* !__FreeBSD__ */
173void
174pflogattach(int npflog)
175{
176 struct ifnet *ifp;
177 int i;
178
179 bzero(pflogif, sizeof(pflogif));
180
181 for (i = 0; i < NPFLOG; i++) {
182 ifp = &pflogif[i].sc_if;
183 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
184 ifp->if_softc = &pflogif[i];
185 ifp->if_mtu = PFLOGMTU;
186 ifp->if_ioctl = pflogioctl;
187 ifp->if_output = pflogoutput;
188 ifp->if_start = pflogstart;
189 ifp->if_type = IFT_PFLOG;
190 ifp->if_snd.ifq_maxlen = ifqmaxlen;
191 ifp->if_hdrlen = PFLOG_HDRLEN;
192 if_attach(ifp);
193 if_alloc_sadl(ifp);
194
195#if NBPFILTER > 0
196 bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
197 PFLOG_HDRLEN);
198#endif
199 }
200}
201#endif /* __FreeBSD__ */
202
203/*
204 * Start output on the pflog interface.
205 */
206void
207pflogstart(struct ifnet *ifp)
208{
209 struct mbuf *m;
210#ifndef __FreeBSD__
211 int s;
212#endif
213
214 for (;;) {
215#ifdef __FreeBSD__
216 IF_LOCK(&ifp->if_snd);
217 _IF_DROP(&ifp->if_snd);
218 _IF_DEQUEUE(&ifp->if_snd, m);
219 if (m == NULL) {
220 IF_UNLOCK(&ifp->if_snd);
221 return;
222 }
223 else
224 m_freem(m);
225 IF_UNLOCK(&ifp->if_snd);
226#else
227 s = splimp();
228 IF_DROP(&ifp->if_snd);
229 IF_DEQUEUE(&ifp->if_snd, m);
230 splx(s);
231 if (m == NULL)
232 return;
233 else
234 m_freem(m);
235#endif
236 }
237}
238
239int
240pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
241 struct rtentry *rt)
242{
243 m_freem(m);
244 return (0);
245}
246
247/* ARGSUSED */
248void
249pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
250{
251 if (rt)
252 rt->rt_rmx.rmx_mtu = PFLOGMTU;
253}
254
255/* ARGSUSED */
256int
257pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
258{
259 switch (cmd) {
260 case SIOCSIFADDR:
261 case SIOCAIFADDR:
262 case SIOCSIFDSTADDR:
263 case SIOCSIFFLAGS:
264 if (ifp->if_flags & IFF_UP)
265 ifp->if_flags |= IFF_RUNNING;
266 else
267 ifp->if_flags &= ~IFF_RUNNING;
268 break;
269 default:
270 return (EINVAL);
271 }
272
273 return (0);
274}
275
276int
277pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
278 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
279 struct pf_ruleset *ruleset)
280{
281#if NBPFILTER > 0
282 struct ifnet *ifn;
283 struct pfloghdr hdr;
284#ifndef __FreeBSD__
285 struct mbuf m1;
286#endif
287
288 if (kif == NULL || m == NULL || rm == NULL)
289 return (-1);
290
291 bzero(&hdr, sizeof(hdr));
292 hdr.length = PFLOG_REAL_HDRLEN;
293 hdr.af = af;
294 hdr.action = rm->action;
295 hdr.reason = reason;
296 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
297
298 if (am == NULL) {
299 hdr.rulenr = htonl(rm->nr);
300 hdr.subrulenr = -1;
301 } else {
302 hdr.rulenr = htonl(am->nr);
303 hdr.subrulenr = htonl(rm->nr);
304 if (ruleset != NULL)
305 memcpy(hdr.ruleset, ruleset->name,
306 sizeof(hdr.ruleset));
307
308
309 }
310 hdr.dir = dir;
311
312#ifdef INET
313 if (af == AF_INET && dir == PF_OUT) {
314 struct ip *ip;
315
316 ip = mtod(m, struct ip *);
317 ip->ip_sum = 0;
318 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
319 }
320#endif /* INET */
321
322#ifndef __FreeBSD__
323 m1.m_next = m;
324 m1.m_len = PFLOG_HDRLEN;
325 m1.m_data = (char *) &hdr;
326#endif
327
328#ifdef __FreeBSD__
329 KASSERT((!LIST_EMPTY(&pflog_list)), ("pflog: no interface"));
330 ifn = &LIST_FIRST(&pflog_list)->sc_if;
331 BPF_MTAP2(ifn, &hdr, sizeof(hdr), m);
332#else
333 ifn = &(pflogif[0].sc_if);
334
335 if (ifn->if_bpf)
336 bpf_mtap(ifn->if_bpf, &m1);
337#endif
338#endif
339
340 return (0);
341}
342
343#ifdef __FreeBSD__
344static int
345pflog_modevent(module_t mod, int type, void *data)
346{
347 int error = 0;
348
349 switch (type) {
350 case MOD_LOAD:
351 LIST_INIT(&pflog_list);
352 if_clone_attach(&pflog_cloner);
353 break;
354
355 case MOD_UNLOAD:
356 if_clone_detach(&pflog_cloner);
357 while (!LIST_EMPTY(&pflog_list))
358 pflog_clone_destroy(
359 &LIST_FIRST(&pflog_list)->sc_if);
360 break;
361
362 default:
363 error = EINVAL;
364 break;
365 }
366
367 return error;
368}
369
370static moduledata_t pflog_mod = {
371 "pflog",
372 pflog_modevent,
373 0
374};
375
376#define PFLOG_MODVER 1
377
378DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
379MODULE_VERSION(pflog, PFLOG_MODVER);
380#endif /* __FreeBSD__ */