1 /*
2 * Copyright (c) 2007-2023 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 ([email protected]),
33 * Angelos D. Keromytis ([email protected]) and
34 * Niels Provos ([email protected]).
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 #include <sys/sysctl.h>
73
74 #include <kern/zalloc.h>
75
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <net/if_types.h>
79 #include <net/route.h>
80 #include <net/bpf.h>
81
82 #if INET
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #endif
88
89 #if !INET
90 #include <netinet/in.h>
91 #endif
92 #include <netinet6/nd6.h>
93
94 #include <net/pfvar.h>
95 #include <net/if_pflog.h>
96
97 #define PFLOGNAME "pflog"
98 #define PFLOGMTU (32768 + MHLEN + MLEN)
99
100 #ifdef PFLOGDEBUG
101 #define DPRINTF(x) do { if (pflogdebug) printf x ; } while (0)
102 #else
103 #define DPRINTF(x)
104 #endif
105
106 static int pflog_remove(struct ifnet *);
107 static int pflog_clone_create(struct if_clone *, u_int32_t, void *);
108 static int pflog_clone_destroy(struct ifnet *);
109 static errno_t pflogoutput(struct ifnet *, struct mbuf *);
110 static errno_t pflogioctl(struct ifnet *, unsigned long, void *);
111 static errno_t pflogdemux(struct ifnet *, struct mbuf *, char *,
112 protocol_family_t *);
113 static errno_t pflogaddproto(struct ifnet *, protocol_family_t,
114 const struct ifnet_demux_desc *, u_int32_t);
115 static errno_t pflogdelproto(struct ifnet *, protocol_family_t);
116 static void pflogfree(struct ifnet *);
117
118 static LIST_HEAD(, pflog_softc) pflogif_list;
119 static struct if_clone pflog_cloner =
120 IF_CLONE_INITIALIZER(PFLOGNAME, pflog_clone_create, pflog_clone_destroy,
121 0, (PFLOGIFS_MAX - 1));
122
123 struct ifnet *pflogifs[PFLOGIFS_MAX]; /* for fast access */
124
125 void
pfloginit(void)126 pfloginit(void)
127 {
128 int i;
129
130 LIST_INIT(&pflogif_list);
131 for (i = 0; i < PFLOGIFS_MAX; i++) {
132 pflogifs[i] = NULL;
133 }
134
135 (void) if_clone_attach(&pflog_cloner);
136 }
137
138 #if !XNU_TARGET_OS_OSX
139 static inline bool
pflog_is_enabled(void)140 pflog_is_enabled(void)
141 {
142 uint8_t flags;
143 static uint8_t pflog_enabled_flags;
144
145 #define _FLAGS_INITIALIZED 0x1
146 #define _FLAGS_ENABLED 0x2
147
148 if (pflog_enabled_flags != 0) {
149 goto done;
150 }
151 flags = _FLAGS_INITIALIZED;
152 if (kern_osreleasetype_matches("Darwin") ||
153 kern_osreleasetype_matches("Restore") ||
154 kern_osreleasetype_matches("Internal") ||
155 kern_osreleasetype_matches("NonUI")) {
156 flags |= _FLAGS_ENABLED;
157 } else {
158 printf("%s: osreleasetype %s doesn't allow pflog\n", __func__,
159 osreleasetype);
160 }
161 lck_rw_lock_exclusive(&pf_perim_lock);
162 pflog_enabled_flags = flags;
163 lck_rw_done(&pf_perim_lock);
164
165 done:
166 return (pflog_enabled_flags & _FLAGS_ENABLED) != 0;
167 }
168 #endif /* !XNU_TARGET_OS_OSX */
169
170 static int
pflog_clone_create(struct if_clone * ifc,u_int32_t unit,__unused void * params)171 pflog_clone_create(struct if_clone *ifc, u_int32_t unit, __unused void *params)
172 {
173 struct pflog_softc *__single pflogif;
174 struct ifnet_init_eparams pf_init = { .name = NULL };
175 int error = 0;
176
177 #if !XNU_TARGET_OS_OSX
178 if (!pflog_is_enabled()) {
179 return EOPNOTSUPP;
180 }
181 #endif /* !XNU_TARGET_OS_OSX */
182
183 if (unit >= PFLOGIFS_MAX) {
184 /* Either the interface cloner or our initializer is broken */
185 panic("%s: unit (%d) exceeds max (%d)", __func__, unit,
186 PFLOGIFS_MAX);
187 /* NOTREACHED */
188 }
189
190 pflogif = kalloc_type(struct pflog_softc, Z_WAITOK_ZERO_NOFAIL);
191
192 bzero(&pf_init, sizeof(pf_init));
193 pf_init.ver = IFNET_INIT_CURRENT_VERSION;
194 pf_init.len = sizeof(pf_init);
195 pf_init.flags = IFNET_INIT_LEGACY;
196 pf_init.name = __unsafe_null_terminated_from_indexable(ifc->ifc_name);
197 pf_init.unit = unit;
198 pf_init.type = IFT_PFLOG;
199 pf_init.family = IFNET_FAMILY_LOOPBACK;
200 pf_init.output = pflogoutput;
201 pf_init.demux = pflogdemux;
202 pf_init.add_proto = pflogaddproto;
203 pf_init.del_proto = pflogdelproto;
204 pf_init.softc = pflogif;
205 pf_init.ioctl = pflogioctl;
206 pf_init.detach = pflogfree;
207
208 pflogif->sc_unit = unit;
209 pflogif->sc_flags |= IFPFLF_DETACHING;
210
211 error = ifnet_allocate_extended(&pf_init, &pflogif->sc_if);
212 if (error != 0) {
213 printf("%s: ifnet_allocate failed - %d\n", __func__, error);
214 kfree_type(struct pflog_softc, pflogif);
215 goto done;
216 }
217
218 ifnet_set_mtu(pflogif->sc_if, PFLOGMTU);
219 ifnet_set_flags(pflogif->sc_if, IFF_UP, IFF_UP);
220
221 error = ifnet_attach(pflogif->sc_if, NULL);
222 if (error != 0) {
223 printf("%s: ifnet_attach failed - %d\n", __func__, error);
224 ifnet_release(pflogif->sc_if);
225 kfree_type(struct pflog_softc, pflogif);
226 goto done;
227 }
228
229 #if NBPFILTER > 0
230 bpfattach(pflogif->sc_if, DLT_PFLOG, PFLOG_HDRLEN);
231 #endif
232
233 lck_rw_lock_shared(&pf_perim_lock);
234 lck_mtx_lock(&pf_lock);
235 LIST_INSERT_HEAD(&pflogif_list, pflogif, sc_list);
236 pflogifs[unit] = pflogif->sc_if;
237 pflogif->sc_flags &= ~IFPFLF_DETACHING;
238 lck_mtx_unlock(&pf_lock);
239 lck_rw_done(&pf_perim_lock);
240
241 done:
242 return error;
243 }
244
245 static int
pflog_remove(struct ifnet * ifp)246 pflog_remove(struct ifnet *ifp)
247 {
248 int error = 0;
249 struct pflog_softc *__single pflogif = NULL;
250
251 lck_rw_lock_shared(&pf_perim_lock);
252 lck_mtx_lock(&pf_lock);
253 pflogif = ifp->if_softc;
254
255 if (pflogif == NULL ||
256 (pflogif->sc_flags & IFPFLF_DETACHING) != 0) {
257 error = EINVAL;
258 goto done;
259 }
260
261 pflogif->sc_flags |= IFPFLF_DETACHING;
262 LIST_REMOVE(pflogif, sc_list);
263 done:
264 lck_mtx_unlock(&pf_lock);
265 lck_rw_done(&pf_perim_lock);
266 return error;
267 }
268
269 static int
pflog_clone_destroy(struct ifnet * ifp)270 pflog_clone_destroy(struct ifnet *ifp)
271 {
272 int error = 0;
273
274 if ((error = pflog_remove(ifp)) != 0) {
275 goto done;
276 }
277 /* bpfdetach() is taken care of as part of interface detach */
278 (void)ifnet_detach(ifp);
279 done:
280 return error;
281 }
282
283 static errno_t
pflogoutput(struct ifnet * ifp,struct mbuf * m)284 pflogoutput(struct ifnet *ifp, struct mbuf *m)
285 {
286 printf("%s: freeing data for %s\n", __func__, if_name(ifp));
287 m_freem(m);
288 return ENOTSUP;
289 }
290
291 static errno_t
pflogioctl(struct ifnet * ifp,unsigned long cmd,void * data)292 pflogioctl(struct ifnet *ifp, unsigned long cmd, void *data)
293 {
294 #pragma unused(data)
295 switch (cmd) {
296 case SIOCSIFADDR:
297 case SIOCAIFADDR:
298 case SIOCSIFDSTADDR:
299 case SIOCSIFFLAGS:
300 if (ifnet_flags(ifp) & IFF_UP) {
301 ifnet_set_flags(ifp, IFF_RUNNING, IFF_RUNNING);
302 } else {
303 ifnet_set_flags(ifp, 0, IFF_RUNNING);
304 }
305 break;
306 default:
307 return ENOTTY;
308 }
309
310 return 0;
311 }
312
313 static errno_t
pflogdemux(struct ifnet * ifp,struct mbuf * m,char * h,protocol_family_t * ppf)314 pflogdemux(struct ifnet *ifp, struct mbuf *m, char *h, protocol_family_t *ppf)
315 {
316 #pragma unused(h, ppf)
317 printf("%s: freeing data for %s\n", __func__, if_name(ifp));
318 m_freem(m);
319 return EJUSTRETURN;
320 }
321
322 static errno_t
pflogaddproto(struct ifnet * ifp,protocol_family_t pf,const struct ifnet_demux_desc * d,u_int32_t cnt)323 pflogaddproto(struct ifnet *ifp, protocol_family_t pf,
324 const struct ifnet_demux_desc *d, u_int32_t cnt)
325 {
326 #pragma unused(ifp, pf, d, cnt)
327 return 0;
328 }
329
330 static errno_t
pflogdelproto(struct ifnet * ifp,protocol_family_t pf)331 pflogdelproto(struct ifnet *ifp, protocol_family_t pf)
332 {
333 #pragma unused(ifp, pf)
334 return 0;
335 }
336
337 static void
pflogfree(struct ifnet * ifp)338 pflogfree(struct ifnet *ifp)
339 {
340 kfree_type(struct pflog_softc, ifp->if_softc);
341 ifp->if_softc = NULL;
342 (void) ifnet_release(ifp);
343 }
344
345 int
pflog_packet(struct pfi_kif * kif,pbuf_t * pbuf,sa_family_t af,u_int8_t dir,u_int8_t reason,struct pf_rule * rm,struct pf_rule * am,struct pf_ruleset * ruleset,struct pf_pdesc * pd)346 pflog_packet(struct pfi_kif *kif, pbuf_t *pbuf, sa_family_t af, u_int8_t dir,
347 u_int8_t reason, struct pf_rule *rm, struct pf_rule *am,
348 struct pf_ruleset *ruleset, struct pf_pdesc *pd)
349 {
350 #if NBPFILTER > 0
351 struct ifnet *ifn;
352 struct pfloghdr hdr;
353 struct mbuf *m;
354
355 LCK_MTX_ASSERT(&pf_lock, LCK_MTX_ASSERT_OWNED);
356
357 if (kif == NULL || !pbuf_is_valid(pbuf) || rm == NULL || pd == NULL) {
358 return -1;
359 }
360
361 if (rm->logif >= PFLOGIFS_MAX ||
362 (ifn = pflogifs[rm->logif]) == NULL || !ifn->if_bpf) {
363 return 0;
364 }
365
366 if ((m = pbuf_to_mbuf(pbuf, FALSE)) == NULL) {
367 return 0;
368 }
369
370 bzero(&hdr, sizeof(hdr));
371 hdr.length = PFLOG_REAL_HDRLEN;
372 hdr.af = af;
373 hdr.action = rm->action;
374 hdr.reason = reason;
375 memcpy(hdr.ifname, kif->pfik_name, sizeof(hdr.ifname));
376
377 if (am == NULL) {
378 hdr.rulenr = htonl(rm->nr);
379 hdr.subrulenr = -1;
380 } else {
381 hdr.rulenr = htonl(am->nr);
382 hdr.subrulenr = htonl(rm->nr);
383 if (ruleset != NULL && ruleset->anchor != NULL) {
384 strbufcpy(hdr.ruleset, ruleset->anchor->name);
385 }
386 }
387 if (rm->log & PF_LOG_SOCKET_LOOKUP && !pd->lookup.done) {
388 pd->lookup.done = pf_socket_lookup(dir, pd);
389 }
390 if (pd->lookup.done > 0) {
391 hdr.uid = pd->lookup.uid;
392 hdr.pid = pd->lookup.pid;
393 } else {
394 hdr.uid = UID_MAX;
395 hdr.pid = NO_PID;
396 }
397 hdr.rule_uid = rm->cuid;
398 hdr.rule_pid = rm->cpid;
399 hdr.dir = dir;
400
401 #if INET
402 if (af == AF_INET && dir == PF_OUT) {
403 struct ip *ip;
404
405 ip = mtod(m, struct ip *);
406 ip->ip_sum = 0;
407 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
408 }
409 #endif /* INET */
410
411 os_atomic_inc(&ifn->if_opackets, relaxed);
412 os_atomic_add(&ifn->if_obytes, m->m_pkthdr.len, relaxed);
413
414 switch (dir) {
415 case PF_IN:
416 bpf_tap_in(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
417 break;
418
419 case PF_OUT:
420 bpf_tap_out(ifn, DLT_PFLOG, m, &hdr, PFLOG_HDRLEN);
421 break;
422
423 default:
424 break;
425 }
426 #endif /* NBPFILTER > 0 */
427 return 0;
428 }
429