npf_connkey.c revision 1.2 1 1.1 rmind /*-
2 1.2 rmind * Copyright (c) 2014-2020 Mindaugas Rasiukevicius <rmind at netbsd org>
3 1.1 rmind * Copyright (c) 2010-2014 The NetBSD Foundation, Inc.
4 1.1 rmind * All rights reserved.
5 1.1 rmind *
6 1.1 rmind * This material is based upon work partially supported by The
7 1.1 rmind * NetBSD Foundation under a contract with Mindaugas Rasiukevicius.
8 1.1 rmind *
9 1.1 rmind * Redistribution and use in source and binary forms, with or without
10 1.1 rmind * modification, are permitted provided that the following conditions
11 1.1 rmind * are met:
12 1.1 rmind * 1. Redistributions of source code must retain the above copyright
13 1.1 rmind * notice, this list of conditions and the following disclaimer.
14 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 rmind * notice, this list of conditions and the following disclaimer in the
16 1.1 rmind * documentation and/or other materials provided with the distribution.
17 1.1 rmind *
18 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
29 1.1 rmind */
30 1.1 rmind
31 1.1 rmind /*
32 1.2 rmind * Connection key -- is an n-tuple structure encoding the address length,
33 1.2 rmind * layer 3 protocol, source and destination addresses and ports (or other
34 1.2 rmind * protocol IDs) and some configurable elements (see below).
35 1.1 rmind *
36 1.1 rmind * Key layout
37 1.1 rmind *
38 1.2 rmind * The single key is formed out of 32-bit integers. The layout is
39 1.2 rmind * as follows (first row -- fields, second row -- number of bits):
40 1.1 rmind *
41 1.2 rmind * | alen | proto | ckey | src-id | dst-id | src-addr | dst-addr |
42 1.2 rmind * +------+-------+--------+--------+--------+----------+----------+
43 1.2 rmind * | 4 | 8 | 20 | 16 | 16 | 32-128 | 32-128 |
44 1.1 rmind *
45 1.1 rmind * The source and destination are inverted if the key is for the
46 1.2 rmind * backwards stream (NPF_FLOW_BACK). The address length depends on
47 1.2 rmind * the 'alen' field. The length is in words and is either 1 or 4,
48 1.2 rmind * meaning 4 or 16 in bytes.
49 1.2 rmind *
50 1.2 rmind * The 20-bit configurable key area ('ckey') is for the optional
51 1.2 rmind * elements which may be included or excluded by the user. It has
52 1.2 rmind * the following layout:
53 1.2 rmind *
54 1.2 rmind * | direction | interface-id |
55 1.2 rmind * +-----------+--------------+
56 1.2 rmind * | 2 | 18 |
57 1.2 rmind *
58 1.2 rmind * Note: neither direction nor interface ID cannot be zero; we rely
59 1.2 rmind * on this by reserving the zero 'ckey' value to for the case when
60 1.2 rmind * these checks are not applicable.
61 1.1 rmind *
62 1.1 rmind * Embedding in the connection structure (npf_conn_t)
63 1.1 rmind *
64 1.1 rmind * Two keys are stored in the npf_conn_t::c_keys[] array, which is
65 1.1 rmind * variable-length, depending on whether the keys store IPv4 or IPv6
66 1.1 rmind * addresses. The length of the first key determines the position
67 1.1 rmind * of the second key.
68 1.2 rmind *
69 1.2 rmind * WARNING: the keys must be immutable while they are in conndb.
70 1.1 rmind */
71 1.1 rmind
72 1.1 rmind #ifdef _KERNEL
73 1.1 rmind #include <sys/cdefs.h>
74 1.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf_connkey.c,v 1.2 2020/05/30 14:16:56 rmind Exp $");
75 1.1 rmind
76 1.1 rmind #include <sys/param.h>
77 1.1 rmind #include <sys/types.h>
78 1.1 rmind #endif
79 1.1 rmind
80 1.1 rmind #define __NPF_CONN_PRIVATE
81 1.1 rmind #include "npf_conn.h"
82 1.1 rmind #include "npf_impl.h"
83 1.1 rmind
84 1.2 rmind unsigned
85 1.2 rmind npf_connkey_setkey(npf_connkey_t *key, unsigned alen, unsigned proto,
86 1.2 rmind const void *ipv, const uint16_t *id, const npf_flow_t flow)
87 1.1 rmind {
88 1.1 rmind const npf_addr_t * const *ips = ipv;
89 1.1 rmind uint32_t *k = key->ck_key;
90 1.1 rmind unsigned isrc, idst;
91 1.1 rmind
92 1.2 rmind if (__predict_true(flow == NPF_FLOW_FORW)) {
93 1.1 rmind isrc = NPF_SRC, idst = NPF_DST;
94 1.1 rmind } else {
95 1.1 rmind isrc = NPF_DST, idst = NPF_SRC;
96 1.1 rmind }
97 1.1 rmind
98 1.1 rmind /*
99 1.1 rmind * See the key layout explanation above.
100 1.1 rmind */
101 1.2 rmind KASSERT((alen >> 2) <= 0xf && proto <= 0xff);
102 1.2 rmind k[0] = ((uint32_t)(alen >> 2) << 28) | (proto << 20);
103 1.1 rmind k[1] = ((uint32_t)id[isrc] << 16) | id[idst];
104 1.1 rmind
105 1.1 rmind if (__predict_true(alen == sizeof(in_addr_t))) {
106 1.1 rmind k[2] = ips[isrc]->word32[0];
107 1.1 rmind k[3] = ips[idst]->word32[0];
108 1.1 rmind return 4 * sizeof(uint32_t);
109 1.1 rmind } else {
110 1.1 rmind const unsigned nwords = alen >> 2;
111 1.1 rmind memcpy(&k[2], ips[isrc], alen);
112 1.1 rmind memcpy(&k[2 + nwords], ips[idst], alen);
113 1.1 rmind return (2 + (nwords * 2)) * sizeof(uint32_t);
114 1.1 rmind }
115 1.1 rmind }
116 1.1 rmind
117 1.2 rmind void
118 1.2 rmind npf_connkey_getkey(const npf_connkey_t *key, unsigned *alen, unsigned *proto,
119 1.2 rmind npf_addr_t *ips, uint16_t *id)
120 1.1 rmind {
121 1.1 rmind const uint32_t *k = key->ck_key;
122 1.1 rmind
123 1.1 rmind /*
124 1.1 rmind * See the key layout explanation above.
125 1.1 rmind */
126 1.1 rmind
127 1.2 rmind *alen = (k[0] >> 28) << 2;
128 1.2 rmind *proto = (k[0] >> 16) & 0xff;
129 1.1 rmind id[NPF_SRC] = k[1] >> 16;
130 1.1 rmind id[NPF_DST] = k[1] & 0xffff;
131 1.1 rmind
132 1.1 rmind switch (*alen) {
133 1.1 rmind case sizeof(struct in6_addr):
134 1.1 rmind case sizeof(struct in_addr):
135 1.1 rmind memcpy(&ips[NPF_SRC], &k[2], *alen);
136 1.1 rmind memcpy(&ips[NPF_DST], &k[2 + ((unsigned)*alen >> 2)], *alen);
137 1.1 rmind return;
138 1.1 rmind default:
139 1.1 rmind KASSERT(0);
140 1.1 rmind }
141 1.1 rmind }
142 1.1 rmind
143 1.2 rmind static inline void
144 1.2 rmind npf_connkey_setckey(npf_connkey_t *key, unsigned ifid, unsigned di)
145 1.2 rmind {
146 1.2 rmind if (ifid) {
147 1.2 rmind /*
148 1.2 rmind * Interface ID: the lower 18 bits of the 20-bit 'ckey'.
149 1.2 rmind * Note: the interface ID cannot be zero.
150 1.2 rmind */
151 1.2 rmind CTASSERT(NPF_MAX_IFMAP < (1U << 18));
152 1.2 rmind key->ck_key[0] |= ifid;
153 1.2 rmind }
154 1.2 rmind if (di) {
155 1.2 rmind /*
156 1.2 rmind * Direction: The highest 2 bits of the 20-bit 'ckey'.
157 1.2 rmind * Note: we rely on PFIL_IN and PFIL_OUT definitions.
158 1.2 rmind */
159 1.2 rmind CTASSERT(PFIL_IN == 0x1 || PFIL_OUT == 0x2);
160 1.2 rmind KASSERT((di & ~PFIL_ALL) == 0);
161 1.2 rmind key->ck_key[0] |= ((uint32_t)di << 18);
162 1.2 rmind }
163 1.2 rmind }
164 1.2 rmind
165 1.2 rmind static void
166 1.2 rmind npf_connkey_getckey(const npf_connkey_t *key, unsigned *ifid, unsigned *di)
167 1.2 rmind {
168 1.2 rmind const uint32_t * const k = key->ck_key;
169 1.2 rmind
170 1.2 rmind *ifid = k[0] & ((1U << 20) - 1);
171 1.2 rmind *di = (k[0] >> 18) & PFIL_ALL;
172 1.2 rmind }
173 1.2 rmind
174 1.1 rmind /*
175 1.2 rmind * npf_conn_adjkey: adjust the connection key by setting the address/port.
176 1.2 rmind *
177 1.2 rmind * => The 'which' must either be NPF_SRC or NPF_DST.
178 1.1 rmind */
179 1.1 rmind void
180 1.1 rmind npf_conn_adjkey(npf_connkey_t *key, const npf_addr_t *naddr,
181 1.2 rmind const uint16_t id, const unsigned which)
182 1.1 rmind {
183 1.2 rmind const unsigned alen = NPF_CONNKEY_ALEN(key);
184 1.1 rmind uint32_t * const k = key->ck_key;
185 1.2 rmind uint32_t *addr = &k[2 + ((alen >> 2) * which)];
186 1.1 rmind
187 1.2 rmind KASSERT(which == NPF_SRC || which == NPF_DST);
188 1.1 rmind KASSERT(alen > 0);
189 1.1 rmind memcpy(addr, naddr, alen);
190 1.1 rmind
191 1.1 rmind if (id) {
192 1.1 rmind const uint32_t oid = k[1];
193 1.2 rmind const unsigned shift = 16 * !which;
194 1.1 rmind const uint32_t mask = 0xffff0000 >> shift;
195 1.1 rmind k[1] = ((uint32_t)id << shift) | (oid & mask);
196 1.1 rmind }
197 1.1 rmind }
198 1.1 rmind
199 1.2 rmind static unsigned
200 1.2 rmind npf_connkey_copy(const npf_connkey_t *skey, npf_connkey_t *dkey, bool invert)
201 1.2 rmind {
202 1.2 rmind const unsigned klen = NPF_CONNKEY_LEN(skey);
203 1.2 rmind const uint32_t *sk = skey->ck_key;
204 1.2 rmind uint32_t *dk = dkey->ck_key;
205 1.2 rmind
206 1.2 rmind if (invert) {
207 1.2 rmind const unsigned alen = NPF_CONNKEY_ALEN(skey);
208 1.2 rmind const unsigned nwords = alen >> 2;
209 1.2 rmind
210 1.2 rmind dk[0] = sk[1];
211 1.2 rmind dk[1] = (sk[1] >> 16) | (sk[1] << 16);
212 1.2 rmind memcpy(&dk[2], &sk[2 + nwords], alen);
213 1.2 rmind memcpy(&dk[2 + nwords], &sk[2], alen);
214 1.2 rmind } else {
215 1.2 rmind memcpy(dk, sk, klen);
216 1.2 rmind }
217 1.2 rmind return klen;
218 1.2 rmind }
219 1.2 rmind
220 1.1 rmind /*
221 1.1 rmind * npf_conn_conkey: construct a key for the connection lookup.
222 1.1 rmind *
223 1.1 rmind * => Returns the key length in bytes or zero on failure.
224 1.1 rmind */
225 1.1 rmind unsigned
226 1.2 rmind npf_conn_conkey(const npf_cache_t *npc, npf_connkey_t *key,
227 1.2 rmind const unsigned di, const npf_flow_t flow)
228 1.1 rmind {
229 1.2 rmind const npf_conn_params_t *params = npc->npc_ctx->params[NPF_PARAMS_CONN];
230 1.2 rmind const nbuf_t *nbuf = npc->npc_nbuf;
231 1.1 rmind const unsigned proto = npc->npc_proto;
232 1.1 rmind const unsigned alen = npc->npc_alen;
233 1.1 rmind const struct tcphdr *th;
234 1.1 rmind const struct udphdr *uh;
235 1.1 rmind uint16_t id[2] = { 0, 0 };
236 1.2 rmind unsigned ret;
237 1.2 rmind
238 1.2 rmind if (npc->npc_ckey) {
239 1.2 rmind /*
240 1.2 rmind * Request to override the connection key.
241 1.2 rmind */
242 1.2 rmind const bool invert = flow != NPF_FLOW_FORW;
243 1.2 rmind return npf_connkey_copy(npc->npc_ckey, key, invert);
244 1.2 rmind }
245 1.1 rmind
246 1.1 rmind switch (proto) {
247 1.1 rmind case IPPROTO_TCP:
248 1.1 rmind KASSERT(npf_iscached(npc, NPC_TCP));
249 1.1 rmind th = npc->npc_l4.tcp;
250 1.1 rmind id[NPF_SRC] = th->th_sport;
251 1.1 rmind id[NPF_DST] = th->th_dport;
252 1.1 rmind break;
253 1.1 rmind case IPPROTO_UDP:
254 1.1 rmind KASSERT(npf_iscached(npc, NPC_UDP));
255 1.1 rmind uh = npc->npc_l4.udp;
256 1.1 rmind id[NPF_SRC] = uh->uh_sport;
257 1.1 rmind id[NPF_DST] = uh->uh_dport;
258 1.1 rmind break;
259 1.1 rmind case IPPROTO_ICMP:
260 1.1 rmind if (npf_iscached(npc, NPC_ICMP_ID)) {
261 1.1 rmind const struct icmp *ic = npc->npc_l4.icmp;
262 1.1 rmind id[NPF_SRC] = ic->icmp_id;
263 1.1 rmind id[NPF_DST] = ic->icmp_id;
264 1.1 rmind break;
265 1.1 rmind }
266 1.1 rmind return 0;
267 1.1 rmind case IPPROTO_ICMPV6:
268 1.1 rmind if (npf_iscached(npc, NPC_ICMP_ID)) {
269 1.1 rmind const struct icmp6_hdr *ic6 = npc->npc_l4.icmp6;
270 1.1 rmind id[NPF_SRC] = ic6->icmp6_id;
271 1.1 rmind id[NPF_DST] = ic6->icmp6_id;
272 1.1 rmind break;
273 1.1 rmind }
274 1.1 rmind return 0;
275 1.1 rmind default:
276 1.1 rmind /* Unsupported protocol. */
277 1.1 rmind return 0;
278 1.1 rmind }
279 1.2 rmind
280 1.2 rmind ret = npf_connkey_setkey(key, alen, proto, npc->npc_ips, id, flow);
281 1.2 rmind npf_connkey_setckey(key,
282 1.2 rmind params->connkey_interface ? nbuf->nb_ifid : 0,
283 1.2 rmind params->connkey_direction ? (di & PFIL_ALL) : 0);
284 1.2 rmind return ret;
285 1.1 rmind }
286 1.1 rmind
287 1.1 rmind /*
288 1.1 rmind * npf_conn_getforwkey: get the address to the "forwards" key.
289 1.1 rmind */
290 1.1 rmind npf_connkey_t *
291 1.1 rmind npf_conn_getforwkey(npf_conn_t *conn)
292 1.1 rmind {
293 1.1 rmind return (void *)&conn->c_keys[0];
294 1.1 rmind }
295 1.1 rmind
296 1.1 rmind /*
297 1.1 rmind * npf_conn_getbackkey: get the address to the "backwards" key.
298 1.1 rmind *
299 1.1 rmind * => It depends on the address length.
300 1.1 rmind */
301 1.1 rmind npf_connkey_t *
302 1.1 rmind npf_conn_getbackkey(npf_conn_t *conn, unsigned alen)
303 1.1 rmind {
304 1.1 rmind const unsigned off = 2 + ((alen * 2) >> 2);
305 1.1 rmind KASSERT(off == NPF_CONNKEY_V4WORDS || off == NPF_CONNKEY_V6WORDS);
306 1.1 rmind return (void *)&conn->c_keys[off];
307 1.1 rmind }
308 1.1 rmind
309 1.1 rmind /*
310 1.1 rmind * Connection key exporting/importing.
311 1.1 rmind */
312 1.1 rmind
313 1.1 rmind nvlist_t *
314 1.2 rmind npf_connkey_export(npf_t *npf, const npf_connkey_t *key)
315 1.1 rmind {
316 1.2 rmind unsigned alen, proto, ifid, di;
317 1.1 rmind npf_addr_t ips[2];
318 1.2 rmind uint16_t ids[2];
319 1.2 rmind nvlist_t *key_nv;
320 1.2 rmind
321 1.2 rmind key_nv = nvlist_create(0);
322 1.2 rmind
323 1.2 rmind npf_connkey_getkey(key, &alen, &proto, ips, ids);
324 1.2 rmind nvlist_add_number(key_nv, "proto", proto);
325 1.2 rmind nvlist_add_number(key_nv, "sport", ids[NPF_SRC]);
326 1.2 rmind nvlist_add_number(key_nv, "dport", ids[NPF_DST]);
327 1.2 rmind nvlist_add_binary(key_nv, "saddr", &ips[NPF_SRC], alen);
328 1.2 rmind nvlist_add_binary(key_nv, "daddr", &ips[NPF_DST], alen);
329 1.2 rmind
330 1.2 rmind npf_connkey_getckey(key, &ifid, &di);
331 1.2 rmind if (ifid) {
332 1.2 rmind char ifname[IFNAMSIZ];
333 1.2 rmind npf_ifmap_copyname(npf, ifid, ifname, sizeof(ifname));
334 1.2 rmind nvlist_add_string(key_nv, "ifname", ifname);
335 1.2 rmind }
336 1.2 rmind if (di) {
337 1.2 rmind nvlist_add_number(key_nv, "di", di);
338 1.2 rmind }
339 1.1 rmind
340 1.2 rmind return key_nv;
341 1.1 rmind }
342 1.1 rmind
343 1.1 rmind unsigned
344 1.2 rmind npf_connkey_import(npf_t *npf, const nvlist_t *key_nv, npf_connkey_t *key)
345 1.1 rmind {
346 1.1 rmind npf_addr_t const * ips[2];
347 1.2 rmind size_t alen1, alen2, proto;
348 1.2 rmind unsigned ret, di, ifid = 0;
349 1.2 rmind const char *ifname;
350 1.2 rmind uint16_t ids[2];
351 1.1 rmind
352 1.2 rmind proto = dnvlist_get_number(key_nv, "proto", 0);
353 1.2 rmind if (proto >= IPPROTO_MAX) {
354 1.2 rmind return 0;
355 1.2 rmind }
356 1.2 rmind ids[NPF_SRC] = dnvlist_get_number(key_nv, "sport", 0);
357 1.2 rmind ids[NPF_DST] = dnvlist_get_number(key_nv, "dport", 0);
358 1.2 rmind ips[NPF_SRC] = dnvlist_get_binary(key_nv, "saddr", &alen1, NULL, 0);
359 1.2 rmind ips[NPF_DST] = dnvlist_get_binary(key_nv, "daddr", &alen2, NULL, 0);
360 1.1 rmind if (alen1 == 0 || alen1 > sizeof(npf_addr_t) || alen1 != alen2) {
361 1.1 rmind return 0;
362 1.1 rmind }
363 1.2 rmind ret = npf_connkey_setkey(key, alen1, proto, ips, ids, NPF_FLOW_FORW);
364 1.2 rmind if (ret == 0) {
365 1.2 rmind return 0;
366 1.2 rmind }
367 1.2 rmind
368 1.2 rmind ifname = dnvlist_get_string(key_nv, "ifname", NULL);
369 1.2 rmind if (ifname && (ifid = npf_ifmap_register(npf, ifname)) == 0) {
370 1.2 rmind return 0;
371 1.2 rmind }
372 1.2 rmind di = dnvlist_get_number(key_nv, "di", 0) & PFIL_ALL;
373 1.2 rmind npf_connkey_setckey(key, ifid, di);
374 1.2 rmind
375 1.2 rmind return ret;
376 1.1 rmind }
377 1.1 rmind
378 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
379 1.1 rmind
380 1.1 rmind void
381 1.1 rmind npf_connkey_print(const npf_connkey_t *key)
382 1.1 rmind {
383 1.2 rmind unsigned alen, proto, ifid, di;
384 1.1 rmind npf_addr_t ips[2];
385 1.2 rmind uint16_t ids[2];
386 1.1 rmind
387 1.2 rmind npf_connkey_getkey(key, &alen, &proto, ips, ids);
388 1.2 rmind npf_connkey_getckey(key, &ifid, &di);
389 1.2 rmind printf("\tkey (ifid %u, di %x)\t", ifid, di);
390 1.2 rmind printf("%s:%u", npf_addr_dump(&ips[0], alen), ids[0]);
391 1.2 rmind printf("-> %s:%u\n", npf_addr_dump(&ips[1], alen), ids[1]);
392 1.1 rmind }
393 1.1 rmind
394 1.1 rmind #endif
395