1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright(c) 2023 Cisco Systems, Inc.
4 // This file contains the implementation of the NPT66 node.
5 // RFC6296: IPv6-to-IPv6 Network Prefix Translation (NPTv6)
7 #include <vnet/ip/ip.h>
8 #include <vnet/ip/ip6.h>
9 #include <vnet/ip/ip6_packet.h>
11 #include <npt66/npt66.h>
16 ip6_address_t internal;
17 ip6_address_t external;
21 format_npt66_trace (u8 *s, va_list *args)
23 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
24 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
25 npt66_trace_t *t = va_arg (*args, npt66_trace_t *);
27 if (t->pool_index != ~0)
28 s = format (s, "npt66: index %d internal: %U external: %U\n",
29 t->pool_index, format_ip6_address, &t->internal,
30 format_ip6_address, &t->external);
32 s = format (s, "npt66: index %d (binding not found)\n", t->pool_index);
36 /* NPT66 next-nodes */
44 ip6_prefix_copy (ip6_address_t dest, ip6_address_t src, int plen)
46 int bytes_to_copy = plen / 8;
47 int residual_bits = plen % 8;
50 for (int i = 0; i < bytes_to_copy; i++)
52 dest.as_u8[i] = src.as_u8[i];
55 // Handle the residual bits, if any
58 uint8_t mask = 0xFF << (8 - residual_bits);
59 dest.as_u8[bytes_to_copy] = (dest.as_u8[bytes_to_copy] & ~mask) |
60 (src.as_u8[bytes_to_copy] & mask);
65 ip6_prefix_cmp (ip6_address_t a, ip6_address_t b, int plen)
67 int bytes_to_compare = plen / 8;
68 int residual_bits = plen % 8;
71 for (int i = 0; i < bytes_to_compare; i++)
73 if (a.as_u8[i] != b.as_u8[i])
75 return 0; // prefixes are not identical
79 // Compare the residual bits, if any
82 uint8_t mask = 0xFF << (8 - residual_bits);
83 if ((a.as_u8[bytes_to_compare] & mask) !=
84 (b.as_u8[bytes_to_compare] & mask))
86 return 0; // prefixes are not identical
89 return 1; // prefixes are identical
93 npt66_adjust_checksum (int plen, bool add, ip_csum_t delta,
94 ip6_address_t *address)
98 // TODO: Check for 0xFFFF
99 if (address->as_u16[3] == 0xffff)
101 address->as_u16[3] = add ? ip_csum_add_even (address->as_u16[3], delta) :
102 ip_csum_sub_even (address->as_u16[3], delta);
106 /* For prefixes longer than 48 find a 16-bit word in the interface id */
107 for (int i = 4; i < 8; i++)
109 if (address->as_u16[i] == 0xffff)
111 address->as_u16[i] = add ?
112 ip_csum_add_even (address->as_u16[i], delta) :
113 ip_csum_sub_even (address->as_u16[i], delta);
121 npt66_translate (ip6_header_t *ip, npt66_binding_t *binding, int dir)
126 if (!ip6_prefix_cmp (ip->src_address, binding->internal,
127 binding->internal_plen))
129 clib_warning ("npt66_translate: src address is not internal");
132 ip->src_address = ip6_prefix_copy (ip->src_address, binding->external,
133 binding->external_plen);
134 /* Checksum neutrality */
135 rv = npt66_adjust_checksum (binding->internal_plen, false,
136 binding->delta, &ip->src_address);
140 if (!ip6_prefix_cmp (ip->dst_address, binding->external,
141 binding->external_plen))
143 clib_warning ("npt66_translate: dst address is not external");
146 ip->dst_address = ip6_prefix_copy (ip->dst_address, binding->internal,
147 binding->internal_plen);
148 rv = npt66_adjust_checksum (binding->internal_plen, true, binding->delta,
156 * Lookup the packet tuple in the flow cache, given the lookup mask.
157 * If a binding is found, rewrite the packet according to instructions,
158 * otherwise follow configured default action (forward, punt or drop)
160 // TODO: Make use of SVR configurable
161 static_always_inline uword
162 npt66_node_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
163 vlib_frame_t *frame, int dir)
165 npt66_main_t *nm = &npt66_main;
166 u32 n_left_from, *from;
167 u16 nexts[VLIB_FRAME_SIZE] = { 0 }, *next = nexts;
168 u32 pool_indicies[VLIB_FRAME_SIZE], *pi = pool_indicies;
169 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
172 from = vlib_frame_vector_args (frame);
173 n_left_from = frame->n_vectors;
174 vlib_get_buffers (vm, from, b, n_left_from);
175 npt66_binding_t *binding;
177 /* Stage 1: build vector of flow hash (based on lookup mask) */
178 while (n_left_from > 0)
180 clib_warning ("DIRECTION: %u", dir);
181 u32 sw_if_index = vnet_buffer (b[0])->sw_if_index[dir];
183 dir == VLIB_TX ? vnet_buffer (b[0])->ip.save_rewrite_length : 0;
184 ip = (ip6_header_t *) (vlib_buffer_get_current (b[0]) + iph_offset);
185 binding = npt66_interface_by_sw_if_index (sw_if_index);
187 *pi = binding - nm->bindings;
189 /* By default pass packet to next node in the feature chain */
190 vnet_feature_next_u16 (next, b[0]);
192 int rv = npt66_translate (ip, binding, dir);
195 clib_warning ("npt66_translate failed");
196 *next = NPT66_NEXT_DROP;
207 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
213 for (i = 0; i < frame->n_vectors; i++)
215 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
217 npt66_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
220 if (!pool_is_free_index (nm->bindings, *pi))
222 npt66_binding_t *tr =
223 pool_elt_at_index (nm->bindings, *pi);
224 t->internal = tr->internal;
225 t->external = tr->external;
237 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
239 return frame->n_vectors;
242 VLIB_NODE_FN (npt66_input_node)
243 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
245 return npt66_node_inline (vm, node, frame, VLIB_RX);
247 VLIB_NODE_FN (npt66_output_node)
248 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
250 return npt66_node_inline (vm, node, frame, VLIB_TX);
253 VLIB_REGISTER_NODE(npt66_input_node) = {
254 .name = "npt66-input",
255 .vector_size = sizeof(u32),
256 .format_trace = format_npt66_trace,
257 .type = VLIB_NODE_TYPE_INTERNAL,
258 // .n_errors = NPT66_N_ERROR,
259 // .error_counters = npt66_error_counters,
260 .n_next_nodes = NPT66_N_NEXT,
263 [NPT66_NEXT_DROP] = "error-drop",
267 VLIB_REGISTER_NODE (npt66_output_node) = {
268 .name = "npt66-output",
269 .vector_size = sizeof (u32),
270 .format_trace = format_npt66_trace,
271 .type = VLIB_NODE_TYPE_INTERNAL,
272 // .n_errors = npt66_N_ERROR,
273 // .error_counters = npt66_error_counters,
274 .sibling_of = "npt66-input",
277 /* Hook up features */
278 VNET_FEATURE_INIT (npt66_input, static) = {
279 .arc_name = "ip6-unicast",
280 .node_name = "npt66-input",
281 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-feature"),
283 VNET_FEATURE_INIT (npt66_output, static) = {
284 .arc_name = "ip6-output",
285 .node_name = "npt66-output",
286 .runs_after = VNET_FEATURES ("ip4-sv-reassembly-output-feature"),