2 * det44.h - deterministic NAT definitions
4 * Copyright (c) 2020 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * @brief Deterministic NAT (CGN) inlines
23 #ifndef __included_det44_inlines_h__
24 #define __included_det44_inlines_h__
26 static_always_inline int
27 det44_is_interface_addr (vlib_node_runtime_t * node,
28 u32 sw_if_index0, u32 ip4_addr)
30 det44_runtime_t *rt = (det44_runtime_t *) node->runtime_data;
31 det44_main_t *dm = &det44_main;
32 ip4_address_t *first_int_addr;
34 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
36 first_int_addr = ip4_interface_first_address (dm->ip4_main,
38 rt->cached_sw_if_index = sw_if_index0;
40 rt->cached_ip4_address = first_int_addr->as_u32;
42 rt->cached_ip4_address = 0;
44 if (PREDICT_FALSE (rt->cached_ip4_address == ip4_addr))
50 * @brief Check if packet should be translated
52 * Packets aimed at outside interface and external address with active session
53 * should be translated.
55 * @param node NAT runtime data
56 * @param sw_if_index0 index of the inside interface
57 * @param ip0 IPv4 header
58 * @param proto0 NAT protocol
59 * @param rx_fib_index0 RX FIB index
61 * @returns 0 if packet should be translated otherwise 1
63 static_always_inline int
64 det44_translate (vlib_node_runtime_t * node, u32 sw_if_index0,
65 ip4_header_t * ip0, u32 proto0, u32 rx_fib_index0)
67 det44_main_t *dm = &det44_main;
68 fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
69 det44_fib_t *outside_fib;
71 .fp_proto = FIB_PROTOCOL_IP4,
74 .ip4.as_u32 = ip0->dst_address.as_u32,
79 /* Don't NAT packet aimed at the interface address */
80 if (PREDICT_FALSE (!det44_is_interface_addr (node, sw_if_index0,
81 ip0->dst_address.as_u32)))
86 /* find out if there is outside feature enabled for this destination */
87 fei = fib_table_lookup (rx_fib_index0, &pfx);
88 if (FIB_NODE_INDEX_INVALID != fei)
90 u32 sw_if_index = fib_entry_get_resolving_interface (fei);
91 if (sw_if_index == ~0)
93 // TODO: go over use cases
95 vec_foreach (outside_fib, dm->outside_fibs)
97 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
98 if (FIB_NODE_INDEX_INVALID != fei)
100 sw_if_index = fib_entry_get_resolving_interface (fei);
101 if (sw_if_index != ~0)
107 if (sw_if_index != ~0)
109 det44_interface_t *i;
111 pool_foreach (i, dm->interfaces, ({
112 /* NAT packet aimed at outside interface */
113 if ((det44_interface_is_outside (i)) && (sw_if_index == i->sw_if_index))
122 #endif /* __included_det44_inlines_h__ */
125 * fd.io coding-style-patch-verification: ON
128 * eval: (c-set-style "gnu")