FD.io VPP  v20.09-64-g4f7b92f0a
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 {
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  {
100  sw_if_index = fib_entry_get_resolving_interface (fei);
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  */
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:212
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
u8 runtime_data[0]
Function dependent node-runtime data.
Definition: node.h:518
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:281
det44_fib_t * outside_fibs
Definition: det44.h:169
#define static_always_inline
Definition: clib.h:108
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
ip4_address_t dst_address
Definition: ip4_packet.h:125
Aggregate type for a prefix.
Definition: fib_types.h:203
unsigned int u32
Definition: types.h:88
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
u32 fib_index
Definition: det44.h:157
#define PREDICT_FALSE(x)
Definition: clib.h:120
det44_main_t det44_main
Definition: det44.c:30
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1458
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:30
u32 cached_sw_if_index
Definition: det44.h:78
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
u32 cached_ip4_address
Definition: det44.h:79
det44_interface_t * interfaces
Definition: det44.h:197
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:31
#define vec_foreach(var, vec)
Vector iterator.
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
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33