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