1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2001 Daniel Hartmeier 5 * Copyright (c) 2003 Cedric Berger 6 * Copyright (c) 2005 Henning Brauer <henning@openbsd.org> 7 * Copyright (c) 2005 Ryan McBride <mcbride@openbsd.org> 8 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 15 * - Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * - Redistributions in binary form must reproduce the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer in the documentation and/or other materials provided 20 * with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * $OpenBSD: pf_if.c,v 1.54 2008/06/14 16:55:28 mk Exp $ 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include "opt_inet.h" 42 #include "opt_inet6.h" 43 44 #include <sys/param.h> 45 #include <sys/kernel.h> 46 #include <sys/eventhandler.h> 47 #include <sys/lock.h> 48 #include <sys/mbuf.h> 49 #include <sys/rwlock.h> 50 #include <sys/socket.h> 51 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <net/vnet.h> 55 #include <net/pfvar.h> 56 #include <net/route.h> 57 58 VNET_DEFINE(struct pfi_kif *, pfi_all); 59 static VNET_DEFINE(long, pfi_update); 60 #define V_pfi_update VNET(pfi_update) 61 #define PFI_BUFFER_MAX 0x10000 62 63 VNET_DECLARE(int, pf_vnet_active); 64 #define V_pf_vnet_active VNET(pf_vnet_active) 65 66 static VNET_DEFINE(struct pfr_addr *, pfi_buffer); 67 static VNET_DEFINE(int, pfi_buffer_cnt); 68 static VNET_DEFINE(int, pfi_buffer_max); 69 #define V_pfi_buffer VNET(pfi_buffer) 70 #define V_pfi_buffer_cnt VNET(pfi_buffer_cnt) 71 #define V_pfi_buffer_max VNET(pfi_buffer_max) 72 73 eventhandler_tag pfi_attach_cookie; 74 eventhandler_tag pfi_detach_cookie; 75 eventhandler_tag pfi_attach_group_cookie; 76 eventhandler_tag pfi_change_group_cookie; 77 eventhandler_tag pfi_detach_group_cookie; 78 eventhandler_tag pfi_ifaddr_event_cookie; 79 80 static void pfi_attach_ifnet(struct ifnet *); 81 static void pfi_attach_ifgroup(struct ifg_group *); 82 83 static void pfi_kif_update(struct pfi_kif *); 84 static void pfi_dynaddr_update(struct pfi_dynaddr *dyn); 85 static void pfi_table_update(struct pfr_ktable *, struct pfi_kif *, int, 86 int); 87 static void pfi_instance_add(struct ifnet *, int, int); 88 static void pfi_address_add(struct sockaddr *, int, int); 89 static int pfi_if_compare(struct pfi_kif *, struct pfi_kif *); 90 static int pfi_skip_if(const char *, struct pfi_kif *); 91 static int pfi_unmask(void *); 92 static void pfi_attach_ifnet_event(void * __unused, struct ifnet *); 93 static void pfi_detach_ifnet_event(void * __unused, struct ifnet *); 94 static void pfi_attach_group_event(void * __unused, struct ifg_group *); 95 static void pfi_change_group_event(void * __unused, char *); 96 static void pfi_detach_group_event(void * __unused, struct ifg_group *); 97 static void pfi_ifaddr_event(void * __unused, struct ifnet *); 98 99 RB_HEAD(pfi_ifhead, pfi_kif); 100 static RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); 101 static RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare); 102 static VNET_DEFINE(struct pfi_ifhead, pfi_ifs); 103 #define V_pfi_ifs VNET(pfi_ifs) 104 105 #define PFI_BUFFER_MAX 0x10000 106 MALLOC_DEFINE(PFI_MTYPE, "pf_ifnet", "pf(4) interface database"); 107 108 LIST_HEAD(pfi_list, pfi_kif); 109 static VNET_DEFINE(struct pfi_list, pfi_unlinked_kifs); 110 #define V_pfi_unlinked_kifs VNET(pfi_unlinked_kifs) 111 static struct mtx pfi_unlnkdkifs_mtx; 112 MTX_SYSINIT(pfi_unlnkdkifs_mtx, &pfi_unlnkdkifs_mtx, "pf unlinked interfaces", 113 MTX_DEF); 114 115 void 116 pfi_initialize_vnet(void) 117 { 118 struct ifg_group *ifg; 119 struct ifnet *ifp; 120 struct pfi_kif *kif; 121 122 V_pfi_buffer_max = 64; 123 V_pfi_buffer = malloc(V_pfi_buffer_max * sizeof(*V_pfi_buffer), 124 PFI_MTYPE, M_WAITOK); 125 126 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK); 127 PF_RULES_WLOCK(); 128 V_pfi_all = pfi_kif_attach(kif, IFG_ALL); 129 PF_RULES_WUNLOCK(); 130 131 IFNET_RLOCK(); 132 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 133 pfi_attach_ifgroup(ifg); 134 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 135 pfi_attach_ifnet(ifp); 136 IFNET_RUNLOCK(); 137 } 138 139 void 140 pfi_initialize(void) 141 { 142 143 pfi_attach_cookie = EVENTHANDLER_REGISTER(ifnet_arrival_event, 144 pfi_attach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); 145 pfi_detach_cookie = EVENTHANDLER_REGISTER(ifnet_departure_event, 146 pfi_detach_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); 147 pfi_attach_group_cookie = EVENTHANDLER_REGISTER(group_attach_event, 148 pfi_attach_group_event, NULL, EVENTHANDLER_PRI_ANY); 149 pfi_change_group_cookie = EVENTHANDLER_REGISTER(group_change_event, 150 pfi_change_group_event, NULL, EVENTHANDLER_PRI_ANY); 151 pfi_detach_group_cookie = EVENTHANDLER_REGISTER(group_detach_event, 152 pfi_detach_group_event, NULL, EVENTHANDLER_PRI_ANY); 153 pfi_ifaddr_event_cookie = EVENTHANDLER_REGISTER(ifaddr_event, 154 pfi_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY); 155 } 156 157 void 158 pfi_cleanup_vnet(void) 159 { 160 struct pfi_kif *kif; 161 162 PF_RULES_WASSERT(); 163 164 V_pfi_all = NULL; 165 while ((kif = RB_MIN(pfi_ifhead, &V_pfi_ifs))) { 166 RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif); 167 if (kif->pfik_group) 168 kif->pfik_group->ifg_pf_kif = NULL; 169 if (kif->pfik_ifp) 170 kif->pfik_ifp->if_pf_kif = NULL; 171 free(kif, PFI_MTYPE); 172 } 173 174 mtx_lock(&pfi_unlnkdkifs_mtx); 175 while ((kif = LIST_FIRST(&V_pfi_unlinked_kifs))) { 176 LIST_REMOVE(kif, pfik_list); 177 free(kif, PFI_MTYPE); 178 } 179 mtx_unlock(&pfi_unlnkdkifs_mtx); 180 181 free(V_pfi_buffer, PFI_MTYPE); 182 } 183 184 void 185 pfi_cleanup(void) 186 { 187 188 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, pfi_attach_cookie); 189 EVENTHANDLER_DEREGISTER(ifnet_departure_event, pfi_detach_cookie); 190 EVENTHANDLER_DEREGISTER(group_attach_event, pfi_attach_group_cookie); 191 EVENTHANDLER_DEREGISTER(group_change_event, pfi_change_group_cookie); 192 EVENTHANDLER_DEREGISTER(group_detach_event, pfi_detach_group_cookie); 193 EVENTHANDLER_DEREGISTER(ifaddr_event, pfi_ifaddr_event_cookie); 194 } 195 196 struct pfi_kif * 197 pfi_kif_find(const char *kif_name) 198 { 199 struct pfi_kif_cmp s; 200 201 PF_RULES_ASSERT(); 202 203 bzero(&s, sizeof(s)); 204 strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name)); 205 206 return (RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&s)); 207 } 208 209 struct pfi_kif * 210 pfi_kif_attach(struct pfi_kif *kif, const char *kif_name) 211 { 212 struct pfi_kif *kif1; 213 214 PF_RULES_WASSERT(); 215 KASSERT(kif != NULL, ("%s: null kif", __func__)); 216 217 kif1 = pfi_kif_find(kif_name); 218 if (kif1 != NULL) { 219 free(kif, PFI_MTYPE); 220 return (kif1); 221 } 222 223 bzero(kif, sizeof(*kif)); 224 strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name)); 225 /* 226 * It seems that the value of time_second is in unintialzied state 227 * when pf sets interface statistics clear time in boot phase if pf 228 * was statically linked to kernel. Instead of setting the bogus 229 * time value have pfi_get_ifaces handle this case. In 230 * pfi_get_ifaces it uses time_second if it sees the time is 0. 231 */ 232 kif->pfik_tzero = time_second > 1 ? time_second : 0; 233 TAILQ_INIT(&kif->pfik_dynaddrs); 234 235 RB_INSERT(pfi_ifhead, &V_pfi_ifs, kif); 236 237 return (kif); 238 } 239 240 void 241 pfi_kif_ref(struct pfi_kif *kif) 242 { 243 244 PF_RULES_WASSERT(); 245 kif->pfik_rulerefs++; 246 } 247 248 void 249 pfi_kif_unref(struct pfi_kif *kif) 250 { 251 252 PF_RULES_WASSERT(); 253 KASSERT(kif->pfik_rulerefs > 0, ("%s: %p has zero refs", __func__, kif)); 254 255 kif->pfik_rulerefs--; 256 257 if (kif->pfik_rulerefs > 0) 258 return; 259 260 /* kif referencing an existing ifnet or group should exist. */ 261 if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == V_pfi_all) 262 return; 263 264 RB_REMOVE(pfi_ifhead, &V_pfi_ifs, kif); 265 266 kif->pfik_flags |= PFI_IFLAG_REFS; 267 268 mtx_lock(&pfi_unlnkdkifs_mtx); 269 LIST_INSERT_HEAD(&V_pfi_unlinked_kifs, kif, pfik_list); 270 mtx_unlock(&pfi_unlnkdkifs_mtx); 271 } 272 273 void 274 pfi_kif_purge(void) 275 { 276 struct pfi_kif *kif, *kif1; 277 278 /* 279 * Do naive mark-and-sweep garbage collecting of old kifs. 280 * Reference flag is raised by pf_purge_expired_states(). 281 */ 282 mtx_lock(&pfi_unlnkdkifs_mtx); 283 LIST_FOREACH_SAFE(kif, &V_pfi_unlinked_kifs, pfik_list, kif1) { 284 if (!(kif->pfik_flags & PFI_IFLAG_REFS)) { 285 LIST_REMOVE(kif, pfik_list); 286 free(kif, PFI_MTYPE); 287 } else 288 kif->pfik_flags &= ~PFI_IFLAG_REFS; 289 } 290 mtx_unlock(&pfi_unlnkdkifs_mtx); 291 } 292 293 int 294 pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif) 295 { 296 struct ifg_list *p; 297 298 if (rule_kif == NULL || rule_kif == packet_kif) 299 return (1); 300 301 if (rule_kif->pfik_group != NULL) 302 /* XXXGL: locking? */ 303 TAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next) 304 if (p->ifgl_group == rule_kif->pfik_group) 305 return (1); 306 307 return (0); 308 } 309 310 static void 311 pfi_attach_ifnet(struct ifnet *ifp) 312 { 313 struct pfi_kif *kif; 314 315 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK); 316 317 PF_RULES_WLOCK(); 318 V_pfi_update++; 319 kif = pfi_kif_attach(kif, ifp->if_xname); 320 321 kif->pfik_ifp = ifp; 322 ifp->if_pf_kif = kif; 323 324 pfi_kif_update(kif); 325 PF_RULES_WUNLOCK(); 326 } 327 328 static void 329 pfi_attach_ifgroup(struct ifg_group *ifg) 330 { 331 struct pfi_kif *kif; 332 333 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK); 334 335 PF_RULES_WLOCK(); 336 V_pfi_update++; 337 kif = pfi_kif_attach(kif, ifg->ifg_group); 338 339 kif->pfik_group = ifg; 340 ifg->ifg_pf_kif = kif; 341 PF_RULES_WUNLOCK(); 342 } 343 344 int 345 pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af) 346 { 347 switch (af) { 348 #ifdef INET 349 case AF_INET: 350 switch (dyn->pfid_acnt4) { 351 case 0: 352 return (0); 353 case 1: 354 return (PF_MATCHA(0, &dyn->pfid_addr4, 355 &dyn->pfid_mask4, a, AF_INET)); 356 default: 357 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET)); 358 } 359 break; 360 #endif /* INET */ 361 #ifdef INET6 362 case AF_INET6: 363 switch (dyn->pfid_acnt6) { 364 case 0: 365 return (0); 366 case 1: 367 return (PF_MATCHA(0, &dyn->pfid_addr6, 368 &dyn->pfid_mask6, a, AF_INET6)); 369 default: 370 return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6)); 371 } 372 break; 373 #endif /* INET6 */ 374 default: 375 return (0); 376 } 377 } 378 379 int 380 pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af) 381 { 382 struct pfi_dynaddr *dyn; 383 char tblname[PF_TABLE_NAME_SIZE]; 384 struct pf_ruleset *ruleset = NULL; 385 struct pfi_kif *kif; 386 int rv = 0; 387 388 PF_RULES_WASSERT(); 389 KASSERT(aw->type == PF_ADDR_DYNIFTL, ("%s: type %u", 390 __func__, aw->type)); 391 KASSERT(aw->p.dyn == NULL, ("%s: dyn is %p", __func__, aw->p.dyn)); 392 393 if ((dyn = malloc(sizeof(*dyn), PFI_MTYPE, M_NOWAIT | M_ZERO)) == NULL) 394 return (ENOMEM); 395 396 if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT)) == NULL) { 397 free(dyn, PFI_MTYPE); 398 return (ENOMEM); 399 } 400 401 if (!strcmp(aw->v.ifname, "self")) 402 dyn->pfid_kif = pfi_kif_attach(kif, IFG_ALL); 403 else 404 dyn->pfid_kif = pfi_kif_attach(kif, aw->v.ifname); 405 pfi_kif_ref(dyn->pfid_kif); 406 407 dyn->pfid_net = pfi_unmask(&aw->v.a.mask); 408 if (af == AF_INET && dyn->pfid_net == 32) 409 dyn->pfid_net = 128; 410 strlcpy(tblname, aw->v.ifname, sizeof(tblname)); 411 if (aw->iflags & PFI_AFLAG_NETWORK) 412 strlcat(tblname, ":network", sizeof(tblname)); 413 if (aw->iflags & PFI_AFLAG_BROADCAST) 414 strlcat(tblname, ":broadcast", sizeof(tblname)); 415 if (aw->iflags & PFI_AFLAG_PEER) 416 strlcat(tblname, ":peer", sizeof(tblname)); 417 if (aw->iflags & PFI_AFLAG_NOALIAS) 418 strlcat(tblname, ":0", sizeof(tblname)); 419 if (dyn->pfid_net != 128) 420 snprintf(tblname + strlen(tblname), 421 sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net); 422 if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) { 423 rv = ENOMEM; 424 goto _bad; 425 } 426 427 if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname)) == NULL) { 428 rv = ENOMEM; 429 goto _bad; 430 } 431 432 dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE; 433 dyn->pfid_iflags = aw->iflags; 434 dyn->pfid_af = af; 435 436 TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry); 437 aw->p.dyn = dyn; 438 pfi_kif_update(dyn->pfid_kif); 439 440 return (0); 441 442 _bad: 443 if (dyn->pfid_kt != NULL) 444 pfr_detach_table(dyn->pfid_kt); 445 if (ruleset != NULL) 446 pf_remove_if_empty_ruleset(ruleset); 447 if (dyn->pfid_kif != NULL) 448 pfi_kif_unref(dyn->pfid_kif); 449 free(dyn, PFI_MTYPE); 450 451 return (rv); 452 } 453 454 static void 455 pfi_kif_update(struct pfi_kif *kif) 456 { 457 struct ifg_list *ifgl; 458 struct pfi_dynaddr *p; 459 460 PF_RULES_WASSERT(); 461 462 /* update all dynaddr */ 463 TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry) 464 pfi_dynaddr_update(p); 465 466 /* again for all groups kif is member of */ 467 if (kif->pfik_ifp != NULL) { 468 IF_ADDR_RLOCK(kif->pfik_ifp); 469 TAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next) 470 pfi_kif_update((struct pfi_kif *) 471 ifgl->ifgl_group->ifg_pf_kif); 472 IF_ADDR_RUNLOCK(kif->pfik_ifp); 473 } 474 } 475 476 static void 477 pfi_dynaddr_update(struct pfi_dynaddr *dyn) 478 { 479 struct pfi_kif *kif; 480 struct pfr_ktable *kt; 481 482 PF_RULES_WASSERT(); 483 KASSERT(dyn && dyn->pfid_kif && dyn->pfid_kt, 484 ("%s: bad argument", __func__)); 485 486 kif = dyn->pfid_kif; 487 kt = dyn->pfid_kt; 488 489 if (kt->pfrkt_larg != V_pfi_update) { 490 /* this table needs to be brought up-to-date */ 491 pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags); 492 kt->pfrkt_larg = V_pfi_update; 493 } 494 pfr_dynaddr_update(kt, dyn); 495 } 496 497 static void 498 pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, int net, int flags) 499 { 500 int e, size2 = 0; 501 struct ifg_member *ifgm; 502 503 V_pfi_buffer_cnt = 0; 504 505 if (kif->pfik_ifp != NULL) 506 pfi_instance_add(kif->pfik_ifp, net, flags); 507 else if (kif->pfik_group != NULL) { 508 IFNET_RLOCK_NOSLEEP(); 509 TAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next) 510 pfi_instance_add(ifgm->ifgm_ifp, net, flags); 511 IFNET_RUNLOCK_NOSLEEP(); 512 } 513 514 if ((e = pfr_set_addrs(&kt->pfrkt_t, V_pfi_buffer, V_pfi_buffer_cnt, &size2, 515 NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK))) 516 printf("%s: cannot set %d new addresses into table %s: %d\n", 517 __func__, V_pfi_buffer_cnt, kt->pfrkt_name, e); 518 } 519 520 static void 521 pfi_instance_add(struct ifnet *ifp, int net, int flags) 522 { 523 struct ifaddr *ia; 524 int got4 = 0, got6 = 0; 525 int net2, af; 526 527 IF_ADDR_RLOCK(ifp); 528 TAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) { 529 if (ia->ifa_addr == NULL) 530 continue; 531 af = ia->ifa_addr->sa_family; 532 if (af != AF_INET && af != AF_INET6) 533 continue; 534 /* 535 * XXX: For point-to-point interfaces, (ifname:0) and IPv4, 536 * jump over addresses without a proper route to work 537 * around a problem with ppp not fully removing the 538 * address used during IPCP. 539 */ 540 if ((ifp->if_flags & IFF_POINTOPOINT) && 541 !(ia->ifa_flags & IFA_ROUTE) && 542 (flags & PFI_AFLAG_NOALIAS) && (af == AF_INET)) 543 continue; 544 if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6) 545 continue; 546 if ((flags & PFI_AFLAG_BROADCAST) && 547 !(ifp->if_flags & IFF_BROADCAST)) 548 continue; 549 if ((flags & PFI_AFLAG_PEER) && 550 !(ifp->if_flags & IFF_POINTOPOINT)) 551 continue; 552 if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 && 553 IN6_IS_ADDR_LINKLOCAL( 554 &((struct sockaddr_in6 *)ia->ifa_addr)->sin6_addr)) 555 continue; 556 if (flags & PFI_AFLAG_NOALIAS) { 557 if (af == AF_INET && got4) 558 continue; 559 if (af == AF_INET6 && got6) 560 continue; 561 } 562 if (af == AF_INET) 563 got4 = 1; 564 else if (af == AF_INET6) 565 got6 = 1; 566 net2 = net; 567 if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) { 568 if (af == AF_INET) 569 net2 = pfi_unmask(&((struct sockaddr_in *) 570 ia->ifa_netmask)->sin_addr); 571 else if (af == AF_INET6) 572 net2 = pfi_unmask(&((struct sockaddr_in6 *) 573 ia->ifa_netmask)->sin6_addr); 574 } 575 if (af == AF_INET && net2 > 32) 576 net2 = 32; 577 if (flags & PFI_AFLAG_BROADCAST) 578 pfi_address_add(ia->ifa_broadaddr, af, net2); 579 else if (flags & PFI_AFLAG_PEER) 580 pfi_address_add(ia->ifa_dstaddr, af, net2); 581 else 582 pfi_address_add(ia->ifa_addr, af, net2); 583 } 584 IF_ADDR_RUNLOCK(ifp); 585 } 586 587 static void 588 pfi_address_add(struct sockaddr *sa, int af, int net) 589 { 590 struct pfr_addr *p; 591 int i; 592 593 if (V_pfi_buffer_cnt >= V_pfi_buffer_max) { 594 int new_max = V_pfi_buffer_max * 2; 595 596 if (new_max > PFI_BUFFER_MAX) { 597 printf("%s: address buffer full (%d/%d)\n", __func__, 598 V_pfi_buffer_cnt, PFI_BUFFER_MAX); 599 return; 600 } 601 p = malloc(new_max * sizeof(*V_pfi_buffer), PFI_MTYPE, 602 M_NOWAIT); 603 if (p == NULL) { 604 printf("%s: no memory to grow buffer (%d/%d)\n", 605 __func__, V_pfi_buffer_cnt, PFI_BUFFER_MAX); 606 return; 607 } 608 memcpy(p, V_pfi_buffer, V_pfi_buffer_max * sizeof(*V_pfi_buffer)); 609 /* no need to zero buffer */ 610 free(V_pfi_buffer, PFI_MTYPE); 611 V_pfi_buffer = p; 612 V_pfi_buffer_max = new_max; 613 } 614 if (af == AF_INET && net > 32) 615 net = 128; 616 p = V_pfi_buffer + V_pfi_buffer_cnt++; 617 bzero(p, sizeof(*p)); 618 p->pfra_af = af; 619 p->pfra_net = net; 620 if (af == AF_INET) 621 p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr; 622 else if (af == AF_INET6) { 623 p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr; 624 if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr)) 625 p->pfra_ip6addr.s6_addr16[1] = 0; 626 } 627 /* mask network address bits */ 628 if (net < 128) 629 ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8)); 630 for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++) 631 ((caddr_t)p)[i] = 0; 632 } 633 634 void 635 pfi_dynaddr_remove(struct pfi_dynaddr *dyn) 636 { 637 638 KASSERT(dyn->pfid_kif != NULL, ("%s: null pfid_kif", __func__)); 639 KASSERT(dyn->pfid_kt != NULL, ("%s: null pfid_kt", __func__)); 640 641 TAILQ_REMOVE(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry); 642 pfi_kif_unref(dyn->pfid_kif); 643 pfr_detach_table(dyn->pfid_kt); 644 free(dyn, PFI_MTYPE); 645 } 646 647 void 648 pfi_dynaddr_copyout(struct pf_addr_wrap *aw) 649 { 650 651 KASSERT(aw->type == PF_ADDR_DYNIFTL, 652 ("%s: type %u", __func__, aw->type)); 653 654 if (aw->p.dyn == NULL || aw->p.dyn->pfid_kif == NULL) 655 return; 656 aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6; 657 } 658 659 static int 660 pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q) 661 { 662 return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ)); 663 } 664 665 void 666 pfi_update_status(const char *name, struct pf_status *pfs) 667 { 668 struct pfi_kif *p; 669 struct pfi_kif_cmp key; 670 struct ifg_member p_member, *ifgm; 671 TAILQ_HEAD(, ifg_member) ifg_members; 672 int i, j, k; 673 674 strlcpy(key.pfik_name, name, sizeof(key.pfik_name)); 675 p = RB_FIND(pfi_ifhead, &V_pfi_ifs, (struct pfi_kif *)&key); 676 if (p == NULL) 677 return; 678 679 if (p->pfik_group != NULL) { 680 bcopy(&p->pfik_group->ifg_members, &ifg_members, 681 sizeof(ifg_members)); 682 } else { 683 /* build a temporary list for p only */ 684 bzero(&p_member, sizeof(p_member)); 685 p_member.ifgm_ifp = p->pfik_ifp; 686 TAILQ_INIT(&ifg_members); 687 TAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next); 688 } 689 if (pfs) { 690 bzero(pfs->pcounters, sizeof(pfs->pcounters)); 691 bzero(pfs->bcounters, sizeof(pfs->bcounters)); 692 } 693 TAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) { 694 if (ifgm->ifgm_ifp == NULL || ifgm->ifgm_ifp->if_pf_kif == NULL) 695 continue; 696 p = (struct pfi_kif *)ifgm->ifgm_ifp->if_pf_kif; 697 698 /* just clear statistics */ 699 if (pfs == NULL) { 700 bzero(p->pfik_packets, sizeof(p->pfik_packets)); 701 bzero(p->pfik_bytes, sizeof(p->pfik_bytes)); 702 p->pfik_tzero = time_second; 703 continue; 704 } 705 for (i = 0; i < 2; i++) 706 for (j = 0; j < 2; j++) 707 for (k = 0; k < 2; k++) { 708 pfs->pcounters[i][j][k] += 709 p->pfik_packets[i][j][k]; 710 pfs->bcounters[i][j] += 711 p->pfik_bytes[i][j][k]; 712 } 713 } 714 } 715 716 void 717 pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size) 718 { 719 struct pfi_kif *p, *nextp; 720 int n = 0; 721 722 for (p = RB_MIN(pfi_ifhead, &V_pfi_ifs); p; p = nextp) { 723 nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p); 724 if (pfi_skip_if(name, p)) 725 continue; 726 if (*size <= n++) 727 break; 728 if (!p->pfik_tzero) 729 p->pfik_tzero = time_second; 730 bcopy(p, buf++, sizeof(*buf)); 731 nextp = RB_NEXT(pfi_ifhead, &V_pfi_ifs, p); 732 } 733 *size = n; 734 } 735 736 static int 737 pfi_skip_if(const char *filter, struct pfi_kif *p) 738 { 739 int n; 740 741 if (filter == NULL || !*filter) 742 return (0); 743 if (!strcmp(p->pfik_name, filter)) 744 return (0); /* exact match */ 745 n = strlen(filter); 746 if (n < 1 || n >= IFNAMSIZ) 747 return (1); /* sanity check */ 748 if (filter[n-1] >= '0' && filter[n-1] <= '9') 749 return (1); /* only do exact match in that case */ 750 if (strncmp(p->pfik_name, filter, n)) 751 return (1); /* prefix doesn't match */ 752 return (p->pfik_name[n] < '0' || p->pfik_name[n] > '9'); 753 } 754 755 int 756 pfi_set_flags(const char *name, int flags) 757 { 758 struct pfi_kif *p; 759 760 RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) { 761 if (pfi_skip_if(name, p)) 762 continue; 763 p->pfik_flags |= flags; 764 } 765 return (0); 766 } 767 768 int 769 pfi_clear_flags(const char *name, int flags) 770 { 771 struct pfi_kif *p; 772 773 RB_FOREACH(p, pfi_ifhead, &V_pfi_ifs) { 774 if (pfi_skip_if(name, p)) 775 continue; 776 p->pfik_flags &= ~flags; 777 } 778 return (0); 779 } 780 781 /* from pf_print_state.c */ 782 static int 783 pfi_unmask(void *addr) 784 { 785 struct pf_addr *m = addr; 786 int i = 31, j = 0, b = 0; 787 u_int32_t tmp; 788 789 while (j < 4 && m->addr32[j] == 0xffffffff) { 790 b += 32; 791 j++; 792 } 793 if (j < 4) { 794 tmp = ntohl(m->addr32[j]); 795 for (i = 31; tmp & (1 << i); --i) 796 b++; 797 } 798 return (b); 799 } 800 801 static void 802 pfi_attach_ifnet_event(void *arg __unused, struct ifnet *ifp) 803 { 804 805 if (V_pf_vnet_active == 0) { 806 /* Avoid teardown race in the least expensive way. */ 807 return; 808 } 809 pfi_attach_ifnet(ifp); 810 #ifdef ALTQ 811 PF_RULES_WLOCK(); 812 pf_altq_ifnet_event(ifp, 0); 813 PF_RULES_WUNLOCK(); 814 #endif 815 } 816 817 static void 818 pfi_detach_ifnet_event(void *arg __unused, struct ifnet *ifp) 819 { 820 struct pfi_kif *kif = (struct pfi_kif *)ifp->if_pf_kif; 821 822 if (kif == NULL) 823 return; 824 825 if (V_pf_vnet_active == 0) { 826 /* Avoid teardown race in the least expensive way. */ 827 return; 828 } 829 PF_RULES_WLOCK(); 830 V_pfi_update++; 831 pfi_kif_update(kif); 832 833 kif->pfik_ifp = NULL; 834 ifp->if_pf_kif = NULL; 835 #ifdef ALTQ 836 pf_altq_ifnet_event(ifp, 1); 837 #endif 838 PF_RULES_WUNLOCK(); 839 } 840 841 static void 842 pfi_attach_group_event(void *arg __unused, struct ifg_group *ifg) 843 { 844 845 if (V_pf_vnet_active == 0) { 846 /* Avoid teardown race in the least expensive way. */ 847 return; 848 } 849 pfi_attach_ifgroup(ifg); 850 } 851 852 static void 853 pfi_change_group_event(void *arg __unused, char *gname) 854 { 855 struct pfi_kif *kif; 856 857 if (V_pf_vnet_active == 0) { 858 /* Avoid teardown race in the least expensive way. */ 859 return; 860 } 861 862 kif = malloc(sizeof(*kif), PFI_MTYPE, M_WAITOK); 863 PF_RULES_WLOCK(); 864 V_pfi_update++; 865 kif = pfi_kif_attach(kif, gname); 866 pfi_kif_update(kif); 867 PF_RULES_WUNLOCK(); 868 } 869 870 static void 871 pfi_detach_group_event(void *arg __unused, struct ifg_group *ifg) 872 { 873 struct pfi_kif *kif = (struct pfi_kif *)ifg->ifg_pf_kif; 874 875 if (kif == NULL) 876 return; 877 878 if (V_pf_vnet_active == 0) { 879 /* Avoid teardown race in the least expensive way. */ 880 return; 881 } 882 PF_RULES_WLOCK(); 883 V_pfi_update++; 884 885 kif->pfik_group = NULL; 886 ifg->ifg_pf_kif = NULL; 887 PF_RULES_WUNLOCK(); 888 } 889 890 static void 891 pfi_ifaddr_event(void *arg __unused, struct ifnet *ifp) 892 { 893 if (ifp->if_pf_kif == NULL) 894 return; 895 896 if (V_pf_vnet_active == 0) { 897 /* Avoid teardown race in the least expensive way. */ 898 return; 899 } 900 PF_RULES_WLOCK(); 901 if (ifp && ifp->if_pf_kif) { 902 V_pfi_update++; 903 pfi_kif_update(ifp->if_pf_kif); 904 } 905 PF_RULES_WUNLOCK(); 906 } 907