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