summaryrefslogtreecommitdiff
path: root/sys/net/npf/npf_if.c
blob: cec30431eeff393ccb3be04a6c84b16485490478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*-
 * Copyright (c) 2019 Mindaugas Rasiukevicius <rmind at noxt eu>
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Mindaugas Rasiukevicius.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * NPF network interface handling.
 *
 * NPF uses its own interface IDs (npf-if-id).  These IDs start from 1.
 * Zero is reserved to indicate "no interface" case or an interface of
 * no interest (i.e. not registered).
 *
 * This module provides an interface to primarily handle the following:
 *
 * - Bind a symbolic interface name to NPF interface ID.
 * - Associate NPF interface ID when the network interface is attached.
 *
 * When NPF configuration is (re)loaded, each referenced network interface
 * name is registered with a unique ID.  If the network interface is already
 * attached, then the ID is associated with it immediately; otherwise, IDs
 * are associated/disassociated on interface events which are monitored
 * using pfil(9) hooks.
 *
 * To avoid race conditions when an active NPF configuration is updated or
 * interfaces are detached/attached, the interface names are never removed
 * and therefore IDs are never re-assigned.  The only point when interface
 * names and IDs are cleared is when the configuration is flushed.
 *
 * A linear counter is used for IDs.
 */

#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_if.c,v 1.13 2020/05/30 14:16:56 rmind Exp $");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/kmem.h>
#include <net/if.h>
#endif

#include "npf_impl.h"

typedef struct npf_ifmap {
	char		ifname[IFNAMSIZ + 1];
} npf_ifmap_t;

#define	NPF_IFMAP_NOID			(0U)
#define	NPF_IFMAP_SLOT2ID(npf, slot)	((npf)->ifmap_off + (slot) + 1)
#define	NPF_IFMAP_ID2SLOT(npf, id)	\
    ((id) - atomic_load_relaxed(&(npf)->ifmap_off) - 1)

void
npf_ifmap_init(npf_t *npf, const npf_ifops_t *ifops)
{
	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;

	KASSERT(ifops != NULL);
	ifops->flush(npf, (void *)(uintptr_t)0);

	mutex_init(&npf->ifmap_lock, MUTEX_DEFAULT, IPL_SOFTNET);
	npf->ifmap = kmem_zalloc(nbytes, KM_SLEEP);
	npf->ifmap_cnt = 0;
	npf->ifmap_off = 0;
	npf->ifops = ifops;
}

void
npf_ifmap_fini(npf_t *npf)
{
	const size_t nbytes = sizeof(npf_ifmap_t) * NPF_MAX_IFMAP;
	mutex_destroy(&npf->ifmap_lock);
	kmem_free(npf->ifmap, nbytes);
}

static unsigned
npf_ifmap_lookup(npf_t *npf, const char *ifname)
{
	KASSERT(mutex_owned(&npf->ifmap_lock));

	for (unsigned i = 0; i < npf->ifmap_cnt; i++) {
		npf_ifmap_t *ifmap = &npf->ifmap[i];

		if (strcmp(ifmap->ifname, ifname) == 0) {
			return NPF_IFMAP_SLOT2ID(npf, i);
		}
	}
	return NPF_IFMAP_NOID;
}

/*
 * npf_ifmap_register: register an interface name; return an assigned
 * NPF network ID on success (non-zero).
 *
 * This routine is mostly called on NPF configuration (re)load for the
 * interfaces names referenced by the rules.
 */
unsigned
npf_ifmap_register(npf_t *npf, const char *ifname)
{
	npf_ifmap_t *ifmap;
	unsigned id, i;
	ifnet_t *ifp;

	mutex_enter(&npf->ifmap_lock);
	if ((id = npf_ifmap_lookup(npf, ifname)) != NPF_IFMAP_NOID) {
		goto out;
	}
	if (npf->ifmap_cnt == NPF_MAX_IFMAP) {
		printf("npf_ifmap_new: out of slots; bump NPF_MAX_IFMAP\n");
		id = NPF_IFMAP_NOID;
		goto out;
	}
	KASSERT(npf->ifmap_cnt < NPF_MAX_IFMAP);

	/* Allocate a new slot and convert and assign an ID. */
	i = npf->ifmap_cnt++;
	ifmap = &npf->ifmap[i];
	strlcpy(ifmap->ifname, ifname, IFNAMSIZ);
	id = NPF_IFMAP_SLOT2ID(npf, i);

	if ((ifp = npf->ifops->lookup(npf, ifname)) != NULL) {
		npf->ifops->setmeta(npf, ifp, (void *)(uintptr_t)id);
	}
out:
	mutex_exit(&npf->ifmap_lock);
	return id;
}

void
npf_ifmap_flush(npf_t *npf)
{
	mutex_enter(&npf->ifmap_lock);
	npf->ifops->flush(npf, (void *)(uintptr_t)NPF_IFMAP_NOID);
	for (unsigned i = 0; i < npf->ifmap_cnt; i++) {
		npf->ifmap[i].ifname[0] = '\0';
	}
	npf->ifmap_cnt = 0;

	/*
	 * Reset the ID counter if reaching the overflow; this is not
	 * realistic, but we maintain correctness.
	 */
	if (npf->ifmap_off < (UINT_MAX - NPF_MAX_IFMAP)) {
		npf->ifmap_off += NPF_MAX_IFMAP;
	} else {
		npf->ifmap_off = 0;
	}
	mutex_exit(&npf->ifmap_lock);
}

/*
 * npf_ifmap_getid: get the ID for the given network interface.
 *
 * => This routine is typically called from the packet handler when
 *    matching whether the packet is on particular network interface.
 *
 * => This routine is lock-free; if the NPF configuration is flushed
 *    while the packet is in-flight, the ID will not match because we
 *    keep the IDs linear.
 */
unsigned
npf_ifmap_getid(npf_t *npf, const ifnet_t *ifp)
{
	const unsigned id = (uintptr_t)npf->ifops->getmeta(npf, ifp);
	return id;
}

/*
 * npf_ifmap_copylogname: this function is toxic; it can return garbage
 * as we don't lock, but it is only used temporarily and only for logging.
 */
void
npf_ifmap_copylogname(npf_t *npf, unsigned id, char *buf, size_t len)
{
	const unsigned i = NPF_IFMAP_ID2SLOT(npf, id);

	membar_consumer();

	if (id != NPF_IFMAP_NOID && i < NPF_MAX_IFMAP) {
		/*
		 * Lock-free access is safe as there is an extra byte
		 * with a permanent NUL terminator at the end.
		 */
		const npf_ifmap_t *ifmap = &npf->ifmap[i];
		strlcpy(buf, ifmap->ifname, MIN(len, IFNAMSIZ));
	} else {
		strlcpy(buf, "???", len);
	}
}

void
npf_ifmap_copyname(npf_t *npf, unsigned id, char *buf, size_t len)
{
	mutex_enter(&npf->ifmap_lock);
	npf_ifmap_copylogname(npf, id, buf, len);
	mutex_exit(&npf->ifmap_lock);
}

__dso_public void
npfk_ifmap_attach(npf_t *npf, ifnet_t *ifp)
{
	const npf_ifops_t *ifops = npf->ifops;
	unsigned id;

	mutex_enter(&npf->ifmap_lock);
	id = npf_ifmap_lookup(npf, ifops->getname(npf, ifp));
	ifops->setmeta(npf, ifp, (void *)(uintptr_t)id);
	mutex_exit(&npf->ifmap_lock);
}

__dso_public void
npfk_ifmap_detach(npf_t *npf, ifnet_t *ifp)
{
	/* Diagnostic. */
	mutex_enter(&npf->ifmap_lock);
	npf->ifops->setmeta(npf, ifp, (void *)(uintptr_t)NPF_IFMAP_NOID);
	mutex_exit(&npf->ifmap_lock);
}