FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
det44_inlines.h
Go to the documentation of this file.
1 /*
2  * det44.h - deterministic NAT definitions
3  *
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:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 /**
19  * @file
20  * @brief Deterministic NAT (CGN) inlines
21  */
22 
23 #ifndef __included_det44_inlines_h__
24 #define __included_det44_inlines_h__
25 
28  u32 sw_if_index0, u32 ip4_addr)
29 {
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;
33 
34  if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
35  {
36  first_int_addr = ip4_interface_first_address (dm->ip4_main,
37  sw_if_index0, 0);
38  rt->cached_sw_if_index = sw_if_index0;
39  if (first_int_addr)
40  rt->cached_ip4_address = first_int_addr->as_u32;
41  else
42  rt->cached_ip4_address = 0;
43  }
44  if (PREDICT_FALSE (rt->cached_ip4_address == ip4_addr))
45  return 0;
46  return 1;
47 }
48 
49 /**
50  * @brief Check if packet should be translated
51  *
52  * Packets aimed at outside interface and external address with active session
53  * should be translated.
54  *
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
60  *
61  * @returns 0 if packet should be translated otherwise 1
62  */
65  ip4_header_t * ip0, u32 proto0, u32 rx_fib_index0)
66 {
67  det44_main_t *dm = &det44_main;
69  det44_fib_t *outside_fib;
70  fib_prefix_t pfx = {
72  .fp_len = 32,
73  .fp_addr = {
74  .ip4.as_u32 = ip0->dst_address.as_u32,
75  }
76  ,
77  };
78 
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)))
82  {
83  return 1;
84  }
85 
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)
89  {
91  if (sw_if_index == ~0)
92  {
93  // TODO: go over use cases
94  /* *INDENT-OFF* */
95  vec_foreach (outside_fib, dm->outside_fibs)
96  {
97  fei = fib_table_lookup (outside_fib->fib_index, &pfx);
98  if (FIB_NODE_INDEX_INVALID != fei)
99  {
101  if (sw_if_index != ~0)
102  break;
103  }
104  }
105  /* *INDENT-ON* */
106  }
107  if (sw_if_index != ~0)
108  {
110  /* *INDENT-OFF* */
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))
114  return 0;
115  }
116  /* *INDENT-ON* */
117  }
118  }
119  return 1;
120 }
121 
122 #endif /* __included_det44_inlines_h__ */
123 
124 /*
125  * fd.io coding-style-patch-verification: ON
126  *
127  * Local Variables:
128  * eval: (c-set-style "gnu")
129  * End:
130  */
det44_runtime_t
Definition: det44.h:77
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
ip4_address_t::as_u32
u32 as_u32
Definition: ip4_packet.h:57
FIB_NODE_INDEX_INVALID
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
det44_main_s::interfaces
det44_interface_t * interfaces
Definition: det44.h:182
fib_table_lookup
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:68
ip4_header_t
Definition: ip4_packet.h:87
det44_interface_t
Definition: det44.h:128
det44_fib_t
Definition: det44.h:140
det44_is_interface_addr
static_always_inline int det44_is_interface_addr(vlib_node_runtime_t *node, u32 sw_if_index0, u32 ip4_addr)
Definition: det44_inlines.h:27
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
det44_main
det44_main_t det44_main
Definition: det44.c:30
det44_main_s::outside_fibs
det44_fib_t * outside_fibs
Definition: det44.h:154
static_always_inline
#define static_always_inline
Definition: clib.h:112
fib_node_index_t
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
if
if(node->flags &VLIB_NODE_FLAG_TRACE) vnet_interface_output_trace(vm
ip4_address_t
Definition: ip4_packet.h:50
det44_translate
static_always_inline int det44_translate(vlib_node_runtime_t *node, u32 sw_if_index0, ip4_header_t *ip0, u32 proto0, u32 rx_fib_index0)
Check if packet should be translated.
Definition: det44_inlines.h:64
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
fib_entry_get_resolving_interface
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1474
ip4_header_t::dst_address
ip4_address_t dst_address
Definition: ip4_packet.h:125
det44_main_s
Definition: det44.h:146
u32
unsigned int u32
Definition: types.h:88
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
fib_prefix_t_::fp_proto
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
rt
vnet_interface_output_runtime_t * rt
Definition: interface_output.c:419
i
int i
Definition: flowhash_template.h:376
det44_interface_is_outside
#define det44_interface_is_outside(i)
Check if Deterministic NAT interface is outside.
Definition: det44.h:219
det44_fib_t::fib_index
u32 fib_index
Definition: det44.h:142
ip4_interface_first_address
ip4_address_t * ip4_interface_first_address(ip4_main_t *im, u32 sw_if_index, ip_interface_address_t **result_ia)
Definition: ip4_forward.c:282
vlib_node_runtime_t
Definition: node.h:454
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
fib_prefix_t_
Aggregate type for a prefix.
Definition: fib_types.h:202