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