nat: 1:1 policy NAT
[vpp.git] / src / plugins / nat / pnat / pnat.h
1 /*
2  * Copyright (c) 2021 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef included_pnat_h
17 #define included_pnat_h
18
19 #include <stdbool.h>
20 #include <vnet/ip/ip4_packet.h>
21 #include <vppinfra/bihash_16_8.h>
22
23 #define PNAT_FLOW_HASH_BUCKETS 256
24
25 /* Definitions from pnat.api */
26 #include <pnat/pnat.api_types.h>
27 typedef vl_api_pnat_5tuple_t pnat_5tuple_t;
28 typedef vl_api_pnat_mask_t pnat_mask_t;
29 typedef vl_api_pnat_attachment_point_t pnat_attachment_point_t;
30
31 /* Rewrite instructions */
32 typedef enum {
33     PNAT_INSTR_NONE = 1 << 0,
34     PNAT_INSTR_SOURCE_ADDRESS = 1 << 1,
35     PNAT_INSTR_SOURCE_PORT = 1 << 2,
36     PNAT_INSTR_DESTINATION_ADDRESS = 1 << 3,
37     PNAT_INSTR_DESTINATION_PORT = 1 << 4,
38 } pnat_instructions_t;
39
40 typedef struct {
41     u64 as_u64[2];
42 } pnat_mask_fast_t;
43
44 /* Session cache entries */
45 typedef struct {
46     /* What to translate to */
47     pnat_instructions_t instructions;
48
49     /* Stored in network byte order */
50     ip4_address_t post_sa;
51     ip4_address_t post_da;
52     u16 post_sp;
53     u16 post_dp;
54
55     /* Used for trace/show commands */
56     pnat_5tuple_t match;
57     pnat_5tuple_t rewrite;
58 } pnat_translation_t;
59
60 /* Interface object */
61 typedef struct {
62     u32 sw_if_index;
63     pnat_mask_t lookup_mask[PNAT_ATTACHMENT_POINT_MAX];
64     pnat_mask_fast_t lookup_mask_fast[PNAT_ATTACHMENT_POINT_MAX];
65
66     /* Feature chain enabled on interface */
67     bool enabled[PNAT_ATTACHMENT_POINT_MAX];
68
69     u32 refcount;
70 } pnat_interface_t;
71
72 /* Globals */
73 typedef struct {
74     bool enabled;
75
76     clib_bihash_16_8_t flowhash; /* Bi-directional */
77
78     /* Interface pool */
79     pnat_interface_t *interfaces;
80     u32 *interface_by_sw_if_index;
81
82     /* Translations pool */
83     pnat_translation_t *translations;
84
85     u16 msg_id_base;
86 } pnat_main_t;
87 extern pnat_main_t pnat_main;
88
89 pnat_interface_t *pnat_interface_by_sw_if_index(u32 sw_if_index);
90
91 /* Packet trace information */
92 typedef struct {
93     u32 pool_index;
94     pnat_5tuple_t match;
95     pnat_5tuple_t rewrite;
96 } pnat_trace_t;
97
98 int pnat_binding_add(pnat_5tuple_t *match, pnat_5tuple_t *rewrite,
99                      u32 *binding_index);
100 int pnat_binding_del(u32 binding_index);
101 int pnat_binding_attach(u32 sw_if_index, pnat_attachment_point_t attachment,
102                         u32 binding_index);
103 int pnat_binding_detach(u32 sw_if_index, pnat_attachment_point_t attachment,
104                         u32 binding_index);
105 u32 pnat_flow_lookup(u32 sw_if_index, pnat_attachment_point_t attachment,
106                      pnat_5tuple_t *match);
107
108 static inline void
109 pnat_calc_key(u32 sw_if_index, pnat_attachment_point_t attachment,
110               ip4_address_t src, ip4_address_t dst, u8 protocol, u16 sport,
111               u16 dport, pnat_mask_fast_t mask, clib_bihash_kv_16_8_t *kv) {
112     kv->key[0] = kv->key[1] = 0;
113     kv->key[0] = (u64)src.as_u32 << 32 | dst.as_u32;
114     kv->key[0] &= mask.as_u64[0];
115     kv->key[1] |=
116         (u64)protocol << 56 | (u64)sw_if_index << 36 | (u64)attachment << 32;
117     kv->key[1] |= sport << 16 | dport;
118     kv->key[1] &= mask.as_u64[1];
119 }
120
121 #endif