]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/contrib/pf/net/if_pflog.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / contrib / pf / net / if_pflog.c
1 /*      $OpenBSD: if_pflog.c,v 1.22 2006/12/15 09:31:20 otto Exp $      */
2 /*
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).
6  *
7  * This code was written by John Ioannidis for BSD/OS in Athens, Greece, 
8  * in November 1995.
9  *
10  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
11  * by Angelos D. Keromytis.
12  *
13  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
14  * and Niels Provos.
15  *
16  * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
17  * and Niels Provos.
18  * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
19  *
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
27  * all.
28  *
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
33  * PURPOSE.
34  */
35
36 #ifdef __FreeBSD__
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_bpf.h"
40 #include "opt_pf.h"
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #ifdef DEV_BPF
46 #define NBPFILTER       DEV_BPF
47 #else
48 #define NBPFILTER       0
49 #endif
50
51 #ifdef DEV_PFLOG
52 #define NPFLOG          DEV_PFLOG
53 #else
54 #define NPFLOG          0
55 #endif
56
57 #else /* ! __FreeBSD__ */
58 #include "bpfilter.h"
59 #include "pflog.h"
60 #endif /* __FreeBSD__ */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/mbuf.h>
65 #include <sys/proc.h>
66 #include <sys/socket.h>
67 #ifdef __FreeBSD__
68 #include <sys/kernel.h>
69 #include <sys/limits.h>
70 #include <sys/malloc.h>
71 #include <sys/module.h>
72 #include <sys/sockio.h>
73 #else
74 #include <sys/ioctl.h>
75 #endif
76
77 #include <net/if.h>
78 #ifdef __FreeBSD__
79 #include <net/if_clone.h>
80 #endif
81 #include <net/if_types.h>
82 #include <net/route.h>
83 #include <net/bpf.h>
84
85 #ifdef  INET
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #endif
91
92 #ifdef INET6
93 #ifndef INET
94 #include <netinet/in.h>
95 #endif
96 #include <netinet6/nd6.h>
97 #endif /* INET6 */
98
99 #include <net/pfvar.h>
100 #include <net/if_pflog.h>
101
102 #ifdef INET
103 #ifdef __FreeBSD__
104 #include <machine/in_cksum.h>
105 #endif
106 #endif
107
108 #define PFLOGMTU        (32768 + MHLEN + MLEN)
109
110 #ifdef PFLOGDEBUG
111 #define DPRINTF(x)    do { if (pflogdebug) printf x ; } while (0)
112 #else
113 #define DPRINTF(x)
114 #endif
115
116 void    pflogattach(int);
117 int     pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
118                        struct route *);
119 int     pflogioctl(struct ifnet *, u_long, caddr_t);
120 void    pflogstart(struct ifnet *);
121 #ifdef __FreeBSD__
122 static int pflog_clone_create(struct if_clone *, int, caddr_t);
123 static void pflog_clone_destroy(struct ifnet *);
124 #else
125 int     pflog_clone_create(struct if_clone *, int);
126 int     pflog_clone_destroy(struct ifnet *);
127 #endif
128
129 LIST_HEAD(, pflog_softc)        pflogif_list;
130 #ifdef __FreeBSD__
131 IFC_SIMPLE_DECLARE(pflog, 1);    
132 #else
133 struct if_clone pflog_cloner =
134     IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
135 #endif
136
137 struct ifnet    *pflogifs[PFLOGIFS_MAX];        /* for fast access */
138
139 #ifndef __FreeBSD__
140 extern int ifqmaxlen;
141 #endif
142
143 void
144 pflogattach(int npflog)
145 {
146         int     i;
147         LIST_INIT(&pflogif_list);
148         for (i = 0; i < PFLOGIFS_MAX; i++)
149                 pflogifs[i] = NULL;
150 #ifndef __FreeBSD__
151         (void) pflog_clone_create(&pflog_cloner, 0);
152 #endif
153         if_clone_attach(&pflog_cloner);
154 }
155
156 #ifdef __FreeBSD__
157 static int
158 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param)
159 #else
160 int
161 pflog_clone_create(struct if_clone *ifc, int unit)
162 #endif
163 {
164         struct ifnet *ifp;
165         struct pflog_softc *pflogif;
166         int s;
167
168         if (unit >= PFLOGIFS_MAX)
169                 return (EINVAL);
170
171         if ((pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_NOWAIT)) == NULL)
172                 return (ENOMEM);
173         bzero(pflogif, sizeof(*pflogif));
174
175         pflogif->sc_unit = unit;
176 #ifdef __FreeBSD__
177         ifp = pflogif->sc_ifp = if_alloc(IFT_PFLOG);
178         if (ifp == NULL) {
179                 free(pflogif, M_DEVBUF);
180                 return (ENOSPC);
181         }
182         if_initname(ifp, ifc->ifc_name, unit);
183 #else
184         ifp = &pflogif->sc_if;
185         snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
186 #endif
187         ifp->if_softc = pflogif;
188         ifp->if_mtu = PFLOGMTU;
189         ifp->if_ioctl = pflogioctl;
190         ifp->if_output = pflogoutput;
191         ifp->if_start = pflogstart;
192 #ifndef __FreeBSD__
193         ifp->if_type = IFT_PFLOG;
194 #endif
195         ifp->if_snd.ifq_maxlen = ifqmaxlen;
196         ifp->if_hdrlen = PFLOG_HDRLEN;
197         if_attach(ifp);
198 #ifndef __FreeBSD__
199         if_alloc_sadl(ifp);
200 #endif
201
202 #if NBPFILTER > 0
203 #ifdef __FreeBSD__
204         bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
205 #else
206         bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
207 #endif
208 #endif
209
210         s = splnet();
211 #ifdef __FreeBSD__
212         PF_LOCK();
213 #endif
214         LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
215         pflogifs[unit] = ifp;
216 #ifdef __FreeBSD__
217         PF_UNLOCK();
218 #endif
219         splx(s);
220
221         return (0);
222 }
223
224 #ifdef __FreeBSD__
225 static void
226 pflog_clone_destroy(struct ifnet *ifp)
227 #else
228 int
229 pflog_clone_destroy(struct ifnet *ifp)
230 #endif
231 {
232         struct pflog_softc      *pflogif = ifp->if_softc;
233         int                      s;
234
235         s = splnet();
236 #ifdef __FreeBSD__
237         PF_LOCK();
238 #endif
239         pflogifs[pflogif->sc_unit] = NULL;
240         LIST_REMOVE(pflogif, sc_list);
241 #ifdef __FreeBSD__
242         PF_UNLOCK();
243 #endif
244         splx(s);
245
246 #if NBPFILTER > 0
247         bpfdetach(ifp);
248 #endif
249         if_detach(ifp);
250 #ifdef __FreeBSD__
251         if_free(ifp);
252 #endif
253         free(pflogif, M_DEVBUF);
254 #ifndef __FreeBSD__
255         return (0);
256 #endif
257 }
258
259 /*
260  * Start output on the pflog interface.
261  */
262 void
263 pflogstart(struct ifnet *ifp)
264 {
265         struct mbuf *m;
266 #ifndef __FreeBSD__
267         int s;
268 #endif
269
270         for (;;) {
271 #ifdef __FreeBSD__
272                 IF_LOCK(&ifp->if_snd);
273                 _IF_DROP(&ifp->if_snd);
274                 _IF_DEQUEUE(&ifp->if_snd, m);
275                 IF_UNLOCK(&ifp->if_snd);
276 #else
277                 s = splnet();
278                 IF_DROP(&ifp->if_snd);
279                 IF_DEQUEUE(&ifp->if_snd, m);
280                 splx(s);
281 #endif
282
283                 if (m == NULL)
284                         return;
285                 else
286                         m_freem(m);
287         }
288 }
289
290 int
291 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
292        struct route *ro)
293 {
294         m_freem(m);
295         return (0);
296 }
297
298 /* ARGSUSED */
299 int
300 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
301 {
302         switch (cmd) {
303         case SIOCSIFADDR:
304         case SIOCAIFADDR:
305         case SIOCSIFDSTADDR:
306         case SIOCSIFFLAGS:
307 #ifdef __FreeBSD__
308                 if (ifp->if_flags & IFF_UP)
309                         ifp->if_drv_flags |= IFF_DRV_RUNNING;
310                 else
311                         ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
312 #else
313                 if (ifp->if_flags & IFF_UP)
314                         ifp->if_flags |= IFF_RUNNING;
315                 else
316                         ifp->if_flags &= ~IFF_RUNNING;
317 #endif
318                 break;
319         default:
320                 return (EINVAL);
321         }
322
323         return (0);
324 }
325
326 int
327 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
328     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
329     struct pf_ruleset *ruleset, struct pf_pdesc *pd)
330 {
331 #if NBPFILTER > 0
332         struct ifnet *ifn;
333         struct pfloghdr hdr;
334
335         if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
336                 return (-1);
337
338         if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
339                 return (0);
340
341         bzero(&hdr, sizeof(hdr));
342         hdr.length = PFLOG_REAL_HDRLEN;
343         hdr.af = af;
344         hdr.action = rm->action;
345         hdr.reason = reason;
346         memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
347
348         if (am == NULL) {
349                 hdr.rulenr = htonl(rm->nr);
350                 hdr.subrulenr = -1;
351         } else {
352                 hdr.rulenr = htonl(am->nr);
353                 hdr.subrulenr = htonl(rm->nr);
354                 if (ruleset != NULL && ruleset->anchor != NULL)
355                         strlcpy(hdr.ruleset, ruleset->anchor->name,
356                             sizeof(hdr.ruleset));
357         }
358         if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
359 #ifdef __FreeBSD__
360                 /* 
361                  * XXX: This should not happen as we force an early lookup
362                  * via debug.pfugidhack
363                  */
364                  ; /* empty */
365 #else
366                 pd->lookup.done = pf_socket_lookup(dir, pd);
367 #endif
368         if (pd->lookup.done > 0) {
369                 hdr.uid = pd->lookup.uid;
370                 hdr.pid = pd->lookup.pid;
371         } else {
372                 hdr.uid = UID_MAX;
373                 hdr.pid = NO_PID;
374         }
375         hdr.rule_uid = rm->cuid;
376         hdr.rule_pid = rm->cpid;
377         hdr.dir = dir;
378
379 #ifdef INET
380         if (af == AF_INET && dir == PF_OUT) {
381                 struct ip *ip;
382
383                 ip = mtod(m, struct ip *);
384                 ip->ip_sum = 0;
385                 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
386         }
387 #endif /* INET */
388
389         ifn->if_opackets++;
390         ifn->if_obytes += m->m_pkthdr.len;
391 #ifdef __FreeBSD__
392         BPF_MTAP2(ifn, &hdr, PFLOG_HDRLEN, m);
393 #else
394         bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
395             BPF_DIRECTION_OUT);
396 #endif
397 #endif
398
399         return (0);
400 }
401
402 #ifdef __FreeBSD__
403 static int
404 pflog_modevent(module_t mod, int type, void *data)
405 {
406         int error = 0;
407
408         switch (type) {
409         case MOD_LOAD:
410                 pflogattach(1);
411                 PF_LOCK();
412                 pflog_packet_ptr = pflog_packet;
413                 PF_UNLOCK();
414                 break;
415         case MOD_UNLOAD:
416                 PF_LOCK();
417                 pflog_packet_ptr = NULL;
418                 PF_UNLOCK();
419                 if_clone_detach(&pflog_cloner);
420                 break;
421         default:
422                 error = EINVAL;
423                 break;
424         }
425
426         return error;
427 }
428
429 static moduledata_t pflog_mod = { "pflog", pflog_modevent, 0 };
430
431 #define PFLOG_MODVER 1
432
433 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
434 MODULE_VERSION(pflog, PFLOG_MODVER);
435 MODULE_DEPEND(pflog, pf, PF_MODVER, PF_MODVER, PF_MODVER);
436 #endif /* __FreeBSD__ */