Deleted Added
sdiff udiff text old ( 130613 ) new ( 130933 )
full compact
1/* $FreeBSD: head/sys/contrib/pf/net/if_pflog.c 130933 2004-06-22 20:13:25Z brooks $ */
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#if defined(__FreeBSD__)
67#include <net/if_clone.h>
68#endif
69#include <net/if_types.h>
70#include <net/route.h>
71#include <net/bpf.h>
72
73#ifdef INET
74#include <netinet/in.h>
75#include <netinet/in_var.h>
76#include <netinet/in_systm.h>
77#include <netinet/ip.h>
78#endif
79
80#ifdef __FreeBSD__
81#include <machine/in_cksum.h>
82#endif
83
84#ifdef INET6
85#ifndef INET
86#include <netinet/in.h>
87#endif
88#include <netinet6/nd6.h>
89#endif /* INET6 */
90
91#include <net/pfvar.h>
92#include <net/if_pflog.h>
93
94#ifdef __FreeBSD__
95#define PFLOGNAME "pflog"
96#endif
97
98#define PFLOGMTU (32768 + MHLEN + MLEN)
99
100#ifdef PFLOGDEBUG
101#define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
102#else
103#define DPRINTF(x)
104#endif
105
106#ifndef __FreeBSD__
107struct pflog_softc pflogif[NPFLOG];
108#endif
109
110#ifdef __FreeBSD__
111static void pflog_clone_destroy(struct ifnet *);
112static int pflog_clone_create(struct if_clone *, int);
113#else
114void pflogattach(int);
115#endif
116int pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
117 struct rtentry *);
118int pflogioctl(struct ifnet *, u_long, caddr_t);
119void pflogrtrequest(int, struct rtentry *, struct sockaddr *);
120void pflogstart(struct ifnet *);
121
122#ifndef __FreeBSD__
123extern int ifqmaxlen;
124#endif
125
126#ifdef __FreeBSD__
127static MALLOC_DEFINE(M_PFLOG, PFLOGNAME, "Packet Filter Logging Interface");
128static LIST_HEAD(pflog_list, pflog_softc) pflog_list;
129IFC_SIMPLE_DECLARE(pflog, 1);
130
131static void
132pflog_clone_destroy(struct ifnet *ifp)
133{
134 struct pflog_softc *sc;
135
136 sc = ifp->if_softc;
137
138 /*
139 * Does we really need this?
140 */
141 IF_DRAIN(&ifp->if_snd);
142
143 bpfdetach(ifp);
144 if_detach(ifp);
145 LIST_REMOVE(sc, sc_next);
146 free(sc, M_PFLOG);
147}
148
149static int
150pflog_clone_create(struct if_clone *ifc, int unit)
151{
152 struct pflog_softc *sc;
153
154 MALLOC(sc, struct pflog_softc *, sizeof(*sc), M_PFLOG, M_WAITOK|M_ZERO);
155
156 if_initname(&sc->sc_if, ifc->ifc_name, unit);
157 sc->sc_if.if_mtu = PFLOGMTU;
158 sc->sc_if.if_ioctl = pflogioctl;
159 sc->sc_if.if_output = pflogoutput;
160 sc->sc_if.if_start = pflogstart;
161 sc->sc_if.if_type = IFT_PFLOG;
162 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
163 sc->sc_if.if_hdrlen = PFLOG_HDRLEN;
164 sc->sc_if.if_softc = sc;
165 if_attach(&sc->sc_if);
166
167 LIST_INSERT_HEAD(&pflog_list, sc, sc_next);
168#if NBPFILTER > 0
169 bpfattach(&sc->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
170#endif
171
172 return (0);
173}
174#else /* !__FreeBSD__ */
175void
176pflogattach(int npflog)
177{
178 struct ifnet *ifp;
179 int i;
180
181 bzero(pflogif, sizeof(pflogif));
182
183 for (i = 0; i < NPFLOG; i++) {
184 ifp = &pflogif[i].sc_if;
185 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", i);
186 ifp->if_softc = &pflogif[i];
187 ifp->if_mtu = PFLOGMTU;
188 ifp->if_ioctl = pflogioctl;
189 ifp->if_output = pflogoutput;
190 ifp->if_start = pflogstart;
191 ifp->if_type = IFT_PFLOG;
192 ifp->if_snd.ifq_maxlen = ifqmaxlen;
193 ifp->if_hdrlen = PFLOG_HDRLEN;
194 if_attach(ifp);
195 if_alloc_sadl(ifp);
196
197#if NBPFILTER > 0
198 bpfattach(&pflogif[i].sc_if.if_bpf, ifp, DLT_PFLOG,
199 PFLOG_HDRLEN);
200#endif
201 }
202}
203#endif /* __FreeBSD__ */
204
205/*
206 * Start output on the pflog interface.
207 */
208void
209pflogstart(struct ifnet *ifp)
210{
211 struct mbuf *m;
212#ifndef __FreeBSD__
213 int s;
214#endif
215
216 for (;;) {
217#ifdef __FreeBSD__
218 IF_LOCK(&ifp->if_snd);
219 _IF_DROP(&ifp->if_snd);
220 _IF_DEQUEUE(&ifp->if_snd, m);
221 if (m == NULL) {
222 IF_UNLOCK(&ifp->if_snd);
223 return;
224 }
225 else
226 m_freem(m);
227 IF_UNLOCK(&ifp->if_snd);
228#else
229 s = splimp();
230 IF_DROP(&ifp->if_snd);
231 IF_DEQUEUE(&ifp->if_snd, m);
232 splx(s);
233 if (m == NULL)
234 return;
235 else
236 m_freem(m);
237#endif
238 }
239}
240
241int
242pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
243 struct rtentry *rt)
244{
245 m_freem(m);
246 return (0);
247}
248
249/* ARGSUSED */
250void
251pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
252{
253 if (rt)
254 rt->rt_rmx.rmx_mtu = PFLOGMTU;
255}
256
257/* ARGSUSED */
258int
259pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
260{
261 switch (cmd) {
262 case SIOCSIFADDR:
263 case SIOCAIFADDR:
264 case SIOCSIFDSTADDR:
265 case SIOCSIFFLAGS:
266 if (ifp->if_flags & IFF_UP)
267 ifp->if_flags |= IFF_RUNNING;
268 else
269 ifp->if_flags &= ~IFF_RUNNING;
270 break;
271 default:
272 return (EINVAL);
273 }
274
275 return (0);
276}
277
278int
279pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
280 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
281 struct pf_ruleset *ruleset)
282{
283#if NBPFILTER > 0
284 struct ifnet *ifn;
285 struct pfloghdr hdr;
286#ifndef __FreeBSD__
287 struct mbuf m1;
288#endif
289
290 if (kif == NULL || m == NULL || rm == NULL)
291 return (-1);
292
293 bzero(&hdr, sizeof(hdr));
294 hdr.length = PFLOG_REAL_HDRLEN;
295 hdr.af = af;
296 hdr.action = rm->action;
297 hdr.reason = reason;
298 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
299
300 if (am == NULL) {
301 hdr.rulenr = htonl(rm->nr);
302 hdr.subrulenr = -1;
303 } else {
304 hdr.rulenr = htonl(am->nr);
305 hdr.subrulenr = htonl(rm->nr);
306 if (ruleset != NULL)
307 memcpy(hdr.ruleset, ruleset->name,
308 sizeof(hdr.ruleset));
309
310
311 }
312 hdr.dir = dir;
313
314#ifdef INET
315 if (af == AF_INET && dir == PF_OUT) {
316 struct ip *ip;
317
318 ip = mtod(m, struct ip *);
319 ip->ip_sum = 0;
320 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
321 }
322#endif /* INET */
323
324#ifndef __FreeBSD__
325 m1.m_next = m;
326 m1.m_len = PFLOG_HDRLEN;
327 m1.m_data = (char *) &hdr;
328#endif
329
330#ifdef __FreeBSD__
331 KASSERT((!LIST_EMPTY(&pflog_list)), ("pflog: no interface"));
332 ifn = &LIST_FIRST(&pflog_list)->sc_if;
333 BPF_MTAP2(ifn, &hdr, sizeof(hdr), m);
334#else
335 ifn = &(pflogif[0].sc_if);
336
337 if (ifn->if_bpf)
338 bpf_mtap(ifn->if_bpf, &m1);
339#endif
340#endif
341
342 return (0);
343}
344
345#ifdef __FreeBSD__
346static int
347pflog_modevent(module_t mod, int type, void *data)
348{
349 int error = 0;
350
351 switch (type) {
352 case MOD_LOAD:
353 LIST_INIT(&pflog_list);
354 if_clone_attach(&pflog_cloner);
355 break;
356
357 case MOD_UNLOAD:
358 if_clone_detach(&pflog_cloner);
359 while (!LIST_EMPTY(&pflog_list))
360 pflog_clone_destroy(
361 &LIST_FIRST(&pflog_list)->sc_if);
362 break;
363
364 default:
365 error = EINVAL;
366 break;
367 }
368
369 return error;
370}
371
372static moduledata_t pflog_mod = {
373 "pflog",
374 pflog_modevent,
375 0
376};
377
378#define PFLOG_MODVER 1
379
380DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
381MODULE_VERSION(pflog, PFLOG_MODVER);
382#endif /* __FreeBSD__ */