npf_nat.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_nat.c,v 1.1 2010/08/22 18:56:22 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * This material is based upon work partially supported by The
8 1.1 rmind * NetBSD Foundation under a contract with 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 address port translation (NAPT).
34 1.1 rmind * Described in RFC 2663, RFC 3022. Commonly just "NAT".
35 1.1 rmind *
36 1.1 rmind * Overview
37 1.1 rmind *
38 1.1 rmind * There are few mechanisms: NAT policy, port map and translation.
39 1.1 rmind * NAT module has a separate ruleset, where rules contain associated
40 1.1 rmind * NAT policy, thus flexible filter criteria can be used.
41 1.1 rmind *
42 1.1 rmind * NAT policies and port maps
43 1.1 rmind *
44 1.1 rmind * NAT policy is applied when a packet matches the rule. Apart from
45 1.1 rmind * filter criteria, NAT policy has a translation (gateway) IP address
46 1.1 rmind * and associated port map. Port map is a bitmap used to reserve and
47 1.1 rmind * use unique TCP/UDP ports for translation. Port maps are unique to
48 1.1 rmind * the IP addresses, therefore multiple NAT policies with the same IP
49 1.1 rmind * will share the same port map.
50 1.1 rmind *
51 1.1 rmind * NAT sessions and translation entries
52 1.1 rmind *
53 1.1 rmind * NAT module relies on session management module. Each "NAT" session
54 1.1 rmind * has an associated translation entry (npf_nat_t). It contains local
55 1.1 rmind * i.e. original IP address with port and translation port, allocated
56 1.1 rmind * from the port map. Each NAT translation entry is associated with
57 1.1 rmind * the policy, which contains translation IP address. Allocated port
58 1.1 rmind * is returned to the port map and translation entry destroyed when
59 1.1 rmind * "NAT" session expires.
60 1.1 rmind */
61 1.1 rmind
62 1.1 rmind #ifdef _KERNEL
63 1.1 rmind #include <sys/cdefs.h>
64 1.1 rmind __KERNEL_RCSID(0, "$NetBSD: npf_nat.c,v 1.1 2010/08/22 18:56:22 rmind Exp $");
65 1.1 rmind
66 1.1 rmind #include <sys/param.h>
67 1.1 rmind #include <sys/kernel.h>
68 1.1 rmind #endif
69 1.1 rmind
70 1.1 rmind #include <sys/atomic.h>
71 1.1 rmind #include <sys/bitops.h>
72 1.1 rmind #include <sys/kmem.h>
73 1.1 rmind #include <sys/pool.h>
74 1.1 rmind #include <net/pfil.h>
75 1.1 rmind #include <netinet/in.h>
76 1.1 rmind
77 1.1 rmind #include "npf_impl.h"
78 1.1 rmind
79 1.1 rmind /*
80 1.1 rmind * NPF portmap structure.
81 1.1 rmind */
82 1.1 rmind typedef struct {
83 1.1 rmind u_int p_refcnt;
84 1.1 rmind uint32_t p_bitmap[0];
85 1.1 rmind } npf_portmap_t;
86 1.1 rmind
87 1.1 rmind /* Portmap range: [ 1024 .. 65535 ] */
88 1.1 rmind #define PORTMAP_FIRST (1024)
89 1.1 rmind #define PORTMAP_SIZE ((65536 - PORTMAP_FIRST) / 32)
90 1.1 rmind #define PORTMAP_FILLED ((uint32_t)~0)
91 1.1 rmind #define PORTMAP_MASK (31)
92 1.1 rmind #define PORTMAP_SHIFT (5)
93 1.1 rmind
94 1.1 rmind /* NAT policy structure. */
95 1.1 rmind struct npf_natpolicy {
96 1.1 rmind LIST_ENTRY(npf_natpolicy) n_entry;
97 1.1 rmind in_addr_t n_gw_ip;
98 1.1 rmind npf_portmap_t * n_portmap;
99 1.1 rmind };
100 1.1 rmind
101 1.1 rmind /* NAT translation entry for a session. */
102 1.1 rmind struct npf_nat {
103 1.1 rmind npf_natpolicy_t * nt_natpolicy;
104 1.1 rmind /* Local address and port (for backwards translation). */
105 1.1 rmind in_addr_t nt_laddr;
106 1.1 rmind in_port_t nt_lport;
107 1.1 rmind /* Translation port (for forwards). */
108 1.1 rmind in_port_t nt_tport;
109 1.1 rmind /* ALG (if any) associated with this NAT entry. */
110 1.1 rmind npf_alg_t * nt_alg;
111 1.1 rmind uintptr_t nt_alg_arg;
112 1.1 rmind };
113 1.1 rmind
114 1.1 rmind static npf_ruleset_t * nat_ruleset;
115 1.1 rmind static LIST_HEAD(, npf_natpolicy) nat_policy_list;
116 1.1 rmind static pool_cache_t nat_cache;
117 1.1 rmind
118 1.1 rmind /*
119 1.1 rmind * npf_nat_sys{init,fini}: initialise/destroy NAT subsystem structures.
120 1.1 rmind */
121 1.1 rmind
122 1.1 rmind void
123 1.1 rmind npf_nat_sysinit(void)
124 1.1 rmind {
125 1.1 rmind
126 1.1 rmind nat_cache = pool_cache_init(sizeof(npf_nat_t), coherency_unit,
127 1.1 rmind 0, 0, "npfnatpl", NULL, IPL_NET, NULL, NULL, NULL);
128 1.1 rmind KASSERT(nat_cache != NULL);
129 1.1 rmind nat_ruleset = npf_ruleset_create();
130 1.1 rmind LIST_INIT(&nat_policy_list);
131 1.1 rmind }
132 1.1 rmind
133 1.1 rmind void
134 1.1 rmind npf_nat_sysfini(void)
135 1.1 rmind {
136 1.1 rmind
137 1.1 rmind /* Flush NAT policies. */
138 1.1 rmind npf_nat_reload(NULL);
139 1.1 rmind KASSERT(LIST_EMPTY(&nat_policy_list));
140 1.1 rmind pool_cache_destroy(nat_cache);
141 1.1 rmind }
142 1.1 rmind
143 1.1 rmind /*
144 1.1 rmind * npf_nat_newpolicy: allocate a new NAT policy.
145 1.1 rmind *
146 1.1 rmind * => Shares portmap if policy is on existing translation address.
147 1.1 rmind * => XXX: serialise at upper layer.
148 1.1 rmind */
149 1.1 rmind npf_natpolicy_t *
150 1.1 rmind npf_nat_newpolicy(in_addr_t gip)
151 1.1 rmind {
152 1.1 rmind npf_natpolicy_t *np, *it;
153 1.1 rmind npf_portmap_t *pm;
154 1.1 rmind
155 1.1 rmind np = kmem_zalloc(sizeof(npf_natpolicy_t), KM_SLEEP);
156 1.1 rmind if (np == NULL) {
157 1.1 rmind return NULL;
158 1.1 rmind }
159 1.1 rmind np->n_gw_ip = gip;
160 1.1 rmind
161 1.1 rmind /* Search for a NAT policy using the same translation address. */
162 1.1 rmind pm = NULL;
163 1.1 rmind LIST_FOREACH(it, &nat_policy_list, n_entry) {
164 1.1 rmind if (it->n_gw_ip != np->n_gw_ip)
165 1.1 rmind continue;
166 1.1 rmind pm = it->n_portmap;
167 1.1 rmind break;
168 1.1 rmind }
169 1.1 rmind if (pm == NULL) {
170 1.1 rmind /* Allocate a new port map for the NAT policy. */
171 1.1 rmind pm = kmem_zalloc(sizeof(npf_portmap_t) +
172 1.1 rmind (PORTMAP_SIZE * sizeof(uint32_t)), KM_SLEEP);
173 1.1 rmind if (pm == NULL) {
174 1.1 rmind kmem_free(np, sizeof(npf_natpolicy_t));
175 1.1 rmind return NULL;
176 1.1 rmind }
177 1.1 rmind pm->p_refcnt = 1;
178 1.1 rmind KASSERT((uintptr_t)pm->p_bitmap == (uintptr_t)pm + sizeof(*pm));
179 1.1 rmind } else {
180 1.1 rmind /* Share the port map. */
181 1.1 rmind pm->p_refcnt++;
182 1.1 rmind }
183 1.1 rmind np->n_portmap = pm;
184 1.1 rmind /*
185 1.1 rmind * Note: old policies with new might co-exist in the list,
186 1.1 rmind * while reload is in progress, but that is not an issue.
187 1.1 rmind */
188 1.1 rmind LIST_INSERT_HEAD(&nat_policy_list, np, n_entry);
189 1.1 rmind return np;
190 1.1 rmind }
191 1.1 rmind
192 1.1 rmind /*
193 1.1 rmind * npf_nat_freepolicy: free NAT policy and, on last reference, free portmap.
194 1.1 rmind *
195 1.1 rmind * => Called from npf_rule_free() during the reload via npf_nat_reload().
196 1.1 rmind */
197 1.1 rmind void
198 1.1 rmind npf_nat_freepolicy(npf_natpolicy_t *np)
199 1.1 rmind {
200 1.1 rmind npf_portmap_t *pm = np->n_portmap;
201 1.1 rmind
202 1.1 rmind LIST_REMOVE(np, n_entry);
203 1.1 rmind if (--pm->p_refcnt == 0) {
204 1.1 rmind kmem_free(pm, sizeof(npf_portmap_t) +
205 1.1 rmind (PORTMAP_SIZE * sizeof(uint32_t)));
206 1.1 rmind }
207 1.1 rmind kmem_free(np, sizeof(npf_natpolicy_t));
208 1.1 rmind }
209 1.1 rmind
210 1.1 rmind /*
211 1.1 rmind * npf_nat_reload: activate new ruleset of NAT policies and destroy old.
212 1.1 rmind *
213 1.1 rmind * => Destruction of ruleset will perform npf_nat_freepolicy() for each policy.
214 1.1 rmind */
215 1.1 rmind void
216 1.1 rmind npf_nat_reload(npf_ruleset_t *nset)
217 1.1 rmind {
218 1.1 rmind npf_ruleset_t *oldnset;
219 1.1 rmind
220 1.1 rmind oldnset = atomic_swap_ptr(&nat_ruleset, nset);
221 1.1 rmind if (oldnset) {
222 1.1 rmind npf_ruleset_destroy(oldnset);
223 1.1 rmind }
224 1.1 rmind }
225 1.1 rmind
226 1.1 rmind /*
227 1.1 rmind * npf_nat_getport: allocate and return a port in the NAT policy portmap.
228 1.1 rmind *
229 1.1 rmind * => Returns in network byte-order.
230 1.1 rmind * => Zero indicates failure.
231 1.1 rmind */
232 1.1 rmind static in_port_t
233 1.1 rmind npf_nat_getport(npf_natpolicy_t *np)
234 1.1 rmind {
235 1.1 rmind npf_portmap_t *pm = np->n_portmap;
236 1.1 rmind u_int n = PORTMAP_SIZE, idx, bit;
237 1.1 rmind uint32_t map, nmap;
238 1.1 rmind
239 1.1 rmind idx = arc4random() % PORTMAP_SIZE;
240 1.1 rmind for (;;) {
241 1.1 rmind KASSERT(idx < PORTMAP_SIZE);
242 1.1 rmind map = pm->p_bitmap[idx];
243 1.1 rmind if (__predict_false(map == PORTMAP_FILLED)) {
244 1.1 rmind if (n-- == 0) {
245 1.1 rmind /* No space. */
246 1.1 rmind return 0;
247 1.1 rmind }
248 1.1 rmind /* This bitmap is sfilled, next. */
249 1.1 rmind idx = (idx ? idx : PORTMAP_SIZE) - 1;
250 1.1 rmind continue;
251 1.1 rmind }
252 1.1 rmind bit = ffs32(~map) - 1;
253 1.1 rmind nmap = map | (1 << bit);
254 1.1 rmind if (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) == map) {
255 1.1 rmind /* Success. */
256 1.1 rmind break;
257 1.1 rmind }
258 1.1 rmind }
259 1.1 rmind return htons(PORTMAP_FIRST + (idx << PORTMAP_SHIFT) + bit);
260 1.1 rmind }
261 1.1 rmind
262 1.1 rmind /*
263 1.1 rmind * npf_nat_putport: return port as available in the NAT policy portmap.
264 1.1 rmind *
265 1.1 rmind * => Port should be in network byte-order.
266 1.1 rmind */
267 1.1 rmind static void
268 1.1 rmind npf_nat_putport(npf_natpolicy_t *np, in_port_t port)
269 1.1 rmind {
270 1.1 rmind npf_portmap_t *pm = np->n_portmap;
271 1.1 rmind uint32_t map, nmap;
272 1.1 rmind u_int idx, bit;
273 1.1 rmind
274 1.1 rmind port = ntohs(port) - PORTMAP_FIRST;
275 1.1 rmind idx = port >> PORTMAP_SHIFT;
276 1.1 rmind bit = port & PORTMAP_MASK;
277 1.1 rmind do {
278 1.1 rmind map = pm->p_bitmap[idx];
279 1.1 rmind KASSERT(map | (1 << bit));
280 1.1 rmind nmap = map & ~(1 << bit);
281 1.1 rmind } while (atomic_cas_32(&pm->p_bitmap[idx], map, nmap) != map);
282 1.1 rmind }
283 1.1 rmind
284 1.1 rmind /*
285 1.1 rmind * npf_natout:
286 1.1 rmind * - Inspect packet for a NAT policy, unless session with NAT
287 1.1 rmind * association already exists.
288 1.1 rmind * - Perform "forwards" translation: rewrite source address, etc.
289 1.1 rmind * - Establish sessions or if already exists, associate NAT policy.
290 1.1 rmind */
291 1.1 rmind int
292 1.1 rmind npf_natout(npf_cache_t *npc, npf_session_t *se, nbuf_t *nbuf,
293 1.1 rmind struct ifnet *ifp, const int layer)
294 1.1 rmind {
295 1.1 rmind const int proto = npc->npc_proto;
296 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
297 1.1 rmind npf_session_t *nse = NULL; /* XXXgcc */
298 1.1 rmind npf_natpolicy_t *np;
299 1.1 rmind npf_nat_t *nt;
300 1.1 rmind npf_rule_t *rl;
301 1.1 rmind in_addr_t gwip;
302 1.1 rmind in_port_t tport;
303 1.1 rmind int error;
304 1.1 rmind bool new;
305 1.1 rmind
306 1.1 rmind /* All relevant IPv4 data should be already cached. */
307 1.1 rmind if (!npf_iscached(npc, NPC_IP46 | NPC_ADDRS)) {
308 1.1 rmind return 0;
309 1.1 rmind }
310 1.1 rmind
311 1.1 rmind /* Detect if there is a linked session pointing to the NAT entry. */
312 1.1 rmind nt = se ? npf_session_retlinknat(se) : NULL;
313 1.1 rmind if (nt) {
314 1.1 rmind np = nt->nt_natpolicy;
315 1.1 rmind new = false;
316 1.1 rmind goto skip;
317 1.1 rmind }
318 1.1 rmind
319 1.1 rmind /* Inspect packet against NAT ruleset, return a policy. */
320 1.1 rmind rl = npf_ruleset_match(nat_ruleset, npc, nbuf, ifp, PFIL_OUT, layer);
321 1.1 rmind np = rl ? npf_rule_getnat(rl) : NULL;
322 1.1 rmind if (np == NULL) {
323 1.1 rmind /* If packet does not match - done. */
324 1.1 rmind return 0;
325 1.1 rmind }
326 1.1 rmind
327 1.1 rmind /* New NAT association. */
328 1.1 rmind nt = pool_cache_get(nat_cache, PR_NOWAIT);
329 1.1 rmind if (nt == NULL){
330 1.1 rmind return ENOMEM;
331 1.1 rmind }
332 1.1 rmind nt->nt_natpolicy = np;
333 1.1 rmind nt->nt_alg = NULL;
334 1.1 rmind new = true;
335 1.1 rmind
336 1.1 rmind /* Save local (source) address. */
337 1.1 rmind nt->nt_laddr = npc->npc_srcip;
338 1.1 rmind
339 1.1 rmind if (proto == IPPROTO_TCP || proto == IPPROTO_UDP) {
340 1.1 rmind /* Also, save local TCP/UDP port. */
341 1.1 rmind KASSERT(npf_iscached(npc, NPC_PORTS));
342 1.1 rmind nt->nt_lport = npc->npc_sport;
343 1.1 rmind /* Get a new port for translation. */
344 1.1 rmind nt->nt_tport = npf_nat_getport(np);
345 1.1 rmind } else {
346 1.1 rmind nt->nt_lport = 0;
347 1.1 rmind nt->nt_tport = 0;
348 1.1 rmind }
349 1.1 rmind
350 1.1 rmind /* Match any ALGs. */
351 1.1 rmind npf_alg_exec(npc, nbuf, nt, PFIL_OUT);
352 1.1 rmind
353 1.1 rmind /* If there is no local session, establish one before translation. */
354 1.1 rmind if (se == NULL) {
355 1.1 rmind nse = npf_session_establish(npc, NULL, PFIL_OUT);
356 1.1 rmind if (nse == NULL) {
357 1.1 rmind error = ENOMEM;
358 1.1 rmind goto out;
359 1.1 rmind }
360 1.1 rmind se = nse;
361 1.1 rmind } else {
362 1.1 rmind nse = NULL;
363 1.1 rmind }
364 1.1 rmind skip:
365 1.1 rmind if (layer == NPF_LAYER_2 && /* XXX */
366 1.1 rmind (n_ptr = nbuf_advance(&nbuf, n_ptr, npc->npc_elen)) == NULL)
367 1.1 rmind return EINVAL;
368 1.1 rmind
369 1.1 rmind /* Execute ALG hooks first. */
370 1.1 rmind npf_alg_exec(npc, nbuf, nt, PFIL_OUT);
371 1.1 rmind
372 1.1 rmind gwip = np->n_gw_ip;
373 1.1 rmind tport = nt->nt_tport;
374 1.1 rmind
375 1.1 rmind /*
376 1.1 rmind * Perform translation: rewrite source address et al.
377 1.1 rmind * Note: cache may be used in npf_rwrport(), update only in the end.
378 1.1 rmind */
379 1.1 rmind if (!npf_rwrip(npc, nbuf, n_ptr, PFIL_OUT, gwip)) {
380 1.1 rmind error = EINVAL;
381 1.1 rmind goto out;
382 1.1 rmind }
383 1.1 rmind if (proto == IPPROTO_TCP || proto == IPPROTO_UDP) {
384 1.1 rmind KASSERT(tport != 0);
385 1.1 rmind if (!npf_rwrport(npc, nbuf, n_ptr, PFIL_OUT, tport, gwip)) {
386 1.1 rmind error = EINVAL;
387 1.1 rmind goto out;
388 1.1 rmind }
389 1.1 rmind }
390 1.1 rmind /* Success: cache new address and port (if any). */
391 1.1 rmind npc->npc_srcip = gwip;
392 1.1 rmind npc->npc_sport = tport;
393 1.1 rmind error = 0;
394 1.1 rmind
395 1.1 rmind if (__predict_false(new)) {
396 1.1 rmind npf_session_t *natse;
397 1.1 rmind /*
398 1.1 rmind * Establish a new NAT session using translated address and
399 1.1 rmind * associate NAT translation data with this session.
400 1.1 rmind *
401 1.1 rmind * Note: packet now has a translated address in the cache.
402 1.1 rmind */
403 1.1 rmind natse = npf_session_establish(npc, nt, PFIL_OUT);
404 1.1 rmind if (natse == NULL) {
405 1.1 rmind error = ENOMEM;
406 1.1 rmind goto out;
407 1.1 rmind }
408 1.1 rmind /*
409 1.1 rmind * Link local session with NAT session, if no link already.
410 1.1 rmind */
411 1.1 rmind npf_session_link(se, natse);
412 1.1 rmind npf_session_release(natse);
413 1.1 rmind out:
414 1.1 rmind if (error) {
415 1.1 rmind if (nse != NULL) {
416 1.1 rmind /* XXX: expire local session if new? */
417 1.1 rmind }
418 1.1 rmind /* Will free the structure and return the port. */
419 1.1 rmind npf_nat_expire(nt);
420 1.1 rmind }
421 1.1 rmind if (nse != NULL) {
422 1.1 rmind /* Drop the reference local session was new. */
423 1.1 rmind npf_session_release(nse);
424 1.1 rmind }
425 1.1 rmind }
426 1.1 rmind return error;
427 1.1 rmind }
428 1.1 rmind
429 1.1 rmind /*
430 1.1 rmind * npf_natin:
431 1.1 rmind * - Inspect packet for a session with associated NAT policy.
432 1.1 rmind * - Perform "backwards" translation: rewrite destination address, etc.
433 1.1 rmind */
434 1.1 rmind int
435 1.1 rmind npf_natin(npf_cache_t *npc, npf_session_t *se, nbuf_t *nbuf, const int layer)
436 1.1 rmind {
437 1.1 rmind npf_nat_t *nt = se ? npf_session_retnat(se) : NULL;
438 1.1 rmind
439 1.1 rmind if (nt == NULL) {
440 1.1 rmind /* No association - no translation. */
441 1.1 rmind return 0;
442 1.1 rmind }
443 1.1 rmind KASSERT(npf_iscached(npc, NPC_IP46 | NPC_ADDRS));
444 1.1 rmind
445 1.1 rmind void *n_ptr = nbuf_dataptr(nbuf);
446 1.1 rmind in_addr_t laddr = nt->nt_laddr;
447 1.1 rmind in_port_t lport = nt->nt_lport;
448 1.1 rmind
449 1.1 rmind if (layer == NPF_LAYER_2) {
450 1.1 rmind n_ptr = nbuf_advance(&nbuf, n_ptr, npc->npc_elen);
451 1.1 rmind if (n_ptr == NULL) {
452 1.1 rmind return EINVAL;
453 1.1 rmind }
454 1.1 rmind }
455 1.1 rmind
456 1.1 rmind /* Execute ALG hooks first. */
457 1.1 rmind npf_alg_exec(npc, nbuf, nt, PFIL_IN);
458 1.1 rmind
459 1.1 rmind /*
460 1.1 rmind * Address translation: rewrite destination address.
461 1.1 rmind * Note: cache will be used in npf_rwrport(), update only in the end.
462 1.1 rmind */
463 1.1 rmind if (!npf_rwrip(npc, nbuf, n_ptr, PFIL_IN, laddr)) {
464 1.1 rmind return EINVAL;
465 1.1 rmind }
466 1.1 rmind switch (npc->npc_proto) {
467 1.1 rmind case IPPROTO_TCP:
468 1.1 rmind case IPPROTO_UDP:
469 1.1 rmind KASSERT(npf_iscached(npc, NPC_PORTS));
470 1.1 rmind /* Rewrite destination port. */
471 1.1 rmind if (!npf_rwrport(npc, nbuf, n_ptr, PFIL_IN, lport, laddr)) {
472 1.1 rmind return EINVAL;
473 1.1 rmind }
474 1.1 rmind break;
475 1.1 rmind case IPPROTO_ICMP:
476 1.1 rmind /* None. */
477 1.1 rmind break;
478 1.1 rmind default:
479 1.1 rmind return ENOTSUP;
480 1.1 rmind }
481 1.1 rmind /* Cache new address and port. */
482 1.1 rmind npc->npc_dstip = laddr;
483 1.1 rmind npc->npc_dport = lport;
484 1.1 rmind return 0;
485 1.1 rmind }
486 1.1 rmind
487 1.1 rmind /*
488 1.1 rmind * npf_nat_getlocal: return local IP address and port from translation entry.
489 1.1 rmind */
490 1.1 rmind void
491 1.1 rmind npf_nat_getlocal(npf_nat_t *nt, in_addr_t *addr, in_port_t *port)
492 1.1 rmind {
493 1.1 rmind
494 1.1 rmind *addr = nt->nt_laddr;
495 1.1 rmind *port = nt->nt_lport;
496 1.1 rmind }
497 1.1 rmind
498 1.1 rmind void
499 1.1 rmind npf_nat_setalg(npf_nat_t *nt, npf_alg_t *alg, uintptr_t arg)
500 1.1 rmind {
501 1.1 rmind
502 1.1 rmind nt->nt_alg = alg;
503 1.1 rmind nt->nt_alg_arg = arg;
504 1.1 rmind }
505 1.1 rmind
506 1.1 rmind /*
507 1.1 rmind * npf_nat_expire: free NAT-related data structures on session expiration.
508 1.1 rmind */
509 1.1 rmind void
510 1.1 rmind npf_nat_expire(npf_nat_t *nt)
511 1.1 rmind {
512 1.1 rmind
513 1.1 rmind if (nt->nt_tport) {
514 1.1 rmind npf_natpolicy_t *np = nt->nt_natpolicy;
515 1.1 rmind npf_nat_putport(np, nt->nt_tport);
516 1.1 rmind }
517 1.1 rmind pool_cache_put(nat_cache, nt);
518 1.1 rmind }
519 1.1 rmind
520 1.1 rmind #if defined(DDB) || defined(_NPF_TESTING)
521 1.1 rmind
522 1.1 rmind void
523 1.1 rmind npf_nat_dump(npf_nat_t *nt)
524 1.1 rmind {
525 1.1 rmind npf_natpolicy_t *np;
526 1.1 rmind struct in_addr ip;
527 1.1 rmind
528 1.1 rmind if (nt) {
529 1.1 rmind np = nt->nt_natpolicy;
530 1.1 rmind goto skip;
531 1.1 rmind }
532 1.1 rmind LIST_FOREACH(np, &nat_policy_list, n_entry) {
533 1.1 rmind skip:
534 1.1 rmind ip.s_addr = np->n_gw_ip;
535 1.1 rmind printf("\tNAT policy: gw_ip = %s\n", inet_ntoa(ip));
536 1.1 rmind if (nt == NULL) {
537 1.1 rmind continue;
538 1.1 rmind }
539 1.1 rmind ip.s_addr = nt->nt_laddr;
540 1.1 rmind printf("\tNAT: original address %s, lport %d, tport = %d\n",
541 1.1 rmind inet_ntoa(ip), ntohs(nt->nt_lport), ntohs(nt->nt_tport));
542 1.1 rmind if (nt->nt_alg) {
543 1.1 rmind printf("\tNAT ALG = %p, ARG = %p\n",
544 1.1 rmind nt->nt_alg, (void *)nt->nt_alg_arg);
545 1.1 rmind }
546 1.1 rmind return;
547 1.1 rmind }
548 1.1 rmind }
549 1.1 rmind
550 1.1 rmind #endif
551