summaryrefslogtreecommitdiff
path: root/sys/net/npf/npf_params.c
blob: f34eea46ffedf819ab9da9f7fcdd24c022486d43 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-
 * Copyright (c) 2019-2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * 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.
 */

#ifdef _KERNEL
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: npf_params.c,v 1.6 2023/02/12 13:21:28 kardel Exp $");

#include <sys/param.h>
#include <sys/types.h>

#include <sys/kmem.h>
#include <sys/queue.h>
#include <sys/thmap.h>
#endif

#include "npf_impl.h"

typedef struct npf_paramreg {
	struct npf_paramreg *	next;
	unsigned		count;
	npf_param_t		params[];
} npf_paramreg_t;

struct npf_paraminfo {
	npf_paramreg_t *	list;
	thmap_t *		map;
};

static inline void
npf_param_general_register(npf_t *npf)
{
	npf_param_t param_map[] = {
		{
			"ip4.reassembly",
			&npf->ip4_reassembly,
			.default_val = 1, // true - host requirements
			.min = 0, .max = 1
		},
		{
			"ip6.reassembly",
			&npf->ip6_reassembly,
			.default_val = 0, // false
			.min = 0, .max = 1
		},
	};
	npf_param_register(npf, param_map, __arraycount(param_map));
}

static uintptr_t
npf_param_thmap_alloc(size_t len)
{
	return (uintptr_t)kmem_alloc(len, KM_SLEEP);
}

static void
npf_param_thmap_free(uintptr_t addr, size_t len)  
{
        kmem_free((void *)addr, len);
} 

static const thmap_ops_t npf_param_thmap_ops = {
        .alloc = npf_param_thmap_alloc,
        .free = npf_param_thmap_free
};

void
npf_param_init(npf_t *npf)
{
	npf_paraminfo_t *paraminfo;

	paraminfo = kmem_zalloc(sizeof(npf_paraminfo_t), KM_SLEEP);
	paraminfo->map = thmap_create(0, &npf_param_thmap_ops, THMAP_NOCOPY);
	npf->paraminfo = paraminfo;

	/* Register some general parameters. */
	npf_param_general_register(npf);
}

void
npf_param_fini(npf_t *npf)
{
	npf_paraminfo_t *pinfo = npf->paraminfo;
	npf_paramreg_t *paramreg = pinfo->list;

	while (paramreg) {
		npf_param_t *plist = paramreg->params;
		npf_paramreg_t *next = paramreg->next;
		size_t len;

		/* Remove the parameters from the map. */
		for (unsigned i = 0; i < paramreg->count; i++) {
			npf_param_t *param = &plist[i];
			const char *name = param->name;
			void *ret __diagused;

			ret = thmap_del(pinfo->map, name, strlen(name));
			KASSERT(ret != NULL);
		}

		/* Destroy this registry. */
		len = offsetof(npf_paramreg_t, params[paramreg->count]);
		kmem_free(paramreg, len);

		/* Next .. */
		paramreg = next;
	}
	thmap_destroy(pinfo->map);
	kmem_free(pinfo, sizeof(npf_paraminfo_t));
}

int
npf_params_export(const npf_t *npf, nvlist_t *nv)
{
	nvlist_t *params, *dparams;

	/*
	 * Export both the active and default values.  The latter are to
	 * accommodate npfctl so it could distinguish what has been set.
	 */
	params = nvlist_create(0);
	dparams = nvlist_create(0);
	for (npf_paramreg_t *pr = npf->paraminfo->list; pr; pr = pr->next) {
		for (unsigned i = 0; i < pr->count; i++) {
			const npf_param_t *param = &pr->params[i];
			const uint64_t val = *param->valp;
			const uint64_t defval = param->default_val;

			nvlist_add_number(params, param->name, val);
			nvlist_add_number(dparams, param->name, defval);
		}
	}
	nvlist_add_nvlist(nv, "params", params);
	nvlist_add_nvlist(nv, "params-defaults", dparams);
	return 0;
}

void *
npf_param_allocgroup(npf_t *npf, npf_paramgroup_t group, size_t len)
{
	void *params = kmem_zalloc(len, KM_SLEEP);
	npf->params[group] = params;
	return params;
}

void
npf_param_freegroup(npf_t *npf, npf_paramgroup_t group, size_t len)
{
	kmem_free(npf->params[group], len);
	npf->params[group] = NULL; // diagnostic
}

/*
 * npf_param_register: register an array of named parameters.
 */
void
npf_param_register(npf_t *npf, npf_param_t *params, unsigned count)
{
	npf_paraminfo_t *pinfo = npf->paraminfo;
	npf_paramreg_t *paramreg;
	size_t len;

	/*
	 * Copy over the parameters.
	 */
	len = offsetof(npf_paramreg_t, params[count]);
	paramreg = kmem_zalloc(len, KM_SLEEP);
	memcpy(paramreg->params, params, sizeof(npf_param_t) * count);
	paramreg->count = count;
	params = NULL; // dead

	/*
	 * Map the parameter names to the variables.
	 * Assign the default values.
	 */
	for (unsigned i = 0; i < count; i++) {
		npf_param_t *param = &paramreg->params[i];
		const char *name = param->name;
		void *ret __diagused;

		ret = thmap_put(pinfo->map, name, strlen(name), param);
		KASSERTMSG(ret == param,
		    "parameter insertion failed: ret=%p, param=%p",
		    ret, param);

		/* Assign the default value. */
		KASSERT(param->default_val >= param->min);
		KASSERT(param->default_val <= param->max);
		*param->valp = param->default_val;
	}

	/* Insert the registry of params into the list. */
	paramreg->next = pinfo->list;
	pinfo->list = paramreg;
}

/*
 * NPF param API.
 */

static npf_param_t *
npf_param_lookup(npf_t *npf, const char *name)
{
	npf_paraminfo_t *pinfo = npf->paraminfo;
	const size_t namelen = strlen(name);
	return thmap_get(pinfo->map, name, namelen);
}

int
npf_param_check(npf_t *npf, const char *name, int val)
{
	npf_param_t *param;

	if ((param = npf_param_lookup(npf, name)) == NULL) {
		return ENOENT;
	}
	if (val < param->min || val > param->max) {
		return EINVAL;
	}
	return 0;
}

__dso_public int
npfk_param_get(npf_t *npf, const char *name, int *val)
{
	npf_param_t *param;

	if ((param = npf_param_lookup(npf, name)) == NULL) {
		return ENOENT;
	}
	*val = *param->valp;
	return 0;
}

__dso_public int
npfk_param_set(npf_t *npf, const char *name, int val)
{
	npf_param_t *param;

	if ((param = npf_param_lookup(npf, name)) == NULL) {
		return ENOENT;
	}
	if (val < param->min || val > param->max) {
		return EINVAL;
	}
	*param->valp = val;
	return 0;
}