The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netpfil/pf/pf_if.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

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

Cache object: aab1e8e138e5da8b0491399d099fd8a6


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.