]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_pflog.c
xnu-1504.9.37.tar.gz
[apple/xnu.git] / bsd / net / if_pflog.c
CommitLineData
b0d623f7 1/*
d1ecb069 2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
b0d623f7
A
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
72#include <net/if.h>
d1ecb069 73#include <net/if_var.h>
b0d623f7
A
74#include <net/if_types.h>
75#include <net/route.h>
76#include <net/bpf.h>
77
78#if INET
79#include <netinet/in.h>
80#include <netinet/in_var.h>
81#include <netinet/in_systm.h>
82#include <netinet/ip.h>
83#endif
84
85#if INET6
86#if !INET
87#include <netinet/in.h>
88#endif
89#include <netinet6/nd6.h>
90#endif /* INET6 */
91
92#include <net/pfvar.h>
93#include <net/if_pflog.h>
94
95#define PFLOGNAME "pflog"
96#define PFLOGMTU (32768 + MHLEN + MLEN)
97
98#ifdef PFLOGDEBUG
99#define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
100#else
101#define DPRINTF(x)
102#endif
103
d1ecb069
A
104static int pflog_clone_create(struct if_clone *, u_int32_t, void *);
105static int pflog_clone_destroy(struct ifnet *);
b0d623f7
A
106static errno_t pflogoutput(struct ifnet *, struct mbuf *);
107static errno_t pflogioctl(struct ifnet *, unsigned long, void *);
108static errno_t pflogdemux(struct ifnet *, struct mbuf *, char *,
109 protocol_family_t *);
110static errno_t pflogaddproto(struct ifnet *, protocol_family_t,
111 const struct ifnet_demux_desc *, u_int32_t);
112static errno_t pflogdelproto(struct ifnet *, protocol_family_t);
d1ecb069 113static void pflogfree(struct ifnet *);
b0d623f7
A
114
115static LIST_HEAD(, pflog_softc) pflogif_list;
d1ecb069
A
116static struct if_clone pflog_cloner =
117 IF_CLONE_INITIALIZER(PFLOGNAME, pflog_clone_create, pflog_clone_destroy,
118 0, (PFLOGIFS_MAX - 1));
b0d623f7
A
119
120struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
b0d623f7
A
121
122void
123pfloginit(void)
124{
125 int i;
126
d1ecb069
A
127 if (pf_perim_lock == NULL || pf_lock == NULL) {
128 panic("%s: called before PF is initialized", __func__);
b0d623f7
A
129 /* NOTREACHED */
130 }
131 LIST_INIT(&pflogif_list);
132 for (i = 0; i < PFLOGIFS_MAX; i++)
133 pflogifs[i] = NULL;
134
d1ecb069 135 (void) if_clone_attach(&pflog_cloner);
b0d623f7
A
136}
137
138static int
d1ecb069 139pflog_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params)
b0d623f7
A
140{
141 struct pflog_softc *pflogif;
142 struct ifnet_init_params pf_init;
143 int error = 0;
144
d1ecb069
A
145 if (unit >= PFLOGIFS_MAX) {
146 /* Either the interface cloner or our initializer is broken */
147 panic("%s: unit (%d) exceeds max (%d)", __func__, unit,
148 PFLOGIFS_MAX);
149 /* NOTREACHED */
b0d623f7
A
150 }
151
152 if ((pflogif = _MALLOC(sizeof (*pflogif),
153 M_DEVBUF, M_WAITOK|M_ZERO)) == NULL) {
154 error = ENOMEM;
155 goto done;
156 }
157
158 bzero(&pf_init, sizeof (pf_init));
d1ecb069
A
159 pf_init.name = ifc->ifc_name;
160 pf_init.unit = unit;
b0d623f7
A
161 pf_init.type = IFT_PFLOG;
162 pf_init.family = IFNET_FAMILY_LOOPBACK;
163 pf_init.output = pflogoutput;
164 pf_init.demux = pflogdemux;
165 pf_init.add_proto = pflogaddproto;
166 pf_init.del_proto = pflogdelproto;
167 pf_init.softc = pflogif;
168 pf_init.ioctl = pflogioctl;
d1ecb069 169 pf_init.detach = pflogfree;
b0d623f7
A
170
171 bzero(pflogif, sizeof (*pflogif));
d1ecb069 172 pflogif->sc_unit = unit;
b0d623f7
A
173
174 error = ifnet_allocate(&pf_init, &pflogif->sc_if);
175 if (error != 0) {
176 printf("%s: ifnet_allocate failed - %d\n", __func__, error);
177 _FREE(pflogif, M_DEVBUF);
178 goto done;
179 }
180
181 ifnet_set_mtu(pflogif->sc_if, PFLOGMTU);
182 ifnet_set_flags(pflogif->sc_if, IFF_UP, IFF_UP);
183
184 error = ifnet_attach(pflogif->sc_if, NULL);
185 if (error != 0) {
186 printf("%s: ifnet_attach failed - %d\n", __func__, error);
187 ifnet_release(pflogif->sc_if);
188 _FREE(pflogif, M_DEVBUF);
189 goto done;
190 }
191
192#if NBPFILTER > 0
193 bpfattach(pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
194#endif
195
d1ecb069
A
196 lck_rw_lock_shared(pf_perim_lock);
197 lck_mtx_lock(pf_lock);
b0d623f7 198 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
d1ecb069
A
199 pflogifs[unit] = pflogif->sc_if;
200 lck_mtx_unlock(pf_lock);
201 lck_rw_done(pf_perim_lock);
b0d623f7 202
d1ecb069 203done:
b0d623f7
A
204 return (error);
205}
206
d1ecb069
A
207static int
208pflog_clone_destroy(struct ifnet *ifp)
b0d623f7 209{
d1ecb069 210 struct pflog_softc *pflogif = ifp->if_softc;
b0d623f7 211
d1ecb069
A
212 lck_rw_lock_shared(pf_perim_lock);
213 lck_mtx_lock(pf_lock);
b0d623f7
A
214 pflogifs[pflogif->sc_unit] = NULL;
215 LIST_REMOVE(pflogif, sc_list);
d1ecb069
A
216 lck_mtx_unlock(pf_lock);
217 lck_rw_done(pf_perim_lock);
b0d623f7 218
d1ecb069
A
219 /* bpfdetach() is taken care of as part of interface detach */
220 (void) ifnet_detach(ifp);
221
222 return 0;
b0d623f7 223}
b0d623f7
A
224
225static errno_t
226pflogoutput(struct ifnet *ifp, struct mbuf *m)
227{
228 printf("%s: freeing data for %s%d\n", __func__, ifp->if_name,
229 ifp->if_unit);
230 m_freem(m);
231 return (ENOTSUP);
232}
233
234static errno_t
235pflogioctl(struct ifnet *ifp, unsigned long cmd, void *data)
236{
237#pragma unused(data)
238 switch (cmd) {
239 case SIOCSIFADDR:
240 case SIOCAIFADDR:
241 case SIOCSIFDSTADDR:
242 case SIOCSIFFLAGS:
243 if (ifnet_flags(ifp) & IFF_UP)
244 ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
245 else
246 ifnet_set_flags(ifp, 0, IFF_RUNNING);
247 break;
248 default:
249 return (ENOTTY);
250 }
251
252 return (0);
253}
254
255static errno_t
256pflogdemux(struct ifnet *ifp, struct mbuf *m, char *h, protocol_family_t *ppf)
257{
258#pragma unused(h, ppf)
259 printf("%s: freeing data for %s%d\n", __func__, ifp->if_name,
260 ifp->if_unit);
261 m_freem(m);
262 return (EJUSTRETURN);
263}
264
265static errno_t
266pflogaddproto(struct ifnet *ifp, protocol_family_t pf,
267 const struct ifnet_demux_desc *d, u_int32_t cnt)
268{
269#pragma unused(ifp, pf, d, cnt)
270 return (0);
271}
272
273static errno_t
274pflogdelproto(struct ifnet *ifp, protocol_family_t pf)
275{
276#pragma unused(ifp, pf)
277 return (0);
278}
279
d1ecb069
A
280static void
281pflogfree(struct ifnet *ifp)
282{
283 _FREE(ifp->if_softc, M_DEVBUF);
284 ifp->if_softc = NULL;
285 (void) ifnet_release(ifp);
286}
287
b0d623f7
A
288int
289pflog_packet(struct pfi_kif *kif, struct mbuf *m, sa_family_t af, u_int8_t dir,
290 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
291 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
292{
293#if NBPFILTER > 0
294 struct ifnet *ifn;
295 struct pfloghdr hdr;
296
d1ecb069
A
297 lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED);
298
b0d623f7
A
299 if (kif == NULL || m == NULL || rm == NULL || pd == NULL)
300 return (-1);
301
302 if (rm->logif >= PFLOGIFS_MAX ||
303 (ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) {
304 return (0);
305 }
306
307 bzero(&hdr, sizeof (hdr));
308 hdr.length = PFLOG_REAL_HDRLEN;
309 hdr.af = af;
310 hdr.action = rm->action;
311 hdr.reason = reason;
312 memcpy(hdr.ifname, kif->pfik_name, sizeof (hdr.ifname));
313
314 if (am == NULL) {
315 hdr.rulenr = htonl(rm->nr);
316 hdr.subrulenr = -1;
317 } else {
318 hdr.rulenr = htonl(am->nr);
319 hdr.subrulenr = htonl(rm->nr);
320 if (ruleset != NULL && ruleset->anchor != NULL)
321 strlcpy(hdr.ruleset, ruleset->anchor->name,
322 sizeof (hdr.ruleset));
323 }
324 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done)
325 pd->lookup.done = pf_socket_lookup(dir, pd);
326 if (pd->lookup.done > 0) {
327 hdr.uid = pd->lookup.uid;
328 hdr.pid = pd->lookup.pid;
329 } else {
330 hdr.uid = UID_MAX;
331 hdr.pid = NO_PID;
332 }
333 hdr.rule_uid = rm->cuid;
334 hdr.rule_pid = rm->cpid;
335 hdr.dir = dir;
336
337#if INET
338 if (af == AF_INET && dir == PF_OUT) {
339 struct ip *ip;
340
341 ip = mtod(m, struct ip *);
342 ip->ip_sum = 0;
343 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
344 }
345#endif /* INET */
346
347 ifn->if_opackets++;
348 ifn->if_obytes += m->m_pkthdr.len;
349
350 switch (dir) {
351 case PF_IN:
352 bpf_tap_in(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
353 break;
354
355 case PF_OUT:
356 bpf_tap_out(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
357 break;
358
359 default:
360 break;
361 }
362#endif /* NBPFILTER > 0 */
363 return (0);
364}