Home | History | Annotate | Line # | Download | only in npf
npf_if.c revision 1.8.14.2
      1       1.1     rmind /*-
      2  1.8.14.2    martin  * Copyright (c) 2019 Mindaugas Rasiukevicius <rmind at noxt eu>
      3       1.1     rmind  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      4       1.1     rmind  * All rights reserved.
      5       1.1     rmind  *
      6       1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      7       1.1     rmind  * by Mindaugas Rasiukevicius.
      8       1.1     rmind  *
      9       1.1     rmind  * Redistribution and use in source and binary forms, with or without
     10       1.1     rmind  * modification, are permitted provided that the following conditions
     11       1.1     rmind  * are met:
     12       1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     13       1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     14       1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     16       1.1     rmind  *    documentation and/or other materials provided with the distribution.
     17       1.1     rmind  *
     18       1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19       1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20       1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21       1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22       1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23       1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24       1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25       1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26       1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27       1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28       1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     29       1.1     rmind  */
     30       1.1     rmind 
     31       1.1     rmind /*
     32  1.8.14.2    martin  * NPF network interface handling.
     33       1.1     rmind  *
     34  1.8.14.2    martin  * NPF uses its own interface IDs (npf-if-id).  These IDs start from 1.
     35  1.8.14.2    martin  * Zero is reserved to indicate "no interface" case or an interface of
     36  1.8.14.2    martin  * no interest (i.e. not registered).
     37       1.5     rmind  *
     38  1.8.14.2    martin  * This module provides an interface to primarily handle the following:
     39       1.1     rmind  *
     40  1.8.14.2    martin  * - Bind a symbolic interface name to NPF interface ID.
     41  1.8.14.2    martin  * - Associate NPF interface ID when the network interface is attached.
     42  1.8.14.2    martin  *
     43  1.8.14.2    martin  * When NPF configuration is (re)loaded, each referenced network interface
     44  1.8.14.2    martin  * name is registered with a unique ID.  If the network interface is already
     45  1.8.14.2    martin  * attached, then the ID is associated with it immediately; otherwise, IDs
     46  1.8.14.2    martin  * are associated/disassociated on interface events which are monitored
     47  1.8.14.2    martin  * using pfil(9) hooks.
     48  1.8.14.2    martin  *
     49  1.8.14.2    martin  * To avoid race conditions when an active NPF configuration is updated or
     50  1.8.14.2    martin  * interfaces are detached/attached, the interface names are never removed
     51  1.8.14.2    martin  * and therefore IDs are never re-assigned.  The only point when interface
     52  1.8.14.2    martin  * names and IDs are cleared is when the configuration is flushed.
     53  1.8.14.2    martin  *
     54  1.8.14.2    martin  * A linear counter is used for IDs.
     55       1.1     rmind  */
     56       1.1     rmind 
     57       1.7  christos #ifdef _KERNEL
     58       1.1     rmind #include <sys/cdefs.h>
     59  1.8.14.2    martin __KERNEL_RCSID(0, "$NetBSD: npf_if.c,v 1.8.14.2 2020/04/13 08:05:15 martin Exp $");
     60       1.1     rmind 
     61       1.1     rmind #include <sys/param.h>
     62       1.1     rmind #include <sys/types.h>
     63       1.1     rmind #include <sys/kmem.h>
     64       1.1     rmind #include <net/if.h>
     65       1.7  christos #endif
     66       1.1     rmind 
     67       1.1     rmind #include "npf_impl.h"
     68       1.1     rmind 
     69       1.7  christos typedef struct npf_ifmap {
     70  1.8.14.2    martin 	char		ifname[IFNAMSIZ + 1];
     71       1.1     rmind } npf_ifmap_t;
     72       1.1     rmind 
     73  1.8.14.2    martin #define	NPF_IFMAP_NOID			(0U)
     74  1.8.14.2    martin #define	NPF_IFMAP_SLOT2ID(npf, slot)	((npf)->ifmap_off + (slot) + 1)
     75  1.8.14.2    martin #define	NPF_IFMAP_ID2SLOT(npf, id)	((id) - (npf)->ifmap_off - 1)
     76  1.8.14.2    martin 
     77       1.7  christos void
     78       1.7  christos npf_ifmap_init(npf_t *npf, const npf_ifops_t *ifops)
     79       1.7  christos {
     80       1.7  christos 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     81       1.7  christos 
     82       1.7  christos 	KASSERT(ifops != NULL);
     83       1.7  christos 	ifops->flush((void *)(uintptr_t)0);
     84       1.7  christos 
     85  1.8.14.2    martin 	mutex_init(&npf->ifmap_lock, MUTEX_DEFAULT, IPL_SOFTNET);
     86       1.7  christos 	npf->ifmap = kmem_zalloc(nbytes, KM_SLEEP);
     87       1.7  christos 	npf->ifmap_cnt = 0;
     88  1.8.14.2    martin 	npf->ifmap_off = 0;
     89       1.7  christos 	npf->ifops = ifops;
     90       1.7  christos }
     91       1.7  christos 
     92       1.7  christos void
     93       1.7  christos npf_ifmap_fini(npf_t *npf)
     94       1.7  christos {
     95       1.7  christos 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     96  1.8.14.2    martin 	mutex_destroy(&npf->ifmap_lock);
     97       1.7  christos 	kmem_free(npf->ifmap, nbytes);
     98       1.7  christos }
     99       1.1     rmind 
    100  1.8.14.2    martin static unsigned
    101       1.7  christos npf_ifmap_lookup(npf_t *npf, const char *ifname)
    102       1.1     rmind {
    103  1.8.14.2    martin 	KASSERT(mutex_owned(&npf->ifmap_lock));
    104       1.1     rmind 
    105  1.8.14.2    martin 	for (unsigned i = 0; i < npf->ifmap_cnt; i++) {
    106  1.8.14.2    martin 		npf_ifmap_t *ifmap = &npf->ifmap[i];
    107       1.1     rmind 
    108  1.8.14.2    martin 		if (strcmp(ifmap->ifname, ifname) == 0) {
    109  1.8.14.2    martin 			return NPF_IFMAP_SLOT2ID(npf, i);
    110  1.8.14.2    martin 		}
    111       1.1     rmind 	}
    112  1.8.14.2    martin 	return NPF_IFMAP_NOID;
    113       1.1     rmind }
    114       1.1     rmind 
    115  1.8.14.2    martin /*
    116  1.8.14.2    martin  * npf_ifmap_register: register an interface name; return an assigned
    117  1.8.14.2    martin  * NPF network ID on success (non-zero).
    118  1.8.14.2    martin  *
    119  1.8.14.2    martin  * This routine is mostly called on NPF configuration (re)load for the
    120  1.8.14.2    martin  * interfaces names referenced by the rules.
    121  1.8.14.2    martin  */
    122  1.8.14.2    martin unsigned
    123       1.7  christos npf_ifmap_register(npf_t *npf, const char *ifname)
    124       1.1     rmind {
    125  1.8.14.2    martin 	npf_ifmap_t *ifmap;
    126  1.8.14.2    martin 	unsigned id, i;
    127       1.1     rmind 	ifnet_t *ifp;
    128       1.1     rmind 
    129  1.8.14.2    martin 	mutex_enter(&npf->ifmap_lock);
    130  1.8.14.2    martin 	if ((id = npf_ifmap_lookup(npf, ifname)) != NPF_IFMAP_NOID) {
    131       1.1     rmind 		goto out;
    132       1.1     rmind 	}
    133  1.8.14.2    martin 	if (npf->ifmap_cnt == NPF_MAX_IFMAP) {
    134  1.8.14.2    martin 		printf("npf_ifmap_new: out of slots; bump NPF_MAX_IFMAP\n");
    135  1.8.14.2    martin 		id = NPF_IFMAP_NOID;
    136       1.1     rmind 		goto out;
    137       1.1     rmind 	}
    138  1.8.14.2    martin 	KASSERT(npf->ifmap_cnt < NPF_MAX_IFMAP);
    139  1.8.14.2    martin 
    140  1.8.14.2    martin 	/* Allocate a new slot and convert and assign an ID. */
    141  1.8.14.2    martin 	i = npf->ifmap_cnt++;
    142  1.8.14.2    martin 	ifmap = &npf->ifmap[i];
    143  1.8.14.2    martin 	strlcpy(ifmap->ifname, ifname, IFNAMSIZ);
    144  1.8.14.2    martin 	id = NPF_IFMAP_SLOT2ID(npf, i);
    145       1.1     rmind 
    146       1.7  christos 	if ((ifp = npf->ifops->lookup(ifname)) != NULL) {
    147  1.8.14.2    martin 		npf->ifops->setmeta(ifp, (void *)(uintptr_t)id);
    148       1.1     rmind 	}
    149       1.1     rmind out:
    150  1.8.14.2    martin 	mutex_exit(&npf->ifmap_lock);
    151  1.8.14.2    martin 	return id;
    152       1.1     rmind }
    153       1.1     rmind 
    154       1.1     rmind void
    155       1.7  christos npf_ifmap_flush(npf_t *npf)
    156       1.1     rmind {
    157  1.8.14.2    martin 	mutex_enter(&npf->ifmap_lock);
    158  1.8.14.2    martin 	npf->ifops->flush((void *)(uintptr_t)NPF_IFMAP_NOID);
    159  1.8.14.2    martin 	for (unsigned i = 0; i < npf->ifmap_cnt; i++) {
    160  1.8.14.2    martin 		npf->ifmap[i].ifname[0] = '\0';
    161       1.1     rmind 	}
    162       1.7  christos 	npf->ifmap_cnt = 0;
    163  1.8.14.2    martin 
    164  1.8.14.2    martin 	/*
    165  1.8.14.2    martin 	 * Reset the ID counter if reaching the overflow; this is not
    166  1.8.14.2    martin 	 * realistic, but we maintain correctness.
    167  1.8.14.2    martin 	 */
    168  1.8.14.2    martin 	if (npf->ifmap_off < (UINT_MAX - NPF_MAX_IFMAP)) {
    169  1.8.14.2    martin 		npf->ifmap_off += NPF_MAX_IFMAP;
    170  1.8.14.2    martin 	} else {
    171  1.8.14.2    martin 		npf->ifmap_off = 0;
    172  1.8.14.2    martin 	}
    173  1.8.14.2    martin 	mutex_exit(&npf->ifmap_lock);
    174       1.1     rmind }
    175       1.1     rmind 
    176  1.8.14.2    martin /*
    177  1.8.14.2    martin  * npf_ifmap_getid: get the ID for the given network interface.
    178  1.8.14.2    martin  *
    179  1.8.14.2    martin  * => This routine is typically called from the packet handler when
    180  1.8.14.2    martin  *    matching whether the packet is on particular network interface.
    181  1.8.14.2    martin  *
    182  1.8.14.2    martin  * => This routine is lock-free; if the NPF configuration is flushed
    183  1.8.14.2    martin  *    while the packet is in-flight, the ID will not match because we
    184  1.8.14.2    martin  *    keep the IDs linear.
    185  1.8.14.2    martin  */
    186  1.8.14.2    martin unsigned
    187       1.7  christos npf_ifmap_getid(npf_t *npf, const ifnet_t *ifp)
    188       1.1     rmind {
    189  1.8.14.2    martin 	const unsigned id = (uintptr_t)npf->ifops->getmeta(ifp);
    190  1.8.14.2    martin 	return id;
    191       1.1     rmind }
    192       1.1     rmind 
    193       1.8  christos /*
    194  1.8.14.2    martin  * npf_ifmap_copylogname: this function is toxic; it can return garbage
    195  1.8.14.2    martin  * as we don't lock, but it is only used temporarily and only for logging.
    196       1.8  christos  */
    197       1.8  christos void
    198  1.8.14.2    martin npf_ifmap_copylogname(npf_t *npf, unsigned id, char *buf, size_t len)
    199       1.8  christos {
    200  1.8.14.2    martin 	const unsigned i = NPF_IFMAP_ID2SLOT(npf, id);
    201  1.8.14.2    martin 
    202  1.8.14.2    martin 	membar_consumer();
    203  1.8.14.2    martin 
    204  1.8.14.2    martin 	if (id != NPF_IFMAP_NOID && i < NPF_MAX_IFMAP) {
    205  1.8.14.2    martin 		/*
    206  1.8.14.2    martin 		 * Lock-free access is safe as there is an extra byte
    207  1.8.14.2    martin 		 * with a permanent NUL terminator at the end.
    208  1.8.14.2    martin 		 */
    209  1.8.14.2    martin 		const npf_ifmap_t *ifmap = &npf->ifmap[i];
    210  1.8.14.2    martin 		strlcpy(buf, ifmap->ifname, MIN(len, IFNAMSIZ));
    211  1.8.14.2    martin 	} else {
    212       1.8  christos 		strlcpy(buf, "???", len);
    213  1.8.14.2    martin 	}
    214       1.8  christos }
    215       1.8  christos 
    216  1.8.14.2    martin void
    217  1.8.14.2    martin npf_ifmap_copyname(npf_t *npf, unsigned id, char *buf, size_t len)
    218       1.4     rmind {
    219  1.8.14.2    martin 	mutex_enter(&npf->ifmap_lock);
    220  1.8.14.2    martin 	npf_ifmap_copylogname(npf, id, buf, len);
    221  1.8.14.2    martin 	mutex_exit(&npf->ifmap_lock);
    222       1.4     rmind }
    223       1.4     rmind 
    224       1.7  christos __dso_public void
    225  1.8.14.2    martin npfk_ifmap_attach(npf_t *npf, ifnet_t *ifp)
    226       1.1     rmind {
    227       1.7  christos 	const npf_ifops_t *ifops = npf->ifops;
    228  1.8.14.2    martin 	unsigned id;
    229       1.7  christos 
    230  1.8.14.2    martin 	mutex_enter(&npf->ifmap_lock);
    231  1.8.14.2    martin 	id = npf_ifmap_lookup(npf, ifops->getname(ifp));
    232  1.8.14.2    martin 	ifops->setmeta(ifp, (void *)(uintptr_t)id);
    233  1.8.14.2    martin 	mutex_exit(&npf->ifmap_lock);
    234       1.1     rmind }
    235       1.1     rmind 
    236       1.7  christos __dso_public void
    237  1.8.14.2    martin npfk_ifmap_detach(npf_t *npf, ifnet_t *ifp)
    238       1.1     rmind {
    239       1.5     rmind 	/* Diagnostic. */
    240  1.8.14.2    martin 	mutex_enter(&npf->ifmap_lock);
    241  1.8.14.2    martin 	npf->ifops->setmeta(ifp, (void *)(uintptr_t)NPF_IFMAP_NOID);
    242  1.8.14.2    martin 	mutex_exit(&npf->ifmap_lock);
    243       1.1     rmind }
    244