pf: Update to OpenBSD 4.2
[dragonfly.git] / sys / net / pf / if_pflog.c
1 /*      $OpenBSD: if_pflog.c,v 1.24 2007/05/26 17:13:30 jason 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  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
21  *
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
29  * all.
30  *
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
35  * PURPOSE.
36  */
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/in_cksum.h>
44 #include <sys/mbuf.h>
45 #include <sys/proc.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>
52 #include <vm/vm_zone.h>
53
54 #include <net/if.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
57 #include <net/bpf.h>
58
59 #ifdef  INET
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #endif
65
66 #ifdef INET6
67 #ifndef INET
68 #include <netinet/in.h>
69 #endif
70 #include <netinet6/nd6.h>
71 #endif /* INET6 */
72
73 #include <net/pf/pfvar.h>
74 #include <net/pf/if_pflog.h>
75
76 #define PFLOGNAME       "pflog"
77
78 #define PFLOGMTU        (32768 + MHLEN + MLEN)
79
80 #ifdef PFLOGDEBUG
81 #define DPRINTF(x)    do { if (pflogdebug) kprintf x ; } while (0)
82 #else
83 #define DPRINTF(x)
84 #endif
85
86 void    pflogattach(int);
87 int     pflogoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
88                        struct rtentry *);
89 int     pflogioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
90 void    pflogrtrequest(int, struct rtentry *, struct sockaddr *);
91 void    pflogstart(struct ifnet *);
92 int     pflog_clone_create(struct if_clone *, int, caddr_t);
93 void pflog_clone_destroy(struct ifnet *);
94
95 LIST_HEAD(, pflog_softc)        pflogif_list;
96 struct if_clone pflog_cloner =
97     IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy, 1, 1);
98
99 struct ifnet    *pflogifs[PFLOGIFS_MAX];        /* for fast access */
100
101 void
102 pflogattach(int npflog)
103 {
104         int     i;
105         LIST_INIT(&pflogif_list);
106         for (i = 0; i < PFLOGIFS_MAX; i++)
107                 pflogifs[i] = NULL;
108         if_clone_attach(&pflog_cloner);
109 }
110
111 int
112 pflog_clone_create(struct if_clone *ifc, int unit, caddr_t param __unused)
113 {
114         struct ifnet *ifp;
115         struct pflog_softc *pflogif;
116
117         if (unit >= PFLOGIFS_MAX)
118                 return (EINVAL);
119
120         if ((pflogif = kmalloc(sizeof(*pflogif), M_DEVBUF, M_WAITOK)) == NULL)
121                 return (ENOMEM);
122         bzero(pflogif, sizeof(*pflogif));
123
124         pflogif->sc_unit = unit;
125         ifp = &pflogif->sc_if;
126         ksnprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
127         ifp->if_softc = pflogif;
128         ifp->if_mtu = PFLOGMTU;
129         ifp->if_ioctl = pflogioctl;
130         ifp->if_output = pflogoutput;
131         ifp->if_start = pflogstart;
132         ifp->if_type = IFT_PFLOG;
133         ifp->if_snd.ifq_maxlen = ifqmaxlen;
134         ifp->if_hdrlen = PFLOG_HDRLEN;
135         if_attach(ifp, NULL);
136
137         bpfattach(&pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
138
139         crit_enter();
140         LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
141         pflogifs[unit] = ifp;
142         crit_exit();
143
144         return (0);
145 }
146
147 void
148 pflog_clone_destroy(struct ifnet *ifp)
149 {
150         struct pflog_softc      *pflogif = ifp->if_softc;
151
152         crit_enter();
153         pflogifs[pflogif->sc_unit] = NULL;
154         LIST_REMOVE(pflogif, sc_list);
155         crit_exit();
156
157 #if NBPFILTER > 0
158         bpfdetach(ifp);
159 #endif
160         if_detach(ifp);
161         kfree(pflogif, M_DEVBUF);
162 }
163
164 /*
165  * Start output on the pflog interface.
166  */
167 void
168 pflogstart(struct ifnet *ifp)
169 {
170         struct mbuf *m;
171
172         for (;;) {
173                 crit_enter();
174                 IF_DROP(&ifp->if_snd);
175                 IF_DEQUEUE(&ifp->if_snd, m);
176                 crit_exit();
177
178                 if (m == NULL)
179                         return;
180                 else
181                         m_freem(m);
182         }
183 }
184
185 int
186 pflogoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
187         struct rtentry *rt)
188 {
189         m_freem(m);
190         return (0);
191 }
192
193 /* ARGSUSED */
194 void
195 pflogrtrequest(int cmd, struct rtentry *rt, struct sockaddr *sa)
196 {
197         if (rt)
198                 rt->rt_rmx.rmx_mtu = PFLOGMTU;
199 }
200
201 /* ARGSUSED */
202 int
203 pflogioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
204 {
205         switch (cmd) {
206         case SIOCSIFADDR:
207         case SIOCAIFADDR:
208         case SIOCSIFDSTADDR:
209         case SIOCSIFFLAGS:
210                 if (ifp->if_flags & IFF_UP)
211                         ifp->if_flags |= IFF_RUNNING;
212                 else
213                         ifp->if_flags &= ~IFF_RUNNING;
214                 break;
215         default:
216                 return (EINVAL);
217         }
218
219         return (0);
220 }
221
222 int
223 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
224     u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
225     struct pf_ruleset *ruleset, struct pf_pdesc *pd)
226 {
227         struct ifnet *ifn = NULL;
228         struct pfloghdr hdr;
229
230         if (kif == NULL || m == NULL || rm == NULL)
231                 return (-1);
232
233         if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
234                 return (0);
235
236         bzero(&hdr, sizeof(hdr));
237         hdr.length = PFLOG_REAL_HDRLEN;
238         hdr.af = af;
239         hdr.action = rm->action;
240         hdr.reason = reason;
241         memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
242
243         if (am == NULL) {
244                 hdr.rulenr = htonl(rm->nr);
245                 hdr.subrulenr = -1;
246         } else {
247                 hdr.rulenr = htonl(am->nr);
248                 hdr.subrulenr = htonl(rm->nr);
249                 if (ruleset != NULL && ruleset->anchor != NULL)
250                         strlcpy(hdr.ruleset, ruleset->anchor->name,
251                             sizeof(hdr.ruleset));
252         }
253         if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
254                 pd->lookup.done = pf_socket_lookup(dir, pd);
255         if (pd->lookup.done > 0) {
256                 hdr.uid = pd->lookup.uid;
257                 hdr.pid = pd->lookup.pid;
258         } else {
259                 hdr.uid = UID_MAX;
260                 hdr.pid = NO_PID;
261         }
262         hdr.rule_uid = rm->cuid;
263         hdr.rule_pid = rm->cpid;
264         hdr.dir = dir;
265
266 #ifdef INET
267         if (af == AF_INET) {
268                 struct ip *ip;
269                 ip = mtod(m, struct ip *);      
270                 ip->ip_len = htons(ip->ip_len);
271                 ip->ip_off = htons(ip->ip_off);
272
273                 if (dir == PF_OUT) {
274                         ip->ip_sum = 0;
275                         ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
276                 }                               
277         }
278 #endif /* INET */
279
280         ifn->if_opackets++;
281         ifn->if_obytes += m->m_pkthdr.len;
282         bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
283             BPF_DIRECTION_OUT);
284
285 #ifdef INET
286         if (af == AF_INET) {
287                 struct ip *ip = mtod(m, struct ip *);
288
289                 ip->ip_len = ntohs(ip->ip_len);
290                 ip->ip_off = ntohs(ip->ip_off);
291         }
292 #endif /* INET */
293
294         return (0);
295 }
296
297 static int
298 pflog_modevent(module_t mod, int type, void *data)
299 {
300         int error = 0;
301
302         switch (type) {
303         case MOD_LOAD:
304                 LIST_INIT(&pflogif_list);
305                 if_clone_attach(&pflog_cloner);
306                 break;
307
308         case MOD_UNLOAD:
309                 if_clone_detach(&pflog_cloner);
310                 while (!LIST_EMPTY(&pflogif_list)) {
311                         pflog_clone_destroy(
312                                 &LIST_FIRST(&pflogif_list)->sc_if);
313                 }
314                 break;
315
316         default:
317                 error = EINVAL;
318                 break;
319         }
320
321         return error;
322 }
323
324 static moduledata_t pflog_mod = {
325         "pflog",
326         pflog_modevent,
327         0
328 };
329
330 #define PFLOG_MODVER 1
331
332 DECLARE_MODULE(pflog, pflog_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
333 MODULE_VERSION(pflog, PFLOG_MODVER);