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