]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/pf_if.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / net / pf_if.c
CommitLineData
b0d623f7 1/*
94ff46dc 2 * Copyright (c) 2007-2019 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
0a7de745 90struct pfi_kif *pfi_all = NULL;
b0d623f7 91
0a7de745
A
92static struct pool pfi_addr_pl;
93static struct pfi_ifhead pfi_ifs;
94static u_int32_t pfi_update = 1;
95static struct pfr_addr *pfi_buffer;
96static int pfi_buffer_cnt;
97static int pfi_buffer_max;
b0d623f7
A
98
99__private_extern__ void pfi_kifaddr_update(void *);
100
101static void pfi_kif_update(struct pfi_kif *);
102static void pfi_dynaddr_update(struct pfi_dynaddr *dyn);
103static void pfi_table_update(struct pfr_ktable *, struct pfi_kif *, int, int);
104static void pfi_instance_add(struct ifnet *, int, int);
105static void pfi_address_add(struct sockaddr *, int, int);
106static int pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
107static int pfi_skip_if(const char *, struct pfi_kif *);
108static int pfi_unmask(void *);
109
110RB_PROTOTYPE_SC(static, pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
111RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
112
0a7de745
A
113#define PFI_BUFFER_MAX 0x10000
114#define PFI_MTYPE M_IFADDR
b0d623f7 115
0a7de745 116#define IFG_ALL "ALL"
b0d623f7
A
117
118void
119pfi_initialize(void)
120{
0a7de745 121 if (pfi_all != NULL) { /* already initialized */
b0d623f7 122 return;
0a7de745 123 }
b0d623f7 124
0a7de745 125 pool_init(&pfi_addr_pl, sizeof(struct pfi_dynaddr), 0, 0, 0,
b0d623f7
A
126 "pfiaddrpl", NULL);
127 pfi_buffer_max = 64;
0a7de745 128 pfi_buffer = _MALLOC(pfi_buffer_max * sizeof(*pfi_buffer),
b0d623f7
A
129 PFI_MTYPE, M_WAITOK);
130
0a7de745 131 if ((pfi_all = pfi_kif_get(IFG_ALL)) == NULL) {
b0d623f7 132 panic("pfi_kif_get for pfi_all failed");
0a7de745 133 }
b0d623f7
A
134}
135
136#if 0
137void
138pfi_destroy(void)
139{
140 pool_destroy(&pfi_addr_pl);
141 _FREE(pfi_buffer, PFI_MTYPE);
142}
143#endif
144
145struct pfi_kif *
146pfi_kif_get(const char *kif_name)
147{
0a7de745
A
148 struct pfi_kif *kif;
149 struct pfi_kif_cmp s;
b0d623f7 150
0a7de745
A
151 bzero(&s, sizeof(s));
152 strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
316670eb 153 if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs,
0a7de745
A
154 (struct pfi_kif *)(void *)&s)) != NULL) {
155 return kif;
156 }
b0d623f7
A
157
158 /* create new one */
0a7de745
A
159 if ((kif = _MALLOC(sizeof(*kif), PFI_MTYPE, M_WAITOK | M_ZERO)) == NULL) {
160 return NULL;
161 }
b0d623f7 162
0a7de745 163 strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
d1ecb069 164 kif->pfik_tzero = pf_calendar_time_second();
b0d623f7
A
165 TAILQ_INIT(&kif->pfik_dynaddrs);
166
167 RB_INSERT(pfi_ifhead, &pfi_ifs, kif);
0a7de745 168 return kif;
b0d623f7
A
169}
170
171void
172pfi_kif_ref(struct pfi_kif *kif, enum pfi_kif_refs what)
173{
174 switch (what) {
175 case PFI_KIF_REF_RULE:
176 kif->pfik_rules++;
177 break;
178 case PFI_KIF_REF_STATE:
179 kif->pfik_states++;
180 break;
181 default:
182 panic("pfi_kif_ref with unknown type");
183 }
184}
185
186void
187pfi_kif_unref(struct pfi_kif *kif, enum pfi_kif_refs what)
188{
0a7de745 189 if (kif == NULL) {
b0d623f7 190 return;
0a7de745 191 }
b0d623f7
A
192
193 switch (what) {
194 case PFI_KIF_REF_NONE:
195 break;
196 case PFI_KIF_REF_RULE:
197 if (kif->pfik_rules <= 0) {
198 printf("pfi_kif_unref: rules refcount <= 0\n");
199 return;
200 }
201 kif->pfik_rules--;
202 break;
203 case PFI_KIF_REF_STATE:
204 if (kif->pfik_states <= 0) {
205 printf("pfi_kif_unref: state refcount <= 0\n");
206 return;
207 }
208 kif->pfik_states--;
209 break;
210 default:
211 panic("pfi_kif_unref with unknown type");
212 }
213
0a7de745 214 if (kif->pfik_ifp != NULL || kif == pfi_all) {
b0d623f7 215 return;
0a7de745 216 }
b0d623f7 217
0a7de745 218 if (kif->pfik_rules || kif->pfik_states) {
b0d623f7 219 return;
0a7de745 220 }
b0d623f7
A
221
222 RB_REMOVE(pfi_ifhead, &pfi_ifs, kif);
223 _FREE(kif, PFI_MTYPE);
224}
225
226int
227pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif)
228{
0a7de745
A
229 if (rule_kif == NULL || rule_kif == packet_kif) {
230 return 1;
231 }
b0d623f7 232
0a7de745 233 return 0;
b0d623f7
A
234}
235
236void
237pfi_attach_ifnet(struct ifnet *ifp)
238{
239 struct pfi_kif *kif;
b0d623f7 240
5ba3f43e 241 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
242
243 pfi_update++;
0a7de745 244 if ((kif = pfi_kif_get(if_name(ifp))) == NULL) {
b0d623f7 245 panic("pfi_kif_get failed");
0a7de745 246 }
b0d623f7
A
247
248 ifnet_lock_exclusive(ifp);
249 kif->pfik_ifp = ifp;
250 ifp->if_pf_kif = kif;
251 ifnet_lock_done(ifp);
252
253 pfi_kif_update(kif);
254}
255
256/*
257 * Caller holds ifnet lock as writer (exclusive);
258 */
259void
260pfi_detach_ifnet(struct ifnet *ifp)
261{
0a7de745 262 struct pfi_kif *kif;
b0d623f7 263
5ba3f43e 264 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 265
0a7de745 266 if ((kif = (struct pfi_kif *)ifp->if_pf_kif) == NULL) {
b0d623f7 267 return;
0a7de745 268 }
b0d623f7
A
269
270 pfi_update++;
271 pfi_kif_update(kif);
272
273 ifnet_lock_exclusive(ifp);
274 kif->pfik_ifp = NULL;
275 ifp->if_pf_kif = NULL;
276 ifnet_lock_done(ifp);
277
278 pfi_kif_unref(kif, PFI_KIF_REF_NONE);
279}
280
281int
282pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
283{
284 switch (af) {
285#if INET
286 case AF_INET:
287 switch (dyn->pfid_acnt4) {
288 case 0:
0a7de745 289 return 0;
b0d623f7 290 case 1:
0a7de745
A
291 return PF_MATCHA(0, &dyn->pfid_addr4,
292 &dyn->pfid_mask4, a, AF_INET);
b0d623f7 293 default:
0a7de745 294 return pfr_match_addr(dyn->pfid_kt, a, AF_INET);
b0d623f7 295 }
b0d623f7
A
296#endif /* INET */
297#if INET6
298 case AF_INET6:
299 switch (dyn->pfid_acnt6) {
300 case 0:
0a7de745 301 return 0;
b0d623f7 302 case 1:
0a7de745
A
303 return PF_MATCHA(0, &dyn->pfid_addr6,
304 &dyn->pfid_mask6, a, AF_INET6);
b0d623f7 305 default:
0a7de745 306 return pfr_match_addr(dyn->pfid_kt, a, AF_INET6);
b0d623f7 307 }
b0d623f7
A
308#endif /* INET6 */
309 default:
0a7de745 310 return 0;
b0d623f7
A
311 }
312}
313
314int
315pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
316{
0a7de745
A
317 struct pfi_dynaddr *dyn;
318 char tblname[PF_TABLE_NAME_SIZE];
319 struct pf_ruleset *ruleset = NULL;
320 int rv = 0;
b0d623f7 321
5ba3f43e 322 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 323
0a7de745
A
324 if (aw->type != PF_ADDR_DYNIFTL) {
325 return 0;
326 }
327 if ((dyn = pool_get(&pfi_addr_pl, PR_WAITOK)) == NULL) {
328 return 1;
329 }
330 bzero(dyn, sizeof(*dyn));
b0d623f7 331
0a7de745 332 if (strcmp(aw->v.ifname, "self") == 0) {
b0d623f7 333 dyn->pfid_kif = pfi_kif_get(IFG_ALL);
0a7de745 334 } else {
b0d623f7 335 dyn->pfid_kif = pfi_kif_get(aw->v.ifname);
0a7de745 336 }
b0d623f7
A
337 if (dyn->pfid_kif == NULL) {
338 rv = 1;
339 goto _bad;
340 }
341 pfi_kif_ref(dyn->pfid_kif, PFI_KIF_REF_RULE);
342
343 dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
0a7de745 344 if (af == AF_INET && dyn->pfid_net == 32) {
b0d623f7 345 dyn->pfid_net = 128;
0a7de745
A
346 }
347 strlcpy(tblname, aw->v.ifname, sizeof(tblname));
348 if (aw->iflags & PFI_AFLAG_NETWORK) {
349 strlcat(tblname, ":network", sizeof(tblname));
350 }
351 if (aw->iflags & PFI_AFLAG_BROADCAST) {
352 strlcat(tblname, ":broadcast", sizeof(tblname));
353 }
354 if (aw->iflags & PFI_AFLAG_PEER) {
355 strlcat(tblname, ":peer", sizeof(tblname));
356 }
357 if (aw->iflags & PFI_AFLAG_NOALIAS) {
358 strlcat(tblname, ":0", sizeof(tblname));
359 }
360 if (dyn->pfid_net != 128) {
b0d623f7 361 snprintf(tblname + strlen(tblname),
0a7de745
A
362 sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
363 }
b0d623f7
A
364 if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
365 rv = 1;
366 goto _bad;
367 }
368
369 if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) {
370 rv = 1;
371 goto _bad;
372 }
373
374 dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
375 dyn->pfid_iflags = aw->iflags;
376 dyn->pfid_af = af;
377
378 TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
379 aw->p.dyn = dyn;
380 pfi_kif_update(dyn->pfid_kif);
0a7de745 381 return 0;
b0d623f7
A
382
383_bad:
0a7de745 384 if (dyn->pfid_kt != NULL) {
b0d623f7 385 pfr_detach_table(dyn->pfid_kt);
0a7de745
A
386 }
387 if (ruleset != NULL) {
b0d623f7 388 pf_remove_if_empty_ruleset(ruleset);
0a7de745
A
389 }
390 if (dyn->pfid_kif != NULL) {
b0d623f7 391 pfi_kif_unref(dyn->pfid_kif, PFI_KIF_REF_RULE);
0a7de745 392 }
b0d623f7 393 pool_put(&pfi_addr_pl, dyn);
0a7de745 394 return rv;
b0d623f7
A
395}
396
397void
398pfi_kif_update(struct pfi_kif *kif)
399{
0a7de745 400 struct pfi_dynaddr *p;
b0d623f7 401
5ba3f43e 402 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
403
404 /* update all dynaddr */
405 TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
0a7de745 406 pfi_dynaddr_update(p);
b0d623f7
A
407}
408
409void
410pfi_dynaddr_update(struct pfi_dynaddr *dyn)
411{
0a7de745
A
412 struct pfi_kif *kif;
413 struct pfr_ktable *kt;
b0d623f7 414
0a7de745 415 if (dyn == NULL || dyn->pfid_kif == NULL || dyn->pfid_kt == NULL) {
b0d623f7 416 panic("pfi_dynaddr_update");
0a7de745 417 }
b0d623f7
A
418
419 kif = dyn->pfid_kif;
420 kt = dyn->pfid_kt;
421
422 if (kt->pfrkt_larg != pfi_update) {
423 /* this table needs to be brought up-to-date */
424 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
425 kt->pfrkt_larg = pfi_update;
426 }
427 pfr_dynaddr_update(kt, dyn);
428}
429
430void
431pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags)
432{
0a7de745 433 int e, size2 = 0;
b0d623f7
A
434
435 pfi_buffer_cnt = 0;
436
0a7de745 437 if (kif->pfik_ifp != NULL) {
b0d623f7 438 pfi_instance_add(kif->pfik_ifp, net, flags);
0a7de745 439 }
b0d623f7 440
d1ecb069 441 if ((e = pfr_set_addrs(&kt->pfrkt_t, CAST_USER_ADDR_T(pfi_buffer),
0a7de745 442 pfi_buffer_cnt, &size2, NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK))) {
b0d623f7
A
443 printf("pfi_table_update: cannot set %d new addresses "
444 "into table %s: %d\n", pfi_buffer_cnt, kt->pfrkt_name, e);
0a7de745 445 }
b0d623f7
A
446}
447
448void
449pfi_instance_add(struct ifnet *ifp, int net, int flags)
450{
0a7de745
A
451 struct ifaddr *ia;
452 int got4 = 0, got6 = 0;
453 int net2, af;
b0d623f7 454
0a7de745 455 if (ifp == NULL) {
b0d623f7 456 return;
0a7de745 457 }
b0d623f7
A
458 ifnet_lock_shared(ifp);
459 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
6d2010ae
A
460 IFA_LOCK(ia);
461 if (ia->ifa_addr == NULL) {
462 IFA_UNLOCK(ia);
b0d623f7 463 continue;
6d2010ae 464 }
b0d623f7 465 af = ia->ifa_addr->sa_family;
6d2010ae
A
466 if (af != AF_INET && af != AF_INET6) {
467 IFA_UNLOCK(ia);
b0d623f7 468 continue;
6d2010ae
A
469 }
470 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6) {
471 IFA_UNLOCK(ia);
b0d623f7 472 continue;
6d2010ae 473 }
b0d623f7 474 if ((flags & PFI_AFLAG_BROADCAST) &&
6d2010ae
A
475 !(ifp->if_flags & IFF_BROADCAST)) {
476 IFA_UNLOCK(ia);
b0d623f7 477 continue;
6d2010ae 478 }
b0d623f7 479 if ((flags & PFI_AFLAG_PEER) &&
6d2010ae
A
480 !(ifp->if_flags & IFF_POINTOPOINT)) {
481 IFA_UNLOCK(ia);
b0d623f7 482 continue;
6d2010ae 483 }
cb323159 484 if ((af == AF_INET6) &&
316670eb
A
485 IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6 *)
486 (void *)ia->ifa_addr)->sin6_addr)) {
6d2010ae 487 IFA_UNLOCK(ia);
b0d623f7 488 continue;
6d2010ae 489 }
cb323159
A
490 if ((af == AF_INET6) &&
491 (((struct in6_ifaddr *)ia)->ia6_flags &
492 (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_DETACHED |
493 IN6_IFF_CLAT46 | IN6_IFF_TEMPORARY | IN6_IFF_DEPRECATED))) {
494 IFA_UNLOCK(ia);
495 continue;
496 }
b0d623f7 497 if (flags & PFI_AFLAG_NOALIAS) {
6d2010ae
A
498 if (af == AF_INET && got4) {
499 IFA_UNLOCK(ia);
b0d623f7 500 continue;
6d2010ae
A
501 }
502 if (af == AF_INET6 && got6) {
503 IFA_UNLOCK(ia);
b0d623f7 504 continue;
6d2010ae 505 }
b0d623f7 506 }
0a7de745 507 if (af == AF_INET) {
b0d623f7 508 got4 = 1;
0a7de745 509 } else if (af == AF_INET6) {
b0d623f7 510 got6 = 1;
0a7de745 511 }
b0d623f7
A
512 net2 = net;
513 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
0a7de745 514 if (af == AF_INET) {
b0d623f7 515 net2 = pfi_unmask(&((struct sockaddr_in *)
316670eb 516 (void *)ia->ifa_netmask)->sin_addr);
0a7de745 517 } else if (af == AF_INET6) {
b0d623f7 518 net2 = pfi_unmask(&((struct sockaddr_in6 *)
316670eb 519 (void *)ia->ifa_netmask)->sin6_addr);
0a7de745 520 }
b0d623f7 521 }
0a7de745 522 if (af == AF_INET && net2 > 32) {
b0d623f7 523 net2 = 32;
0a7de745
A
524 }
525 if (flags & PFI_AFLAG_BROADCAST) {
b0d623f7 526 pfi_address_add(ia->ifa_broadaddr, af, net2);
0a7de745 527 } else if (flags & PFI_AFLAG_PEER) {
b0d623f7 528 pfi_address_add(ia->ifa_dstaddr, af, net2);
0a7de745 529 } else {
b0d623f7 530 pfi_address_add(ia->ifa_addr, af, net2);
0a7de745 531 }
6d2010ae 532 IFA_UNLOCK(ia);
b0d623f7
A
533 }
534 ifnet_lock_done(ifp);
535}
536
537void
538pfi_address_add(struct sockaddr *sa, int af, int net)
539{
0a7de745
A
540 struct pfr_addr *p;
541 int i;
b0d623f7
A
542
543 if (pfi_buffer_cnt >= pfi_buffer_max) {
0a7de745 544 int new_max = pfi_buffer_max * 2;
b0d623f7
A
545
546 if (new_max > PFI_BUFFER_MAX) {
547 printf("pfi_address_add: address buffer full (%d/%d)\n",
548 pfi_buffer_cnt, PFI_BUFFER_MAX);
549 return;
550 }
0a7de745 551 p = _MALLOC(new_max * sizeof(*pfi_buffer), PFI_MTYPE,
b0d623f7
A
552 M_WAITOK);
553 if (p == NULL) {
554 printf("pfi_address_add: no memory to grow buffer "
555 "(%d/%d)\n", pfi_buffer_cnt, PFI_BUFFER_MAX);
556 return;
557 }
94ff46dc 558 memcpy(p, pfi_buffer, pfi_buffer_max * sizeof(*pfi_buffer));
b0d623f7
A
559 /* no need to zero buffer */
560 _FREE(pfi_buffer, PFI_MTYPE);
561 pfi_buffer = p;
562 pfi_buffer_max = new_max;
563 }
0a7de745 564 if (af == AF_INET && net > 32) {
b0d623f7 565 net = 128;
0a7de745 566 }
b0d623f7 567 p = pfi_buffer + pfi_buffer_cnt++;
0a7de745 568 bzero(p, sizeof(*p));
b0d623f7
A
569 p->pfra_af = af;
570 p->pfra_net = net;
0a7de745 571 if (af == AF_INET) {
316670eb 572 p->pfra_ip4addr = ((struct sockaddr_in *)(void *)sa)->sin_addr;
0a7de745 573 } else if (af == AF_INET6) {
316670eb
A
574 p->pfra_ip6addr =
575 ((struct sockaddr_in6 *)(void *)sa)->sin6_addr;
0a7de745 576 if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr)) {
b0d623f7 577 p->pfra_ip6addr.s6_addr16[1] = 0;
0a7de745 578 }
b0d623f7
A
579 }
580 /* mask network address bits */
0a7de745
A
581 if (net < 128) {
582 ((caddr_t)p)[p->pfra_net / 8] &= ~(0xFF >> (p->pfra_net % 8));
583 }
584 for (i = (p->pfra_net + 7) / 8; i < (int)sizeof(p->pfra_u); i++) {
b0d623f7 585 ((caddr_t)p)[i] = 0;
0a7de745 586 }
b0d623f7
A
587}
588
589void
590pfi_dynaddr_remove(struct pf_addr_wrap *aw)
591{
592 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
0a7de745 593 aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL) {
b0d623f7 594 return;
0a7de745 595 }
b0d623f7
A
596
597 TAILQ_REMOVE(&aw->p.dyn->pfid_kif->pfik_dynaddrs, aw->p.dyn, entry);
598 pfi_kif_unref(aw->p.dyn->pfid_kif, PFI_KIF_REF_RULE);
599 aw->p.dyn->pfid_kif = NULL;
600 pfr_detach_table(aw->p.dyn->pfid_kt);
601 aw->p.dyn->pfid_kt = NULL;
602 pool_put(&pfi_addr_pl, aw->p.dyn);
603 aw->p.dyn = NULL;
604}
605
606void
607pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
608{
609 if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
0a7de745 610 aw->p.dyn->pfid_kif == NULL) {
b0d623f7 611 return;
0a7de745 612 }
b0d623f7
A
613 aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
614}
615
616void
617pfi_kifaddr_update(void *v)
618{
0a7de745 619 struct pfi_kif *kif = (struct pfi_kif *)v;
b0d623f7 620
5ba3f43e 621 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
622
623 pfi_update++;
624 pfi_kif_update(kif);
625}
626
627int
628pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
629{
0a7de745 630 return strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ);
b0d623f7
A
631}
632
633void
634pfi_update_status(const char *name, struct pf_status *pfs)
635{
0a7de745
A
636 struct pfi_kif *p;
637 struct pfi_kif_cmp key;
638 int i, j, k;
b0d623f7 639
5ba3f43e 640 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7 641
0a7de745 642 strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
316670eb 643 p = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)(void *)&key);
0a7de745 644 if (p == NULL) {
b0d623f7 645 return;
0a7de745 646 }
b0d623f7 647
4bd07ac2 648 if (pfs != NULL) {
0a7de745
A
649 bzero(pfs->pcounters, sizeof(pfs->pcounters));
650 bzero(pfs->bcounters, sizeof(pfs->bcounters));
651 for (i = 0; i < 2; i++) {
652 for (j = 0; j < 2; j++) {
4bd07ac2
A
653 for (k = 0; k < 2; k++) {
654 pfs->pcounters[i][j][k] +=
0a7de745 655 p->pfik_packets[i][j][k];
4bd07ac2 656 pfs->bcounters[i][j] +=
0a7de745 657 p->pfik_bytes[i][j][k];
4bd07ac2 658 }
0a7de745
A
659 }
660 }
4bd07ac2
A
661 } else {
662 /* just clear statistics */
0a7de745
A
663 bzero(p->pfik_packets, sizeof(p->pfik_packets));
664 bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
d1ecb069 665 p->pfik_tzero = pf_calendar_time_second();
b0d623f7 666 }
b0d623f7
A
667}
668
669int
d1ecb069 670pfi_get_ifaces(const char *name, user_addr_t buf, int *size)
b0d623f7 671{
0a7de745
A
672 struct pfi_kif *p, *nextp;
673 int n = 0;
b0d623f7 674
5ba3f43e 675 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
676
677 for (p = RB_MIN(pfi_ifhead, &pfi_ifs); p; p = nextp) {
678 nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
0a7de745 679 if (pfi_skip_if(name, p)) {
b0d623f7 680 continue;
0a7de745 681 }
b0d623f7 682 if (*size > n++) {
d1ecb069
A
683 struct pfi_uif u;
684
0a7de745 685 if (!p->pfik_tzero) {
d1ecb069 686 p->pfik_tzero = pf_calendar_time_second();
0a7de745 687 }
b0d623f7 688 pfi_kif_ref(p, PFI_KIF_REF_RULE);
d1ecb069
A
689
690 /* return the user space version of pfi_kif */
0a7de745
A
691 bzero(&u, sizeof(u));
692 bcopy(p->pfik_name, &u.pfik_name, sizeof(u.pfik_name));
d1ecb069 693 bcopy(p->pfik_packets, &u.pfik_packets,
0a7de745 694 sizeof(u.pfik_packets));
d1ecb069 695 bcopy(p->pfik_bytes, &u.pfik_bytes,
0a7de745 696 sizeof(u.pfik_bytes));
d1ecb069
A
697 u.pfik_tzero = p->pfik_tzero;
698 u.pfik_flags = p->pfik_flags;
699 u.pfik_states = p->pfik_states;
700 u.pfik_rules = p->pfik_rules;
701
0a7de745 702 if (copyout(&u, buf, sizeof(u))) {
b0d623f7 703 pfi_kif_unref(p, PFI_KIF_REF_RULE);
0a7de745 704 return EFAULT;
b0d623f7 705 }
0a7de745 706 buf += sizeof(u);
b0d623f7
A
707 nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
708 pfi_kif_unref(p, PFI_KIF_REF_RULE);
709 }
710 }
711 *size = n;
0a7de745 712 return 0;
b0d623f7
A
713}
714
715int
716pfi_skip_if(const char *filter, struct pfi_kif *p)
717{
0a7de745 718 int n;
b0d623f7 719
0a7de745
A
720 if (filter == NULL || !*filter) {
721 return 0;
722 }
723 if (strcmp(p->pfik_name, filter) == 0) {
724 return 0; /* exact match */
725 }
b0d623f7 726 n = strlen(filter);
0a7de745
A
727 if (n < 1 || n >= IFNAMSIZ) {
728 return 1; /* sanity check */
729 }
730 if (filter[n - 1] >= '0' && filter[n - 1] <= '9') {
731 return 1; /* only do exact match in that case */
732 }
733 if (strncmp(p->pfik_name, filter, n)) {
734 return 1; /* prefix doesn't match */
735 }
736 return p->pfik_name[n] < '0' || p->pfik_name[n] > '9';
b0d623f7
A
737}
738
739int
740pfi_set_flags(const char *name, int flags)
741{
0a7de745 742 struct pfi_kif *p;
b0d623f7 743
5ba3f43e 744 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
745
746 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
0a7de745 747 if (pfi_skip_if(name, p)) {
b0d623f7 748 continue;
0a7de745 749 }
b0d623f7
A
750 p->pfik_flags |= flags;
751 }
0a7de745 752 return 0;
b0d623f7
A
753}
754
755int
756pfi_clear_flags(const char *name, int flags)
757{
0a7de745 758 struct pfi_kif *p;
b0d623f7 759
5ba3f43e 760 LCK_MTX_ASSERT(pf_lock, LCK_MTX_ASSERT_OWNED);
b0d623f7
A
761
762 RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
0a7de745 763 if (pfi_skip_if(name, p)) {
b0d623f7 764 continue;
0a7de745 765 }
b0d623f7
A
766 p->pfik_flags &= ~flags;
767 }
0a7de745 768 return 0;
b0d623f7
A
769}
770
771/* from pf_print_state.c */
772int
773pfi_unmask(void *addr)
774{
775 struct pf_addr *m = addr;
776 int i = 31, j = 0, b = 0;
777 u_int32_t tmp;
778
779 while (j < 4 && m->addr32[j] == 0xffffffff) {
780 b += 32;
781 j++;
782 }
783 if (j < 4) {
784 tmp = ntohl(m->addr32[j]);
0a7de745 785 for (i = 31; tmp & (1 << i); --i) {
b0d623f7 786 b++;
0a7de745 787 }
b0d623f7 788 }
0a7de745 789 return b;
b0d623f7 790}