]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/net/if_pflog.c
xnu-1699.24.23.tar.gz
[apple/xnu.git] / bsd / net / if_pflog.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/* $apfw: if_pflog.c,v 1.4 2008/08/27 00:01:32 jhw Exp $ */
30/* $OpenBSD: if_pflog.c,v 1.22 2006/12/15 09:31:20 otto Exp $ */
31/*
32 * The authors of this code are John Ioannidis (ji@tla.org),
33 * Angelos D. Keromytis (kermit@csd.uch.gr) and
34 * Niels Provos (provos@physnet.uni-hamburg.de).
35 *
36 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
37 * in November 1995.
38 *
39 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
40 * by Angelos D. Keromytis.
41 *
42 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
43 * and Niels Provos.
44 *
45 * Copyright (C) 1995, 1996, 1997, 1998 by John Ioannidis, Angelos D. Keromytis
46 * and Niels Provos.
47 * Copyright (c) 2001, Angelos D. Keromytis, Niels Provos.
48 *
49 * Permission to use, copy, and modify this software with or without fee
50 * is hereby granted, provided that this entire notice is included in
51 * all copies of any software which is or includes a copy or
52 * modification of this software.
53 * You may use this code under the GNU public license if you so wish. Please
54 * contribute changes back to the authors under this freer than GPL license
55 * so that we may further the use of strong encryption without limitations to
56 * all.
57 *
58 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
59 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
60 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
61 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
62 * PURPOSE.
63 */
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/mbuf.h>
68#include <sys/proc_internal.h>
69#include <sys/socket.h>
70#include <sys/ioctl.h>
71#include <sys/mcache.h>
72
73#include <net/if.h>
74#include <net/if_var.h>
75#include <net/if_types.h>
76#include <net/route.h>
77#include <net/bpf.h>
78
79#if INET
80#include <netinet/in.h>
81#include <netinet/in_var.h>
82#include <netinet/in_systm.h>
83#include <netinet/ip.h>
84#endif
85
86#if INET6
87#if !INET
88#include <netinet/in.h>
89#endif
90#include <netinet6/nd6.h>
91#endif /* INET6 */
92
93#include <net/pfvar.h>
94#include <net/if_pflog.h>
95
96#define PFLOGNAME "pflog"
97#define PFLOGMTU (32768 + MHLEN + MLEN)
98
99#ifdef PFLOGDEBUG
100#define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
101#else
102#define DPRINTF(x)
103#endif
104
105static int pflog_clone_create(struct if_clone *, u_int32_t, void *);
106static int pflog_clone_destroy(struct ifnet *);
107static errno_t pflogoutput(struct ifnet *, struct mbuf *);
108static errno_t pflogioctl(struct ifnet *, unsigned long, void *);
109static errno_t pflogdemux(struct ifnet *, struct mbuf *, char *,
110 protocol_family_t *);
111static errno_t pflogaddproto(struct ifnet *, protocol_family_t,
112 const struct ifnet_demux_desc *, u_int32_t);
113static errno_t pflogdelproto(struct ifnet *, protocol_family_t);
114static void pflogfree(struct ifnet *);
115
116static LIST_HEAD(, pflog_softc) pflogif_list;
117static struct if_clone pflog_cloner =
118 IF_CLONE_INITIALIZER(PFLOGNAME, pflog_clone_create, pflog_clone_destroy,
119 0, (PFLOGIFS_MAX - 1));
120
121struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
122
123void
124pfloginit(void)
125{
126 int i;
127
128 if (pf_perim_lock == NULL || pf_lock == NULL) {
129 panic("%s: called before PF is initialized", __func__);
130 /* NOTREACHED */
131 }
132 LIST_INIT(&pflogif_list);
133 for (i = 0; i < PFLOGIFS_MAX; i++)
134 pflogifs[i] = NULL;
135
136 (void) if_clone_attach(&pflog_cloner);
137}
138
139static int
140pflog_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params)
141{
142 struct pflog_softc *pflogif;
143 struct ifnet_init_params pf_init;
144 int error = 0;
145
146 if (unit >= PFLOGIFS_MAX) {
147 /* Either the interface cloner or our initializer is broken */
148 panic("%s: unit (%d) exceeds max (%d)", __func__, unit,
149 PFLOGIFS_MAX);
150 /* NOTREACHED */
151 }
152
153 if ((pflogif = _MALLOC(sizeof (*pflogif),
154 M_DEVBUF, M_WAITOK|M_ZERO)) == NULL) {
155 error = ENOMEM;
156 goto done;
157 }
158
159 bzero(&pf_init, sizeof (pf_init));
160 pf_init.name = ifc->ifc_name;
161 pf_init.unit = unit;
162 pf_init.type = IFT_PFLOG;
163 pf_init.family = IFNET_FAMILY_LOOPBACK;
164 pf_init.output = pflogoutput;
165 pf_init.demux = pflogdemux;
166 pf_init.add_proto = pflogaddproto;
167 pf_init.del_proto = pflogdelproto;
168 pf_init.softc = pflogif;
169 pf_init.ioctl = pflogioctl;
170 pf_init.detach = pflogfree;
171
172 bzero(pflogif, sizeof (*pflogif));
173 pflogif->sc_unit = unit;
174
175 error = ifnet_allocate(&pf_init, &pflogif->sc_if);
176 if (error != 0) {
177 printf("%s: ifnet_allocate failed - %d\n", __func__, error);
178 _FREE(pflogif, M_DEVBUF);
179 goto done;
180 }
181
182 ifnet_set_mtu(pflogif->sc_if, PFLOGMTU);
183 ifnet_set_flags(pflogif->sc_if, IFF_UP, IFF_UP);
184
185 error = ifnet_attach(pflogif->sc_if, NULL);
186 if (error != 0) {
187 printf("%s: ifnet_attach failed - %d\n", __func__, error);
188 ifnet_release(pflogif->sc_if);
189 _FREE(pflogif, M_DEVBUF);
190 goto done;
191 }
192
193#if NBPFILTER > 0
194 bpfattach(pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
195#endif
196
197 lck_rw_lock_shared(pf_perim_lock);
198 lck_mtx_lock(pf_lock);
199 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
200 pflogifs[unit] = pflogif->sc_if;
201 lck_mtx_unlock(pf_lock);
202 lck_rw_done(pf_perim_lock);
203
204done:
205 return (error);
206}
207
208static int
209pflog_clone_destroy(struct ifnet *ifp)
210{
211 struct pflog_softc *pflogif = ifp->if_softc;
212
213 lck_rw_lock_shared(pf_perim_lock);
214 lck_mtx_lock(pf_lock);
215 pflogifs[pflogif->sc_unit] = NULL;
216 LIST_REMOVE(pflogif, sc_list);
217 lck_mtx_unlock(pf_lock);
218 lck_rw_done(pf_perim_lock);
219
220 /* bpfdetach() is taken care of as part of interface detach */
221 (void) ifnet_detach(ifp);
222
223 return 0;
224}
225
226static errno_t
227pflogoutput(struct ifnet *ifp, struct mbuf *m)
228{
229 printf("%s: freeing data for %s%d\n", __func__, ifp->if_name,
230 ifp->if_unit);
231 m_freem(m);
232 return (ENOTSUP);
233}
234
235static errno_t
236pflogioctl(struct ifnet *ifp, unsigned long cmd, void *data)
237{
238#pragma unused(data)
239 switch (cmd) {
240 case SIOCSIFADDR:
241 case SIOCAIFADDR:
242 case SIOCSIFDSTADDR:
243 case SIOCSIFFLAGS:
244 if (ifnet_flags(ifp) & IFF_UP)
245 ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
246 else
247 ifnet_set_flags(ifp, 0, IFF_RUNNING);
248 break;
249 default:
250 return (ENOTTY);
251 }
252
253 return (0);
254}
255
256static errno_t
257pflogdemux(struct ifnet *ifp, struct mbuf *m, char *h, protocol_family_t *ppf)
258{
259#pragma unused(h, ppf)
260 printf("%s: freeing data for %s%d\n", __func__, ifp->if_name,
261 ifp->if_unit);
262 m_freem(m);
263 return (EJUSTRETURN);
264}
265
266static errno_t
267pflogaddproto(struct ifnet *ifp, protocol_family_t pf,
268 const struct ifnet_demux_desc *d, u_int32_t cnt)
269{
270#pragma unused(ifp, pf, d, cnt)
271 return (0);
272}
273
274static errno_t
275pflogdelproto(struct ifnet *ifp, protocol_family_t pf)
276{
277#pragma unused(ifp, pf)
278 return (0);
279}
280
281static void
282pflogfree(struct ifnet *ifp)
283{
284 _FREE(ifp->if_softc, M_DEVBUF);
285 ifp->if_softc = NULL;
286 (void) ifnet_release(ifp);
287}
288
289int
290pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
291 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
292 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
293{
294#if NBPFILTER > 0
295 struct ifnet *ifn;
296 struct pfloghdr hdr;
297
298 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
299
300 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
301 return (-1);
302
303 if (rm->logif >= PFLOGIFS_MAX ||
304 (ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) {
305 return (0);
306 }
307
308 bzero(&hdr, sizeof (hdr));
309 hdr.length = PFLOG_REAL_HDRLEN;
310 hdr.af = af;
311 hdr.action = rm->action;
312 hdr.reason = reason;
313 memcpy(hdr.ifname, kif->pfik_name, sizeof (hdr.ifname));
314
315 if (am == NULL) {
316 hdr.rulenr = htonl(rm->nr);
317 hdr.subrulenr = -1;
318 } else {
319 hdr.rulenr = htonl(am->nr);
320 hdr.subrulenr = htonl(rm->nr);
321 if (ruleset != NULL && ruleset->anchor != NULL)
322 strlcpy(hdr.ruleset, ruleset->anchor->name,
323 sizeof (hdr.ruleset));
324 }
325 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
326 pd->lookup.done = pf_socket_lookup(dir, pd);
327 if (pd->lookup.done > 0) {
328 hdr.uid = pd->lookup.uid;
329 hdr.pid = pd->lookup.pid;
330 } else {
331 hdr.uid = UID_MAX;
332 hdr.pid = NO_PID;
333 }
334 hdr.rule_uid = rm->cuid;
335 hdr.rule_pid = rm->cpid;
336 hdr.dir = dir;
337
338#if INET
339 if (af == AF_INET && dir == PF_OUT) {
340 struct ip *ip;
341
342 ip = mtod(m, struct ip *);
343 ip->ip_sum = 0;
344 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
345 }
346#endif /* INET */
347
348 atomic_add_64(&ifn->if_opackets, 1);
349 atomic_add_64(&ifn->if_obytes, m->m_pkthdr.len);
350
351 switch (dir) {
352 case PF_IN:
353 bpf_tap_in(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
354 break;
355
356 case PF_OUT:
357 bpf_tap_out(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
358 break;
359
360 default:
361 break;
362 }
363#endif /* NBPFILTER > 0 */
364 return (0);
365}