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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_pnat_node_h
17 #define included_pnat_node_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>
26 typedef enum { PNAT_NEXT_DROP, PNAT_N_NEXT } pnat_next_t;
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 *);
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);
44 * Given a packet and rewrite instructions from a translation modify packet.
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);
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;
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;
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));
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);
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);
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);
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);
100 l4csum = ip_csum_sub_even(l4csum, csumd);
101 udp->checksum = ip_csum_fold(l4csum);
104 return PNAT_ERROR_NONE;
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)
112 static_always_inline uword pnat_node_inline(vlib_main_t *vm,
113 vlib_node_runtime_t *node,
115 pnat_attachment_point_t attachment,
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;
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;
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;
136 dir == VLIB_TX ? vnet_buffer(b[0])->ip.save_rewrite_length : 0;
137 ip0 = (ip4_header_t *)(vlib_buffer_get_current(b[0]) + iph_offset);
138 interface = pnat_interface_by_sw_if_index(sw_if_index0);
140 pnat_mask_fast_t mask = interface->lookup_mask_fast[attachment];
141 pnat_calc_key(sw_if_index0, attachment, ip0->src_address,
142 ip0->dst_address, ip0->protocol, sport0, dport0, mask,
144 /* By default pass packet to next node in the feature chain */
145 vnet_feature_next_u16(next, b[0]);
147 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
150 u32 errno0 = pnat_rewrite_ip4(value.value, ip0);
151 if (PREDICT_FALSE(errno0)) {
152 next[0] = PNAT_NEXT_DROP;
153 b[0]->error = node->errors[errno0];
168 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) {
172 for (i = 0; i < frame->n_vectors; i++) {
173 if (b[0]->flags & VLIB_BUFFER_IS_TRACED) {
174 pnat_trace_t *t = vlib_add_trace(vm, node, b[0], sizeof(*t));
176 if (!pool_is_free_index(pm->translations, *pi)) {
177 pnat_translation_t *tr =
178 pool_elt_at_index(pm->translations, *pi);
179 t->match = tr->match;
180 t->rewrite = tr->rewrite;
191 vlib_buffer_enqueue_to_next(vm, node, from, nexts, frame->n_vectors);
193 return frame->n_vectors;