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/tcp/tcp_packet.h>
24 #include <vnet/ip/format.h>
27 typedef enum { PNAT_NEXT_DROP, PNAT_N_NEXT } pnat_next_t;
29 u8 *format_pnat_match_tuple(u8 *s, va_list *args);
30 u8 *format_pnat_rewrite_tuple(u8 *s, va_list *args);
31 static inline u8 *format_pnat_trace(u8 *s, va_list *args) {
32 CLIB_UNUSED(vlib_main_t * vm) = va_arg(*args, vlib_main_t *);
33 CLIB_UNUSED(vlib_node_t * node) = va_arg(*args, vlib_node_t *);
34 pnat_trace_t *t = va_arg(*args, pnat_trace_t *);
36 s = format(s, "pnat: index %d\n", t->pool_index);
37 if (t->pool_index != ~0) {
38 s = format(s, " match: %U\n", format_pnat_match_tuple,
40 s = format(s, " rewrite: %U", format_pnat_rewrite_tuple,
47 * Given a packet and rewrite instructions from a translation modify packet.
49 // TODO: Generalize to write with mask
50 static u32 pnat_rewrite_ip4(u32 pool_index, ip4_header_t *ip) {
51 pnat_main_t *pm = &pnat_main;
52 if (pool_is_free_index(pm->translations, pool_index))
53 return PNAT_ERROR_REWRITE;
54 pnat_translation_t *t = pool_elt_at_index(pm->translations, pool_index);
58 if (t->instructions & PNAT_INSTR_DESTINATION_ADDRESS) {
59 csumd = ip_csum_sub_even(csumd, ip->dst_address.as_u32);
60 csumd = ip_csum_add_even(csumd, t->post_da.as_u32);
61 ip->dst_address = t->post_da;
63 if (t->instructions & PNAT_INSTR_SOURCE_ADDRESS) {
64 csumd = ip_csum_sub_even(csumd, ip->src_address.as_u32);
65 csumd = ip_csum_add_even(csumd, t->post_sa.as_u32);
66 ip->src_address = t->post_sa;
69 ip_csum_t csum = ip->checksum;
70 csum = ip_csum_sub_even(csum, csumd);
71 ip->checksum = ip_csum_fold(csum);
72 if (ip->checksum == 0xffff)
74 ASSERT(ip->checksum == ip4_header_checksum(ip));
76 u16 plen = clib_net_to_host_u16(ip->length);
78 /* Nothing more to do if this is a fragment. */
79 if (ip4_is_fragment(ip))
80 return PNAT_ERROR_NONE;
83 if (ip->protocol == IP_PROTOCOL_TCP) {
84 /* Assume IP4 header is 20 bytes */
85 if (plen < sizeof(ip4_header_t) + sizeof(tcp_header_t))
86 return PNAT_ERROR_TOOSHORT;
88 tcp_header_t *tcp = ip4_next_header(ip);
89 ip_csum_t l4csum = tcp->checksum;
90 if (t->instructions & PNAT_INSTR_DESTINATION_PORT) {
91 l4csum = ip_csum_sub_even(l4csum, tcp->dst_port);
92 l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_dp));
93 tcp->dst_port = clib_net_to_host_u16(t->post_dp);
95 if (t->instructions & PNAT_INSTR_SOURCE_PORT) {
96 l4csum = ip_csum_sub_even(l4csum, tcp->src_port);
97 l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_sp));
98 tcp->src_port = clib_net_to_host_u16(t->post_sp);
100 l4csum = ip_csum_sub_even(l4csum, csumd);
101 tcp->checksum = ip_csum_fold(l4csum);
102 } else if (ip->protocol == IP_PROTOCOL_UDP) {
103 if (plen < sizeof(ip4_header_t) + sizeof(udp_header_t))
104 return PNAT_ERROR_TOOSHORT;
105 udp_header_t *udp = ip4_next_header(ip);
106 ip_csum_t l4csum = udp->checksum;
107 if (t->instructions & PNAT_INSTR_DESTINATION_PORT) {
108 l4csum = ip_csum_sub_even(l4csum, udp->dst_port);
109 l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_dp));
110 udp->dst_port = clib_net_to_host_u16(t->post_dp);
112 if (t->instructions & PNAT_INSTR_SOURCE_PORT) {
113 l4csum = ip_csum_sub_even(l4csum, udp->src_port);
114 l4csum = ip_csum_add_even(l4csum, clib_net_to_host_u16(t->post_sp));
115 udp->src_port = clib_net_to_host_u16(t->post_sp);
118 l4csum = ip_csum_sub_even(l4csum, csumd);
119 udp->checksum = ip_csum_fold(l4csum);
122 if (t->instructions & PNAT_INSTR_COPY_BYTE) {
123 /* Copy byte from somewhere in packet to elsewhere */
125 if (t->to_offset >= plen || t->from_offset > plen) {
126 return PNAT_ERROR_TOOSHORT;
129 p[t->to_offset] = p[t->from_offset];
130 ip->checksum = ip4_header_checksum(ip);
133 if (t->instructions & PNAT_INSTR_CLEAR_BYTE) {
134 /* Clear byte at offset */
136 p[t->clear_offset] = 0;
137 ip->checksum = ip4_header_checksum(ip);
141 return PNAT_ERROR_NONE;
145 * Lookup the packet tuple in the flow cache, given the lookup mask.
146 * If a binding is found, rewrite the packet according to instructions,
147 * otherwise follow configured default action (forward, punt or drop)
149 // TODO: Make use of SVR configurable
150 static_always_inline uword pnat_node_inline(vlib_main_t *vm,
151 vlib_node_runtime_t *node,
153 pnat_attachment_point_t attachment,
155 pnat_main_t *pm = &pnat_main;
156 u32 n_left_from, *from;
157 u16 nexts[VLIB_FRAME_SIZE] = {0}, *next = nexts;
158 u32 pool_indicies[VLIB_FRAME_SIZE], *pi = pool_indicies;
159 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
160 clib_bihash_kv_16_8_t kv, value;
163 from = vlib_frame_vector_args(frame);
164 n_left_from = frame->n_vectors;
165 vlib_get_buffers(vm, from, b, n_left_from);
166 pnat_interface_t *interface;
168 /* Stage 1: build vector of flow hash (based on lookup mask) */
169 while (n_left_from > 0) {
170 u32 sw_if_index0 = vnet_buffer(b[0])->sw_if_index[dir];
171 u16 sport0 = vnet_buffer(b[0])->ip.reass.l4_src_port;
172 u16 dport0 = vnet_buffer(b[0])->ip.reass.l4_dst_port;
174 dir == VLIB_TX ? vnet_buffer(b[0])->ip.save_rewrite_length : 0;
175 ip0 = (ip4_header_t *)(vlib_buffer_get_current(b[0]) + iph_offset);
176 interface = pnat_interface_by_sw_if_index(sw_if_index0);
178 pnat_mask_fast_t mask = interface->lookup_mask_fast[attachment];
179 pnat_calc_key(sw_if_index0, attachment, ip0->src_address,
180 ip0->dst_address, ip0->protocol, sport0, dport0, mask,
182 /* By default pass packet to next node in the feature chain */
183 vnet_feature_next_u16(next, b[0]);
185 if (clib_bihash_search_16_8(&pm->flowhash, &kv, &value) == 0) {
188 u32 errno0 = pnat_rewrite_ip4(value.value, ip0);
189 if (PREDICT_FALSE(errno0)) {
190 next[0] = PNAT_NEXT_DROP;
191 b[0]->error = node->errors[errno0];
206 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE))) {
210 for (i = 0; i < frame->n_vectors; i++) {
211 if (b[0]->flags & VLIB_BUFFER_IS_TRACED) {
212 pnat_trace_t *t = vlib_add_trace(vm, node, b[0], sizeof(*t));
214 if (!pool_is_free_index(pm->translations, *pi)) {
215 pnat_translation_t *tr =
216 pool_elt_at_index(pm->translations, *pi);
217 t->match = tr->match;
218 t->rewrite = tr->rewrite;
229 vlib_buffer_enqueue_to_next(vm, node, from, nexts, frame->n_vectors);
231 return frame->n_vectors;