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