nat: 1:1 policy NAT
[vpp.git] / src / plugins / nat / pnat / pnat_node.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_node_h
17 #define included_pnat_node_h
18
19 #include "pnat.h"
20 #include <pnat/pnat.api_enum.h>
21 #include <vnet/feature/feature.h>
22 #include <vnet/udp/udp_packet.h>
23 #include <vnet/ip/format.h>
24
25 /* PNAT next-nodes */
26 typedef enum { PNAT_NEXT_DROP, PNAT_N_NEXT } pnat_next_t;
27
28 // u8 *format_pnat_key(u8 *s, va_list *args);
29 u8 *format_pnat_5tuple(u8 *s, va_list *args);
30 static inline u8 *format_pnat_trace(u8 *s, va_list *args) {
31     CLIB_UNUSED(vlib_main_t * vm) = va_arg(*args, vlib_main_t *);
32     CLIB_UNUSED(vlib_node_t * node) = va_arg(*args, vlib_node_t *);
33     pnat_trace_t *t = va_arg(*args, pnat_trace_t *);
34
35     s = format(s, "pnat: index %d\n", t->pool_index);
36     if (t->pool_index != ~0) {
37         s = format(s, "        match: %U\n", format_pnat_5tuple, &t->match);
38         s = format(s, "        rewrite: %U", format_pnat_5tuple, &t->rewrite);
39     }
40     return s;
41 }
42
43 /*
44  * Given a packet and rewrite instructions from a translation modify packet.
45  */
46 static u32 pnat_rewrite_ip4(u32 pool_index, ip4_header_t *ip) {
47     pnat_main_t *pm = &pnat_main;
48     if (pool_is_free_index(pm->translations, pool_index))
49         return PNAT_ERROR_REWRITE;
50     pnat_translation_t *t = pool_elt_at_index(pm->translations, pool_index);
51
52     ip_csum_t csumd = 0;
53
54     if (t->instructions & PNAT_INSTR_DESTINATION_ADDRESS) {
55         csumd = ip_csum_sub_even(csumd, ip->dst_address.as_u32);
56         csumd = ip_csum_add_even(csumd, t->post_da.as_u32);
57         ip->dst_address = t->post_da;
58     }
59     if (t->instructions & PNAT_INSTR_SOURCE_ADDRESS) {
60         csumd = ip_csum_sub_even(csumd, ip->src_address.as_u32);
61         csumd = ip_csum_add_even(csumd, t->post_sa.as_u32);
62         ip->src_address = t->post_sa;
63     }
64
65     ip_csum_t csum = ip->checksum;
66     csum = ip_csum_sub_even(csum, csumd);
67     ip->checksum = ip_csum_fold(csum);
68     ASSERT(ip->checksum == ip4_header_checksum(ip));
69
70     /* L4 ports */
71     if (ip->protocol == IP_PROTOCOL_TCP) {
72         tcp_header_t *tcp = ip4_next_header(ip);
73         ip_csum_t l4csum = tcp->checksum;
74         if (t->instructions & PNAT_INSTR_DESTINATION_PORT) {
75             l4csum = ip_csum_sub_even(l4csum, tcp->dst_port);
76             l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_dp));
77             tcp->dst_port = clib_net_to_host_u16(t->post_dp);
78         }
79         if (t->instructions & PNAT_INSTR_SOURCE_PORT) {
80             l4csum = ip_csum_sub_even(l4csum, tcp->src_port);
81             l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_sp));
82             tcp->src_port = clib_net_to_host_u16(t->post_sp);
83         }
84         l4csum = ip_csum_sub_even(l4csum, csumd);
85         tcp->checksum = ip_csum_fold(l4csum);
86     } else if (ip->protocol == IP_PROTOCOL_UDP) {
87         udp_header_t *udp = ip4_next_header(ip);
88         ip_csum_t l4csum = udp->checksum;
89         if (t->instructions & PNAT_INSTR_DESTINATION_PORT) {
90             l4csum = ip_csum_sub_even(l4csum, udp->dst_port);
91             l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_dp));
92             udp->dst_port = clib_net_to_host_u16(t->post_dp);
93         }
94         if (t->instructions & PNAT_INSTR_SOURCE_PORT) {
95             l4csum = ip_csum_sub_even(l4csum, udp->src_port);
96             l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_sp));
97             udp->src_port = clib_net_to_host_u16(t->post_sp);
98         }
99         if (udp->checksum) {
100             l4csum = ip_csum_sub_even(l4csum, csumd);
101             udp->checksum = ip_csum_fold(l4csum);
102         }
103     }
104     return PNAT_ERROR_NONE;
105 }
106
107 /*
108  * Lookup the packet tuple in the flow cache, given the lookup mask.
109  * If a binding is found, rewrite the packet according to instructions,
110  * otherwise follow configured default action (forward, punt or drop)
111  */
112 static_always_inline uword pnat_node_inline(vlib_main_t *vm,
113                                             vlib_node_runtime_t *node,
114                                             vlib_frame_t *frame,
115                                             pnat_attachment_point_t attachment,
116                                             int dir) {
117     pnat_main_t *pm = &pnat_main;
118     u32 n_left_from, *from;
119     u16 nexts[VLIB_FRAME_SIZE] = {0}, *next = nexts;
120     u32 pool_indicies[VLIB_FRAME_SIZE], *pi = pool_indicies;
121     vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
122     clib_bihash_kv_16_8_t kv, value;
123     ip4_header_t *ip0;
124
125     from = vlib_frame_vector_args(frame);
126     n_left_from = frame->n_vectors;
127     vlib_get_buffers(vm, from, b, n_left_from);
128     pnat_interface_t *interface;
129
130     /* Stage 1: build vector of flow hash (based on lookup mask) */
131     while (n_left_from > 0) {
132         u32 sw_if_index0 = vnet_buffer(b[0])->sw_if_index[dir];
133         u16 sport0 = vnet_buffer(b[0])->ip.reass.l4_src_port;
134         u16 dport0 = vnet_buffer(b[0])->ip.reass.l4_dst_port;
135         u32 iph_offset = vnet_buffer(b[0])->ip.reass.save_rewrite_length;
136         ip0 = (ip4_header_t *)(vlib_buffer_get_current(b[0]) + iph_offset);
137         interface = pnat_interface_by_sw_if_index(sw_if_index0);
138         ASSERT(interface);
139         pnat_mask_fast_t mask = interface->lookup_mask_fast[attachment];
140         pnat_calc_key(sw_if_index0, attachment, ip0->src_address,
141                       ip0->dst_address, ip0->protocol, sport0, dport0, mask,
142                       &kv);
143         /* By default pass packet to next node in the feature chain */
144         vnet_feature_next_u16(next, b[0]);
145
146         if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
147             /* Cache hit */
148             *pi = value.value;
149             u32 iph_offset = vnet_buffer(b[0])->ip.reass.save_rewrite_length;
150             ip0 = (ip4_header_t *)(vlib_buffer_get_current(b[0]) + iph_offset);
151             u32 errno0 = pnat_rewrite_ip4(value.value, ip0);
152             if (PREDICT_FALSE(errno0)) {
153                 next[0] = PNAT_NEXT_DROP;
154                 b[0]->error = node->errors[errno0];
155             }
156         } else {
157             /* Cache miss */
158             *pi = ~0;
159         }
160         next += 1;
161
162         /*next: */
163         n_left_from -= 1;
164         b += 1;
165         pi += 1;
166     }
167
168     /* Packet trace */
169     if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) {
170         u32 i;
171         b = bufs;
172         pi = pool_indicies;
173         for (i = 0; i < frame->n_vectors; i++) {
174             if (b[0]->flags & VLIB_BUFFER_IS_TRACED) {
175                 pnat_trace_t *t = vlib_add_trace(vm, node, b[0], sizeof(*t));
176                 if (*pi != ~0) {
177                     if (!pool_is_free_index(pm->translations, *pi)) {
178                         pnat_translation_t *tr =
179                             pool_elt_at_index(pm->translations, *pi);
180                         t->match = tr->match;
181                         t->rewrite = tr->rewrite;
182                     }
183                 }
184                 t->pool_index = *pi;
185                 b += 1;
186                 pi += 1;
187             } else
188                 break;
189         }
190     }
191
192     vlib_buffer_enqueue_to_next(vm, node, from, nexts, frame->n_vectors);
193
194     return frame->n_vectors;
195 }
196 #endif