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