]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
6d2010ae | 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: pf_if.c,v 1.4 2008/08/27 00:01:32 jhw Exp $ */ | |
30 | /* $OpenBSD: pf_if.c,v 1.46 2006/12/13 09:01:59 itojun Exp $ */ | |
31 | ||
32 | /* | |
33 | * Copyright 2005 Henning Brauer <henning@openbsd.org> | |
34 | * Copyright 2005 Ryan McBride <mcbride@openbsd.org> | |
35 | * Copyright (c) 2001 Daniel Hartmeier | |
36 | * Copyright (c) 2003 Cedric Berger | |
37 | * All rights reserved. | |
38 | * | |
39 | * Redistribution and use in source and binary forms, with or without | |
40 | * modification, are permitted provided that the following conditions | |
41 | * are met: | |
42 | * | |
43 | * - Redistributions of source code must retain the above copyright | |
44 | * notice, this list of conditions and the following disclaimer. | |
45 | * - Redistributions in binary form must reproduce the above | |
46 | * copyright notice, this list of conditions and the following | |
47 | * disclaimer in the documentation and/or other materials provided | |
48 | * with the distribution. | |
49 | * | |
50 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
51 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
52 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
53 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
54 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
55 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
56 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
57 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
58 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
59 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
60 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
61 | * POSSIBILITY OF SUCH DAMAGE. | |
62 | */ | |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/systm.h> | |
66 | #include <sys/mbuf.h> | |
67 | #include <sys/filio.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/socketvar.h> | |
70 | #include <sys/kernel.h> | |
71 | #include <sys/time.h> | |
72 | #include <sys/malloc.h> | |
73 | ||
74 | #include <net/if.h> | |
75 | #include <net/if_types.h> | |
76 | #include <net/if_var.h> | |
77 | ||
78 | #include <netinet/in.h> | |
79 | #include <netinet/in_var.h> | |
80 | #include <netinet/in_systm.h> | |
81 | #include <netinet/ip.h> | |
82 | #include <netinet/ip_var.h> | |
83 | ||
84 | #if INET6 | |
85 | #include <netinet/ip6.h> | |
86 | #endif /* INET6 */ | |
87 | ||
88 | #include <net/pfvar.h> | |
89 | ||
90 | struct pfi_kif *pfi_all = NULL; | |
91 | ||
92 | static struct pool pfi_addr_pl; | |
93 | static struct pfi_ifhead pfi_ifs; | |
94 | static u_int32_t pfi_update = 1; | |
95 | static struct pfr_addr *pfi_buffer; | |
96 | static int pfi_buffer_cnt; | |
97 | static int pfi_buffer_max; | |
98 | ||
99 | __private_extern__ void pfi_kifaddr_update(void *); | |
100 | ||
101 | static void pfi_kif_update(struct pfi_kif *); | |
102 | static void pfi_dynaddr_update(struct pfi_dynaddr *dyn); | |
103 | static void pfi_table_update(struct pfr_ktable *, struct pfi_kif *, int, int); | |
104 | static void pfi_instance_add(struct ifnet *, int, int); | |
105 | static void pfi_address_add(struct sockaddr *, int, int); | |
106 | static int pfi_if_compare(struct pfi_kif *, struct pfi_kif *); | |
107 | static int pfi_skip_if(const char *, struct pfi_kif *); | |
108 | static int pfi_unmask(void *); | |
109 | ||
110 | RB_PROTOTYPE_SC(static, pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); | |
111 | RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); | |
112 | ||
113 | #define PFI_BUFFER_MAX 0x10000 | |
114 | #define PFI_MTYPE M_IFADDR | |
115 | ||
116 | #define IFG_ALL "ALL" | |
117 | ||
118 | void | |
119 | pfi_initialize(void) | |
120 | { | |
121 | if (pfi_all != NULL) /* already initialized */ | |
122 | return; | |
123 | ||
124 | pool_init(&pfi_addr_pl, sizeof (struct pfi_dynaddr), 0, 0, 0, | |
125 | "pfiaddrpl", NULL); | |
126 | pfi_buffer_max = 64; | |
127 | pfi_buffer = _MALLOC(pfi_buffer_max * sizeof (*pfi_buffer), | |
128 | PFI_MTYPE, M_WAITOK); | |
129 | ||
130 | if ((pfi_all = pfi_kif_get(IFG_ALL)) == NULL) | |
131 | panic("pfi_kif_get for pfi_all failed"); | |
132 | } | |
133 | ||
134 | #if 0 | |
135 | void | |
136 | pfi_destroy(void) | |
137 | { | |
138 | pool_destroy(&pfi_addr_pl); | |
139 | _FREE(pfi_buffer, PFI_MTYPE); | |
140 | } | |
141 | #endif | |
142 | ||
143 | struct pfi_kif * | |
144 | pfi_kif_get(const char *kif_name) | |
145 | { | |
146 | struct pfi_kif *kif; | |
147 | struct pfi_kif_cmp s; | |
148 | ||
149 | bzero(&s, sizeof (s)); | |
150 | strlcpy(s.pfik_name, kif_name, sizeof (s.pfik_name)); | |
151 | if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&s)) != NULL) | |
152 | return (kif); | |
153 | ||
154 | /* create new one */ | |
155 | if ((kif = _MALLOC(sizeof (*kif), PFI_MTYPE, M_WAITOK|M_ZERO)) == NULL) | |
156 | return (NULL); | |
157 | ||
158 | strlcpy(kif->pfik_name, kif_name, sizeof (kif->pfik_name)); | |
d1ecb069 | 159 | kif->pfik_tzero = pf_calendar_time_second(); |
b0d623f7 A |
160 | TAILQ_INIT(&kif->pfik_dynaddrs); |
161 | ||
162 | RB_INSERT(pfi_ifhead, &pfi_ifs, kif); | |
163 | return (kif); | |
164 | } | |
165 | ||
166 | void | |
167 | pfi_kif_ref(struct pfi_kif *kif, enum pfi_kif_refs what) | |
168 | { | |
169 | switch (what) { | |
170 | case PFI_KIF_REF_RULE: | |
171 | kif->pfik_rules++; | |
172 | break; | |
173 | case PFI_KIF_REF_STATE: | |
174 | kif->pfik_states++; | |
175 | break; | |
176 | default: | |
177 | panic("pfi_kif_ref with unknown type"); | |
178 | } | |
179 | } | |
180 | ||
181 | void | |
182 | pfi_kif_unref(struct pfi_kif *kif, enum pfi_kif_refs what) | |
183 | { | |
184 | if (kif == NULL) | |
185 | return; | |
186 | ||
187 | switch (what) { | |
188 | case PFI_KIF_REF_NONE: | |
189 | break; | |
190 | case PFI_KIF_REF_RULE: | |
191 | if (kif->pfik_rules <= 0) { | |
192 | printf("pfi_kif_unref: rules refcount <= 0\n"); | |
193 | return; | |
194 | } | |
195 | kif->pfik_rules--; | |
196 | break; | |
197 | case PFI_KIF_REF_STATE: | |
198 | if (kif->pfik_states <= 0) { | |
199 | printf("pfi_kif_unref: state refcount <= 0\n"); | |
200 | return; | |
201 | } | |
202 | kif->pfik_states--; | |
203 | break; | |
204 | default: | |
205 | panic("pfi_kif_unref with unknown type"); | |
206 | } | |
207 | ||
208 | if (kif->pfik_ifp != NULL || kif == pfi_all) | |
209 | return; | |
210 | ||
211 | if (kif->pfik_rules || kif->pfik_states) | |
212 | return; | |
213 | ||
214 | RB_REMOVE(pfi_ifhead, &pfi_ifs, kif); | |
215 | _FREE(kif, PFI_MTYPE); | |
216 | } | |
217 | ||
218 | int | |
219 | pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif) | |
220 | { | |
221 | ||
222 | if (rule_kif == NULL || rule_kif == packet_kif) | |
223 | return (1); | |
224 | ||
225 | return (0); | |
226 | } | |
227 | ||
228 | void | |
229 | pfi_attach_ifnet(struct ifnet *ifp) | |
230 | { | |
231 | struct pfi_kif *kif; | |
232 | char if_name[IFNAMSIZ]; | |
233 | ||
234 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
235 | ||
236 | pfi_update++; | |
237 | (void) snprintf(if_name, sizeof (if_name), "%s%d", | |
238 | ifp->if_name, ifp->if_unit); | |
239 | if ((kif = pfi_kif_get(if_name)) == NULL) | |
240 | panic("pfi_kif_get failed"); | |
241 | ||
242 | ifnet_lock_exclusive(ifp); | |
243 | kif->pfik_ifp = ifp; | |
244 | ifp->if_pf_kif = kif; | |
245 | ifnet_lock_done(ifp); | |
246 | ||
247 | pfi_kif_update(kif); | |
248 | } | |
249 | ||
250 | /* | |
251 | * Caller holds ifnet lock as writer (exclusive); | |
252 | */ | |
253 | void | |
254 | pfi_detach_ifnet(struct ifnet *ifp) | |
255 | { | |
256 | struct pfi_kif *kif; | |
257 | ||
258 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
259 | ||
260 | if ((kif = (struct pfi_kif *)ifp->if_pf_kif) == NULL) | |
261 | return; | |
262 | ||
263 | pfi_update++; | |
264 | pfi_kif_update(kif); | |
265 | ||
266 | ifnet_lock_exclusive(ifp); | |
267 | kif->pfik_ifp = NULL; | |
268 | ifp->if_pf_kif = NULL; | |
269 | ifnet_lock_done(ifp); | |
270 | ||
271 | pfi_kif_unref(kif, PFI_KIF_REF_NONE); | |
272 | } | |
273 | ||
274 | int | |
275 | pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af) | |
276 | { | |
277 | switch (af) { | |
278 | #if INET | |
279 | case AF_INET: | |
280 | switch (dyn->pfid_acnt4) { | |
281 | case 0: | |
282 | return (0); | |
283 | case 1: | |
284 | return (PF_MATCHA(0, &dyn->pfid_addr4, | |
285 | &dyn->pfid_mask4, a, AF_INET)); | |
286 | default: | |
287 | return (pfr_match_addr(dyn->pfid_kt, a, AF_INET)); | |
288 | } | |
289 | break; | |
290 | #endif /* INET */ | |
291 | #if INET6 | |
292 | case AF_INET6: | |
293 | switch (dyn->pfid_acnt6) { | |
294 | case 0: | |
295 | return (0); | |
296 | case 1: | |
297 | return (PF_MATCHA(0, &dyn->pfid_addr6, | |
298 | &dyn->pfid_mask6, a, AF_INET6)); | |
299 | default: | |
300 | return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6)); | |
301 | } | |
302 | break; | |
303 | #endif /* INET6 */ | |
304 | default: | |
305 | return (0); | |
306 | } | |
307 | } | |
308 | ||
309 | int | |
310 | pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af) | |
311 | { | |
312 | struct pfi_dynaddr *dyn; | |
313 | char tblname[PF_TABLE_NAME_SIZE]; | |
314 | struct pf_ruleset *ruleset = NULL; | |
315 | int rv = 0; | |
316 | ||
317 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
318 | ||
319 | if (aw->type != PF_ADDR_DYNIFTL) | |
320 | return (0); | |
321 | if ((dyn = pool_get(&pfi_addr_pl, PR_WAITOK)) == NULL) | |
322 | return (1); | |
323 | bzero(dyn, sizeof (*dyn)); | |
324 | ||
325 | if (strcmp(aw->v.ifname, "self") == 0) | |
326 | dyn->pfid_kif = pfi_kif_get(IFG_ALL); | |
327 | else | |
328 | dyn->pfid_kif = pfi_kif_get(aw->v.ifname); | |
329 | if (dyn->pfid_kif == NULL) { | |
330 | rv = 1; | |
331 | goto _bad; | |
332 | } | |
333 | pfi_kif_ref(dyn->pfid_kif, PFI_KIF_REF_RULE); | |
334 | ||
335 | dyn->pfid_net = pfi_unmask(&aw->v.a.mask); | |
336 | if (af == AF_INET && dyn->pfid_net == 32) | |
337 | dyn->pfid_net = 128; | |
338 | strlcpy(tblname, aw->v.ifname, sizeof (tblname)); | |
339 | if (aw->iflags & PFI_AFLAG_NETWORK) | |
340 | strlcat(tblname, ":network", sizeof (tblname)); | |
341 | if (aw->iflags & PFI_AFLAG_BROADCAST) | |
342 | strlcat(tblname, ":broadcast", sizeof (tblname)); | |
343 | if (aw->iflags & PFI_AFLAG_PEER) | |
344 | strlcat(tblname, ":peer", sizeof (tblname)); | |
345 | if (aw->iflags & PFI_AFLAG_NOALIAS) | |
346 | strlcat(tblname, ":0", sizeof (tblname)); | |
347 | if (dyn->pfid_net != 128) | |
348 | snprintf(tblname + strlen(tblname), | |
349 | sizeof (tblname) - strlen(tblname), "/%d", dyn->pfid_net); | |
350 | if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) { | |
351 | rv = 1; | |
352 | goto _bad; | |
353 | } | |
354 | ||
355 | if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) { | |
356 | rv = 1; | |
357 | goto _bad; | |
358 | } | |
359 | ||
360 | dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE; | |
361 | dyn->pfid_iflags = aw->iflags; | |
362 | dyn->pfid_af = af; | |
363 | ||
364 | TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry); | |
365 | aw->p.dyn = dyn; | |
366 | pfi_kif_update(dyn->pfid_kif); | |
367 | return (0); | |
368 | ||
369 | _bad: | |
370 | if (dyn->pfid_kt != NULL) | |
371 | pfr_detach_table(dyn->pfid_kt); | |
372 | if (ruleset != NULL) | |
373 | pf_remove_if_empty_ruleset(ruleset); | |
374 | if (dyn->pfid_kif != NULL) | |
375 | pfi_kif_unref(dyn->pfid_kif, PFI_KIF_REF_RULE); | |
376 | pool_put(&pfi_addr_pl, dyn); | |
377 | return (rv); | |
378 | } | |
379 | ||
380 | void | |
381 | pfi_kif_update(struct pfi_kif *kif) | |
382 | { | |
383 | struct pfi_dynaddr *p; | |
384 | ||
385 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
386 | ||
387 | /* update all dynaddr */ | |
388 | TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry) | |
389 | pfi_dynaddr_update(p); | |
390 | } | |
391 | ||
392 | void | |
393 | pfi_dynaddr_update(struct pfi_dynaddr *dyn) | |
394 | { | |
395 | struct pfi_kif *kif; | |
396 | struct pfr_ktable *kt; | |
397 | ||
398 | if (dyn == NULL || dyn->pfid_kif == NULL || dyn->pfid_kt == NULL) | |
399 | panic("pfi_dynaddr_update"); | |
400 | ||
401 | kif = dyn->pfid_kif; | |
402 | kt = dyn->pfid_kt; | |
403 | ||
404 | if (kt->pfrkt_larg != pfi_update) { | |
405 | /* this table needs to be brought up-to-date */ | |
406 | pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags); | |
407 | kt->pfrkt_larg = pfi_update; | |
408 | } | |
409 | pfr_dynaddr_update(kt, dyn); | |
410 | } | |
411 | ||
412 | void | |
413 | pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags) | |
414 | { | |
415 | int e, size2 = 0; | |
416 | ||
417 | pfi_buffer_cnt = 0; | |
418 | ||
419 | if (kif->pfik_ifp != NULL) | |
420 | pfi_instance_add(kif->pfik_ifp, net, flags); | |
421 | ||
d1ecb069 A |
422 | if ((e = pfr_set_addrs(&kt->pfrkt_t, CAST_USER_ADDR_T(pfi_buffer), |
423 | pfi_buffer_cnt, &size2, NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK))) | |
b0d623f7 A |
424 | printf("pfi_table_update: cannot set %d new addresses " |
425 | "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e); | |
426 | } | |
427 | ||
428 | void | |
429 | pfi_instance_add(struct ifnet *ifp, int net, int flags) | |
430 | { | |
431 | struct ifaddr *ia; | |
432 | int got4 = 0, got6 = 0; | |
433 | int net2, af; | |
434 | ||
435 | if (ifp == NULL) | |
436 | return; | |
437 | ifnet_lock_shared(ifp); | |
438 | TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { | |
6d2010ae A |
439 | IFA_LOCK(ia); |
440 | if (ia->ifa_addr == NULL) { | |
441 | IFA_UNLOCK(ia); | |
b0d623f7 | 442 | continue; |
6d2010ae | 443 | } |
b0d623f7 | 444 | af = ia->ifa_addr->sa_family; |
6d2010ae A |
445 | if (af != AF_INET && af != AF_INET6) { |
446 | IFA_UNLOCK(ia); | |
b0d623f7 | 447 | continue; |
6d2010ae A |
448 | } |
449 | if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6) { | |
450 | IFA_UNLOCK(ia); | |
b0d623f7 | 451 | continue; |
6d2010ae | 452 | } |
b0d623f7 | 453 | if ((flags & PFI_AFLAG_BROADCAST) && |
6d2010ae A |
454 | !(ifp->if_flags & IFF_BROADCAST)) { |
455 | IFA_UNLOCK(ia); | |
b0d623f7 | 456 | continue; |
6d2010ae | 457 | } |
b0d623f7 | 458 | if ((flags & PFI_AFLAG_PEER) && |
6d2010ae A |
459 | !(ifp->if_flags & IFF_POINTOPOINT)) { |
460 | IFA_UNLOCK(ia); | |
b0d623f7 | 461 | continue; |
6d2010ae | 462 | } |
b0d623f7 A |
463 | if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 && |
464 | IN6_IS_ADDR_LINKLOCAL( | |
6d2010ae A |
465 | &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr)) { |
466 | IFA_UNLOCK(ia); | |
b0d623f7 | 467 | continue; |
6d2010ae | 468 | } |
b0d623f7 | 469 | if (flags & PFI_AFLAG_NOALIAS) { |
6d2010ae A |
470 | if (af == AF_INET && got4) { |
471 | IFA_UNLOCK(ia); | |
b0d623f7 | 472 | continue; |
6d2010ae A |
473 | } |
474 | if (af == AF_INET6 && got6) { | |
475 | IFA_UNLOCK(ia); | |
b0d623f7 | 476 | continue; |
6d2010ae | 477 | } |
b0d623f7 A |
478 | } |
479 | if (af == AF_INET) | |
480 | got4 = 1; | |
481 | else if (af == AF_INET6) | |
482 | got6 = 1; | |
483 | net2 = net; | |
484 | if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) { | |
485 | if (af == AF_INET) | |
486 | net2 = pfi_unmask(&((struct sockaddr_in *) | |
487 | ia->ifa_netmask)->sin_addr); | |
488 | else if (af == AF_INET6) | |
489 | net2 = pfi_unmask(&((struct sockaddr_in6 *) | |
490 | ia->ifa_netmask)->sin6_addr); | |
491 | } | |
492 | if (af == AF_INET && net2 > 32) | |
493 | net2 = 32; | |
494 | if (flags & PFI_AFLAG_BROADCAST) | |
495 | pfi_address_add(ia->ifa_broadaddr, af, net2); | |
496 | else if (flags & PFI_AFLAG_PEER) | |
497 | pfi_address_add(ia->ifa_dstaddr, af, net2); | |
498 | else | |
499 | pfi_address_add(ia->ifa_addr, af, net2); | |
6d2010ae | 500 | IFA_UNLOCK(ia); |
b0d623f7 A |
501 | } |
502 | ifnet_lock_done(ifp); | |
503 | } | |
504 | ||
505 | void | |
506 | pfi_address_add(struct sockaddr *sa, int af, int net) | |
507 | { | |
508 | struct pfr_addr *p; | |
509 | int i; | |
510 | ||
511 | if (pfi_buffer_cnt >= pfi_buffer_max) { | |
512 | int new_max = pfi_buffer_max * 2; | |
513 | ||
514 | if (new_max > PFI_BUFFER_MAX) { | |
515 | printf("pfi_address_add: address buffer full (%d/%d)\n", | |
516 | pfi_buffer_cnt, PFI_BUFFER_MAX); | |
517 | return; | |
518 | } | |
519 | p = _MALLOC(new_max * sizeof (*pfi_buffer), PFI_MTYPE, | |
520 | M_WAITOK); | |
521 | if (p == NULL) { | |
522 | printf("pfi_address_add: no memory to grow buffer " | |
523 | "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX); | |
524 | return; | |
525 | } | |
526 | memcpy(pfi_buffer, p, pfi_buffer_cnt * sizeof (*pfi_buffer)); | |
527 | /* no need to zero buffer */ | |
528 | _FREE(pfi_buffer, PFI_MTYPE); | |
529 | pfi_buffer = p; | |
530 | pfi_buffer_max = new_max; | |
531 | } | |
532 | if (af == AF_INET && net > 32) | |
533 | net = 128; | |
534 | p = pfi_buffer + pfi_buffer_cnt++; | |
535 | bzero(p, sizeof (*p)); | |
536 | p->pfra_af = af; | |
537 | p->pfra_net = net; | |
538 | if (af == AF_INET) | |
539 | p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr; | |
540 | else if (af == AF_INET6) { | |
541 | p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr; | |
542 | if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr)) | |
543 | p->pfra_ip6addr.s6_addr16[1] = 0; | |
544 | } | |
545 | /* mask network address bits */ | |
546 | if (net < 128) | |
547 | ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8)); | |
548 | for (i = (p->pfra_net+7)/8; i < (int)sizeof (p->pfra_u); i++) | |
549 | ((caddr_t)p)[i] = 0; | |
550 | } | |
551 | ||
552 | void | |
553 | pfi_dynaddr_remove(struct pf_addr_wrap *aw) | |
554 | { | |
555 | if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL || | |
556 | aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL) | |
557 | return; | |
558 | ||
559 | TAILQ_REMOVE(&aw->p.dyn->pfid_kif->pfik_dynaddrs, aw->p.dyn, entry); | |
560 | pfi_kif_unref(aw->p.dyn->pfid_kif, PFI_KIF_REF_RULE); | |
561 | aw->p.dyn->pfid_kif = NULL; | |
562 | pfr_detach_table(aw->p.dyn->pfid_kt); | |
563 | aw->p.dyn->pfid_kt = NULL; | |
564 | pool_put(&pfi_addr_pl, aw->p.dyn); | |
565 | aw->p.dyn = NULL; | |
566 | } | |
567 | ||
568 | void | |
569 | pfi_dynaddr_copyout(struct pf_addr_wrap *aw) | |
570 | { | |
571 | if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL || | |
572 | aw->p.dyn->pfid_kif == NULL) | |
573 | return; | |
574 | aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6; | |
575 | } | |
576 | ||
577 | void | |
578 | pfi_kifaddr_update(void *v) | |
579 | { | |
580 | struct pfi_kif *kif = (struct pfi_kif *)v; | |
581 | ||
582 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
583 | ||
584 | pfi_update++; | |
585 | pfi_kif_update(kif); | |
586 | } | |
587 | ||
588 | int | |
589 | pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q) | |
590 | { | |
591 | return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ)); | |
592 | } | |
593 | ||
594 | void | |
595 | pfi_update_status(const char *name, struct pf_status *pfs) | |
596 | { | |
597 | struct pfi_kif *p; | |
598 | struct pfi_kif_cmp key; | |
599 | int i, j, k; | |
600 | ||
601 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
602 | ||
603 | strlcpy(key.pfik_name, name, sizeof (key.pfik_name)); | |
604 | p = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&key); | |
605 | if (p == NULL) | |
606 | return; | |
607 | ||
608 | if (pfs) { | |
609 | bzero(pfs->pcounters, sizeof (pfs->pcounters)); | |
610 | bzero(pfs->bcounters, sizeof (pfs->bcounters)); | |
611 | } | |
612 | /* just clear statistics */ | |
613 | if (pfs == NULL) { | |
614 | bzero(p->pfik_packets, sizeof (p->pfik_packets)); | |
615 | bzero(p->pfik_bytes, sizeof (p->pfik_bytes)); | |
d1ecb069 | 616 | p->pfik_tzero = pf_calendar_time_second(); |
b0d623f7 A |
617 | } |
618 | for (i = 0; i < 2; i++) | |
619 | for (j = 0; j < 2; j++) | |
620 | for (k = 0; k < 2; k++) { | |
621 | pfs->pcounters[i][j][k] += | |
622 | p->pfik_packets[i][j][k]; | |
623 | pfs->bcounters[i][j] += | |
624 | p->pfik_bytes[i][j][k]; | |
625 | } | |
626 | } | |
627 | ||
628 | int | |
d1ecb069 | 629 | pfi_get_ifaces(const char *name, user_addr_t buf, int *size) |
b0d623f7 | 630 | { |
d1ecb069 | 631 | struct pfi_kif *p, *nextp; |
b0d623f7 A |
632 | int n = 0; |
633 | ||
634 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
635 | ||
636 | for (p = RB_MIN(pfi_ifhead, &pfi_ifs); p; p = nextp) { | |
637 | nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p); | |
638 | if (pfi_skip_if(name, p)) | |
639 | continue; | |
640 | if (*size > n++) { | |
d1ecb069 A |
641 | struct pfi_uif u; |
642 | ||
b0d623f7 | 643 | if (!p->pfik_tzero) |
d1ecb069 | 644 | p->pfik_tzero = pf_calendar_time_second(); |
b0d623f7 | 645 | pfi_kif_ref(p, PFI_KIF_REF_RULE); |
d1ecb069 A |
646 | |
647 | /* return the user space version of pfi_kif */ | |
648 | bzero(&u, sizeof (u)); | |
649 | bcopy(p->pfik_name, &u.pfik_name, sizeof (u.pfik_name)); | |
650 | bcopy(p->pfik_packets, &u.pfik_packets, | |
651 | sizeof (u.pfik_packets)); | |
652 | bcopy(p->pfik_bytes, &u.pfik_bytes, | |
653 | sizeof (u.pfik_bytes)); | |
654 | u.pfik_tzero = p->pfik_tzero; | |
655 | u.pfik_flags = p->pfik_flags; | |
656 | u.pfik_states = p->pfik_states; | |
657 | u.pfik_rules = p->pfik_rules; | |
658 | ||
659 | if (copyout(&u, buf, sizeof (u))) { | |
b0d623f7 A |
660 | pfi_kif_unref(p, PFI_KIF_REF_RULE); |
661 | return (EFAULT); | |
662 | } | |
d1ecb069 | 663 | buf += sizeof (u); |
b0d623f7 A |
664 | nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p); |
665 | pfi_kif_unref(p, PFI_KIF_REF_RULE); | |
666 | } | |
667 | } | |
668 | *size = n; | |
669 | return (0); | |
670 | } | |
671 | ||
672 | int | |
673 | pfi_skip_if(const char *filter, struct pfi_kif *p) | |
674 | { | |
675 | int n; | |
676 | ||
677 | if (filter == NULL || !*filter) | |
678 | return (0); | |
679 | if (strcmp(p->pfik_name, filter) == 0) | |
680 | return (0); /* exact match */ | |
681 | n = strlen(filter); | |
682 | if (n < 1 || n >= IFNAMSIZ) | |
683 | return (1); /* sanity check */ | |
684 | if (filter[n-1] >= '0' && filter[n-1] <= '9') | |
685 | return (1); /* only do exact match in that case */ | |
686 | if (strncmp(p->pfik_name, filter, n)) | |
687 | return (1); /* prefix doesn't match */ | |
688 | return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9'); | |
689 | } | |
690 | ||
691 | int | |
692 | pfi_set_flags(const char *name, int flags) | |
693 | { | |
694 | struct pfi_kif *p; | |
695 | ||
696 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
697 | ||
698 | RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { | |
699 | if (pfi_skip_if(name, p)) | |
700 | continue; | |
701 | p->pfik_flags |= flags; | |
702 | } | |
703 | return (0); | |
704 | } | |
705 | ||
706 | int | |
707 | pfi_clear_flags(const char *name, int flags) | |
708 | { | |
709 | struct pfi_kif *p; | |
710 | ||
711 | lck_mtx_assert(pf_lock, LCK_MTX_ASSERT_OWNED); | |
712 | ||
713 | RB_FOREACH(p, pfi_ifhead, &pfi_ifs) { | |
714 | if (pfi_skip_if(name, p)) | |
715 | continue; | |
716 | p->pfik_flags &= ~flags; | |
717 | } | |
718 | return (0); | |
719 | } | |
720 | ||
721 | /* from pf_print_state.c */ | |
722 | int | |
723 | pfi_unmask(void *addr) | |
724 | { | |
725 | struct pf_addr *m = addr; | |
726 | int i = 31, j = 0, b = 0; | |
727 | u_int32_t tmp; | |
728 | ||
729 | while (j < 4 && m->addr32[j] == 0xffffffff) { | |
730 | b += 32; | |
731 | j++; | |
732 | } | |
733 | if (j < 4) { | |
734 | tmp = ntohl(m->addr32[j]); | |
735 | for (i = 31; tmp & (1 << i); --i) | |
736 | b++; | |
737 | } | |
738 | return (b); | |
739 | } |