if_pflog.c revision 1.13 1 /* $NetBSD: if_pflog.c,v 1.13 2008/11/07 00:20:13 dyoung Exp $ */
2 /* $OpenBSD: if_pflog.c,v 1.24 2007/05/26 17:13:30 jason Exp $ */
3
4 /*
5 * The authors of this code are John Ioannidis (ji (at) tla.org),
6 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
7 * Niels Provos (provos (at) physnet.uni-hamburg.de).
8 *
9 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
10 * in November 1995.
11 *
12 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
13 * by Angelos D. Keromytis.
14 *
15 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
16 * and Niels Provos.
17 *
18 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
19 * and Niels Provos.
20 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
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 <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: if_pflog.c,v 1.13 2008/11/07 00:20:13 dyoung Exp $");
40
41 #ifdef _KERNEL_OPT
42 #include "opt_inet.h"
43 #endif
44
45 #include "bpfilter.h"
46 #include "pflog.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/bpf.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #endif
66
67 #ifdef INET6
68 #ifndef INET
69 #include <netinet/in.h>
70 #endif
71 #include <netinet6/nd6.h>
72 #endif /* INET6 */
73
74 #include <net/pfvar.h>
75 #include <net/if_pflog.h>
76
77 #define PFLOGMTU (32768 + MHLEN + MLEN)
78
79 #ifdef PFLOGDEBUG
80 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
81 #else
82 #define DPRINTF(x)
83 #endif
84
85 void pflogattach(int);
86 #ifdef _LKM
87 void pflogdetach(void);
88 #endif /* _LKM */
89 int pflogoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
90 struct rtentry *);
91 int pflogioctl(struct ifnet *, u_long, void *);
92 void pflogstart(struct ifnet *);
93 int pflog_clone_create(struct if_clone *, int);
94 int pflog_clone_destroy(struct ifnet *);
95
96 LIST_HEAD(, pflog_softc) pflogif_list;
97 struct if_clone pflog_cloner =
98 IF_CLONE_INITIALIZER("pflog", pflog_clone_create, pflog_clone_destroy);
99
100 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
101
102 void
103 pflogattach(int npflog)
104 {
105 int i;
106
107 LIST_INIT(&pflogif_list);
108 for (i = 0; i < PFLOGIFS_MAX; i++)
109 pflogifs[i] = NULL;
110 if_clone_attach(&pflog_cloner);
111 }
112
113 #ifdef _LKM
114 void
115 pflogdetach(void)
116 {
117 int i;
118
119 for (i = 0; i < PFLOGIFS_MAX; i++) {
120 if (pflogifs[i] != NULL)
121 pflog_clone_destroy(pflogifs[i]);
122 }
123 if_clone_detach(&pflog_cloner);
124 }
125 #endif /* _LKM */
126
127 int
128 pflog_clone_create(struct if_clone *ifc, int unit)
129 {
130 struct ifnet *ifp;
131 struct pflog_softc *pflogif;
132 int s;
133
134 if (unit >= PFLOGIFS_MAX)
135 return (EINVAL);
136
137 if ((pflogif = malloc(sizeof(*pflogif), M_DEVBUF, M_NOWAIT)) == NULL)
138 return (ENOMEM);
139 bzero(pflogif, sizeof(*pflogif));
140
141 pflogif->sc_unit = unit;
142 ifp = &pflogif->sc_if;
143 snprintf(ifp->if_xname, sizeof ifp->if_xname, "pflog%d", unit);
144 ifp->if_softc = pflogif;
145 ifp->if_mtu = PFLOGMTU;
146 ifp->if_ioctl = pflogioctl;
147 ifp->if_output = pflogoutput;
148 ifp->if_start = pflogstart;
149 ifp->if_type = IFT_PFLOG;
150 #ifndef __NetBSD__
151 ifp->if_snd.ifq_maxlen = ifqmaxlen;
152 #endif /* !__NetBSD__ */
153 ifp->if_hdrlen = PFLOG_HDRLEN;
154 if_attach(ifp);
155 if_alloc_sadl(ifp);
156
157 #if NBPFILTER > 0
158 #ifdef __NetBSD__
159 bpfattach(ifp, DLT_PFLOG, PFLOG_HDRLEN);
160 #else
161 bpfattach(&pflogif->sc_if.if_bpf, ifp, DLT_PFLOG, PFLOG_HDRLEN);
162 #endif /* !__NetBSD__ */
163 #endif
164
165 s = splnet();
166 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
167 pflogifs[unit] = ifp;
168 splx(s);
169
170 return (0);
171 }
172
173 int
174 pflog_clone_destroy(struct ifnet *ifp)
175 {
176 struct pflog_softc *pflogif = ifp->if_softc;
177 int s;
178
179 s = splnet();
180 pflogifs[pflogif->sc_unit] = NULL;
181 LIST_REMOVE(pflogif, sc_list);
182 splx(s);
183
184 #if NBPFILTER > 0
185 bpfdetach(ifp);
186 #endif
187 if_detach(ifp);
188 free(pflogif, M_DEVBUF);
189 return (0);
190 }
191
192 /*
193 * Start output on the pflog interface.
194 */
195 void
196 pflogstart(struct ifnet *ifp)
197 {
198 struct mbuf *m;
199 int s;
200
201 for (;;) {
202 s = splnet();
203 IF_DROP(&ifp->if_snd);
204 IF_DEQUEUE(&ifp->if_snd, m);
205 splx(s);
206
207 if (m == NULL)
208 return;
209 else
210 m_freem(m);
211 }
212 }
213
214 int
215 pflogoutput(struct ifnet *ifp, struct mbuf *m,
216 const struct sockaddr *dst, struct rtentry *rt)
217 {
218 m_freem(m);
219 return (0);
220 }
221
222 /* ARGSUSED */
223 int
224 pflogioctl(struct ifnet *ifp, u_long cmd, void *data)
225 {
226 int error = 0;
227
228 switch (cmd) {
229 case SIOCSIFFLAGS:
230 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
231 break;
232 /*FALLTHROUGH*/
233 case SIOCINITIFADDR:
234 case SIOCAIFADDR:
235 case SIOCSIFDSTADDR:
236 if (ifp->if_flags & IFF_UP)
237 ifp->if_flags |= IFF_RUNNING;
238 else
239 ifp->if_flags &= ~IFF_RUNNING;
240 break;
241 default:
242 error = ifioctl_common(ifp, cmd, data);
243 }
244
245 return error;
246 }
247
248 int
249 pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
250 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
251 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
252 {
253 #if NBPFILTER > 0
254 struct ifnet *ifn;
255 struct pfloghdr hdr;
256
257 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
258 return (-1);
259
260 if ((ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf)
261 return (0);
262
263 bzero(&hdr, sizeof(hdr));
264 hdr.length = PFLOG_REAL_HDRLEN;
265 hdr.af = af;
266 hdr.action = rm->action;
267 hdr.reason = reason;
268 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
269
270 if (am == NULL) {
271 hdr.rulenr = htonl(rm->nr);
272 hdr.subrulenr = -1;
273 } else {
274 hdr.rulenr = htonl(am->nr);
275 hdr.subrulenr = htonl(rm->nr);
276 if (ruleset != NULL && ruleset->anchor != NULL)
277 strlcpy(hdr.ruleset, ruleset->anchor->name,
278 sizeof(hdr.ruleset));
279 }
280 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
281 pd->lookup.done = pf_socket_lookup(dir, pd);
282 if (pd->lookup.done > 0) {
283 hdr.uid = pd->lookup.uid;
284 hdr.pid = pd->lookup.pid;
285 } else {
286 hdr.uid = UID_MAX;
287 hdr.pid = NO_PID;
288 }
289 hdr.rule_uid = rm->cuid;
290 hdr.rule_pid = rm->cpid;
291 hdr.dir = dir;
292
293 #ifdef INET
294 if (af == AF_INET && dir == PF_OUT) {
295 struct ip *ip;
296
297 ip = mtod(m, struct ip *);
298 ip->ip_sum = 0;
299 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
300 }
301 #endif /* INET */
302
303 ifn->if_opackets++;
304 ifn->if_obytes += m->m_pkthdr.len;
305
306 #ifdef __NetBSD__
307 bpf_mtap2(ifn->if_bpf, &hdr, PFLOG_HDRLEN, m);
308 #else
309 bpf_mtap_hdr(ifn->if_bpf, (char *)&hdr, PFLOG_HDRLEN, m,
310 BPF_DIRECTION_OUT);
311 #endif /* !__NetBSD__ */
312
313 #endif
314
315 return (0);
316 }
317