Home | History | Annotate | Line # | Download | only in npf
npf_if.c revision 1.10
      1 /*-
      2  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Mindaugas Rasiukevicius.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * NPF network interface handling module.
     32  *
     33  * NPF uses its own interface IDs (npf-if-id).  When NPF configuration is
     34  * (re)loaded, each required interface name is registered and a matching
     35  * network interface gets an ID assigned.  If an interface is not present,
     36  * it gets an ID on attach.
     37  *
     38  * IDs start from 1.  Zero is reserved to indicate "no interface" case or
     39  * an interface of no interest (i.e. not registered).
     40  *
     41  * The IDs are mapped synchronously based on interface events which are
     42  * monitored using pfil(9) hooks.
     43  */
     44 
     45 #ifdef _KERNEL
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: npf_if.c,v 1.10 2019/08/11 20:26:33 rmind Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/types.h>
     51 #include <sys/kmem.h>
     52 #include <net/if.h>
     53 #endif
     54 
     55 #include "npf_impl.h"
     56 
     57 typedef struct npf_ifmap {
     58 	char		n_ifname[IFNAMSIZ];
     59 } npf_ifmap_t;
     60 
     61 void
     62 npf_ifmap_init(npf_t *npf, const npf_ifops_t *ifops)
     63 {
     64 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     65 
     66 	KASSERT(ifops != NULL);
     67 	ifops->flush((void *)(uintptr_t)0);
     68 
     69 	npf->ifmap = kmem_zalloc(nbytes, KM_SLEEP);
     70 	npf->ifmap_cnt = 0;
     71 	npf->ifops = ifops;
     72 }
     73 
     74 void
     75 npf_ifmap_fini(npf_t *npf)
     76 {
     77 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     78 	kmem_free(npf->ifmap, nbytes);
     79 }
     80 
     81 static u_int
     82 npf_ifmap_new(npf_t *npf)
     83 {
     84 	KASSERT(npf_config_locked_p(npf));
     85 
     86 	for (u_int i = 0; i < npf->ifmap_cnt; i++)
     87 		if (npf->ifmap[i].n_ifname[0] == '\0')
     88 			return i + 1;
     89 
     90 	if (npf->ifmap_cnt == NPF_MAX_IFMAP) {
     91 		printf("npf_ifmap_new: out of slots; bump NPF_MAX_IFMAP\n");
     92 		return 0;
     93 	}
     94 	return ++npf->ifmap_cnt;
     95 }
     96 
     97 static u_int
     98 npf_ifmap_lookup(npf_t *npf, const char *ifname)
     99 {
    100 	KASSERT(npf_config_locked_p(npf));
    101 
    102 	for (u_int i = 0; i < npf->ifmap_cnt; i++) {
    103 		npf_ifmap_t *nim = &npf->ifmap[i];
    104 
    105 		if (nim->n_ifname[0] && strcmp(nim->n_ifname, ifname) == 0)
    106 			return i + 1;
    107 	}
    108 	return 0;
    109 }
    110 
    111 u_int
    112 npf_ifmap_register(npf_t *npf, const char *ifname)
    113 {
    114 	npf_ifmap_t *nim;
    115 	ifnet_t *ifp;
    116 	u_int i;
    117 
    118 	npf_config_enter(npf);
    119 	if ((i = npf_ifmap_lookup(npf, ifname)) != 0) {
    120 		goto out;
    121 	}
    122 	if ((i = npf_ifmap_new(npf)) == 0) {
    123 		goto out;
    124 	}
    125 	nim = &npf->ifmap[i - 1];
    126 	strlcpy(nim->n_ifname, ifname, IFNAMSIZ);
    127 
    128 	if ((ifp = npf->ifops->lookup(ifname)) != NULL) {
    129 		npf->ifops->setmeta(ifp, (void *)(uintptr_t)i);
    130 	}
    131 out:
    132 	npf_config_exit(npf);
    133 	return i;
    134 }
    135 
    136 void
    137 npf_ifmap_flush(npf_t *npf)
    138 {
    139 	KASSERT(npf_config_locked_p(npf));
    140 
    141 	for (u_int i = 0; i < npf->ifmap_cnt; i++) {
    142 		npf->ifmap[i].n_ifname[0] = '\0';
    143 	}
    144 	npf->ifmap_cnt = 0;
    145 	npf->ifops->flush((void *)(uintptr_t)0);
    146 }
    147 
    148 u_int
    149 npf_ifmap_getid(npf_t *npf, const ifnet_t *ifp)
    150 {
    151 	const u_int i = (uintptr_t)npf->ifops->getmeta(ifp);
    152 	KASSERT(i <= npf->ifmap_cnt);
    153 	return i;
    154 }
    155 
    156 /*
    157  * This function is toxic; it can return garbage since we don't
    158  * lock, but it is only used temporarily and only for logging.
    159  */
    160 void
    161 npf_ifmap_copyname(npf_t *npf, u_int id, char *buf, size_t len)
    162 {
    163 	if (id > 0 && id < npf->ifmap_cnt)
    164 		strlcpy(buf, npf->ifmap[id - 1].n_ifname,
    165 		    MIN(len, sizeof(npf->ifmap[id - 1].n_ifname)));
    166 	else
    167 		strlcpy(buf, "???", len);
    168 }
    169 
    170 const char *
    171 npf_ifmap_getname(npf_t *npf, const u_int id)
    172 {
    173 	const char *ifname;
    174 
    175 	KASSERT(npf_config_locked_p(npf));
    176 	KASSERT(id > 0 && id <= npf->ifmap_cnt);
    177 
    178 	ifname = npf->ifmap[id - 1].n_ifname;
    179 	KASSERT(ifname[0] != '\0');
    180 	return ifname;
    181 }
    182 
    183 __dso_public void
    184 npfk_ifmap_attach(npf_t *npf, ifnet_t *ifp)
    185 {
    186 	const npf_ifops_t *ifops = npf->ifops;
    187 	u_int i;
    188 
    189 	npf_config_enter(npf);
    190 	i = npf_ifmap_lookup(npf, ifops->getname(ifp));
    191 	ifops->setmeta(ifp, (void *)(uintptr_t)i);
    192 	npf_config_exit(npf);
    193 }
    194 
    195 __dso_public void
    196 npfk_ifmap_detach(npf_t *npf, ifnet_t *ifp)
    197 {
    198 	/* Diagnostic. */
    199 	npf_config_enter(npf);
    200 	npf->ifops->setmeta(ifp, (void *)(uintptr_t)0);
    201 	npf_config_exit(npf);
    202 }
    203