Home | History | Annotate | Line # | Download | only in npf
npf_if.c revision 1.6.2.1
      1  1.6.2.1  pgoyette /*	$NetBSD: npf_if.c,v 1.6.2.1 2017/01/07 08:56:50 pgoyette 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.6.2.1  pgoyette #ifdef _KERNEL
     48      1.1     rmind #include <sys/cdefs.h>
     49  1.6.2.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: npf_if.c,v 1.6.2.1 2017/01/07 08:56:50 pgoyette 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.6.2.1  pgoyette #endif
     56      1.1     rmind 
     57      1.1     rmind #include "npf_impl.h"
     58      1.1     rmind 
     59  1.6.2.1  pgoyette 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.6.2.1  pgoyette void
     64  1.6.2.1  pgoyette npf_ifmap_init(npf_t *npf, const npf_ifops_t *ifops)
     65  1.6.2.1  pgoyette {
     66  1.6.2.1  pgoyette 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     67  1.6.2.1  pgoyette 
     68  1.6.2.1  pgoyette 	KASSERT(ifops != NULL);
     69  1.6.2.1  pgoyette 	ifops->flush((void *)(uintptr_t)0);
     70  1.6.2.1  pgoyette 
     71  1.6.2.1  pgoyette 	npf->ifmap = kmem_zalloc(nbytes, KM_SLEEP);
     72  1.6.2.1  pgoyette 	npf->ifmap_cnt = 0;
     73  1.6.2.1  pgoyette 	npf->ifops = ifops;
     74  1.6.2.1  pgoyette }
     75  1.6.2.1  pgoyette 
     76  1.6.2.1  pgoyette void
     77  1.6.2.1  pgoyette npf_ifmap_fini(npf_t *npf)
     78  1.6.2.1  pgoyette {
     79  1.6.2.1  pgoyette 	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
     80  1.6.2.1  pgoyette 	kmem_free(npf->ifmap, nbytes);
     81  1.6.2.1  pgoyette }
     82      1.1     rmind 
     83      1.1     rmind static u_int
     84  1.6.2.1  pgoyette npf_ifmap_new(npf_t *npf)
     85      1.1     rmind {
     86  1.6.2.1  pgoyette 	KASSERT(npf_config_locked_p(npf));
     87      1.1     rmind 
     88  1.6.2.1  pgoyette 	for (u_int i = 0; i < npf->ifmap_cnt; i++)
     89  1.6.2.1  pgoyette 		if (npf->ifmap[i].n_ifname[0] == '\0')
     90      1.1     rmind 			return i + 1;
     91      1.1     rmind 
     92  1.6.2.1  pgoyette 	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.6.2.1  pgoyette 	return ++npf->ifmap_cnt;
     97      1.1     rmind }
     98      1.1     rmind 
     99      1.1     rmind static u_int
    100  1.6.2.1  pgoyette npf_ifmap_lookup(npf_t *npf, const char *ifname)
    101      1.1     rmind {
    102  1.6.2.1  pgoyette 	KASSERT(npf_config_locked_p(npf));
    103      1.1     rmind 
    104  1.6.2.1  pgoyette 	for (u_int i = 0; i < npf->ifmap_cnt; i++) {
    105  1.6.2.1  pgoyette 		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.6.2.1  pgoyette 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.6.2.1  pgoyette 	npf_config_enter(npf);
    121  1.6.2.1  pgoyette 	if ((i = npf_ifmap_lookup(npf, ifname)) != 0) {
    122      1.1     rmind 		goto out;
    123      1.1     rmind 	}
    124  1.6.2.1  pgoyette 	if ((i = npf_ifmap_new(npf)) == 0) {
    125      1.1     rmind 		goto out;
    126      1.1     rmind 	}
    127  1.6.2.1  pgoyette 	nim = &npf->ifmap[i - 1];
    128      1.1     rmind 	strlcpy(nim->n_ifname, ifname, IFNAMSIZ);
    129      1.1     rmind 
    130  1.6.2.1  pgoyette 	if ((ifp = npf->ifops->lookup(ifname)) != NULL) {
    131  1.6.2.1  pgoyette 		npf->ifops->setmeta(ifp, (void *)(uintptr_t)i);
    132      1.1     rmind 	}
    133      1.1     rmind out:
    134  1.6.2.1  pgoyette 	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.6.2.1  pgoyette npf_ifmap_flush(npf_t *npf)
    140      1.1     rmind {
    141  1.6.2.1  pgoyette 	KASSERT(npf_config_locked_p(npf));
    142      1.1     rmind 
    143  1.6.2.1  pgoyette 	for (u_int i = 0; i < npf->ifmap_cnt; i++) {
    144  1.6.2.1  pgoyette 		npf->ifmap[i].n_ifname[0] = '\0';
    145      1.1     rmind 	}
    146  1.6.2.1  pgoyette 	npf->ifmap_cnt = 0;
    147  1.6.2.1  pgoyette 	npf->ifops->flush((void *)(uintptr_t)0);
    148      1.1     rmind }
    149      1.1     rmind 
    150      1.1     rmind u_int
    151  1.6.2.1  pgoyette npf_ifmap_getid(npf_t *npf, const ifnet_t *ifp)
    152      1.1     rmind {
    153  1.6.2.1  pgoyette 	const u_int i = (uintptr_t)npf->ifops->getmeta(ifp);
    154  1.6.2.1  pgoyette 	KASSERT(i <= npf->ifmap_cnt);
    155      1.1     rmind 	return i;
    156      1.1     rmind }
    157      1.1     rmind 
    158      1.4     rmind const char *
    159  1.6.2.1  pgoyette npf_ifmap_getname(npf_t *npf, const u_int id)
    160      1.4     rmind {
    161      1.4     rmind 	const char *ifname;
    162      1.4     rmind 
    163  1.6.2.1  pgoyette 	KASSERT(npf_config_locked_p(npf));
    164  1.6.2.1  pgoyette 	KASSERT(id > 0 && id <= npf->ifmap_cnt);
    165      1.4     rmind 
    166  1.6.2.1  pgoyette 	ifname = npf->ifmap[id - 1].n_ifname;
    167      1.4     rmind 	KASSERT(ifname[0] != '\0');
    168      1.4     rmind 	return ifname;
    169      1.4     rmind }
    170      1.4     rmind 
    171  1.6.2.1  pgoyette __dso_public void
    172  1.6.2.1  pgoyette npf_ifmap_attach(npf_t *npf, ifnet_t *ifp)
    173      1.1     rmind {
    174  1.6.2.1  pgoyette 	const npf_ifops_t *ifops = npf->ifops;
    175  1.6.2.1  pgoyette 	u_int i;
    176  1.6.2.1  pgoyette 
    177  1.6.2.1  pgoyette 	npf_config_enter(npf);
    178  1.6.2.1  pgoyette 	i = npf_ifmap_lookup(npf, ifops->getname(ifp));
    179  1.6.2.1  pgoyette 	ifops->setmeta(ifp, (void *)(uintptr_t)i);
    180  1.6.2.1  pgoyette 	npf_config_exit(npf);
    181      1.1     rmind }
    182      1.1     rmind 
    183  1.6.2.1  pgoyette __dso_public void
    184  1.6.2.1  pgoyette npf_ifmap_detach(npf_t *npf, ifnet_t *ifp)
    185      1.1     rmind {
    186      1.5     rmind 	/* Diagnostic. */
    187  1.6.2.1  pgoyette 	npf_config_enter(npf);
    188  1.6.2.1  pgoyette 	npf->ifops->setmeta(ifp, (void *)(uintptr_t)0);
    189  1.6.2.1  pgoyette 	npf_config_exit(npf);
    190      1.1     rmind }
    191