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 #include <vnet/plugin/plugin.h>
17 #include <vpp/app/version.h>
20 #include <vnet/tcp/tcp_types.h>
22 char *hsi_error_strings[] = {
23 #define hsi_error(n, s) s,
24 #include <hsi/hsi_error.def>
28 typedef enum hsi_input_next_
30 HSI_INPUT_NEXT_UDP_INPUT,
31 HSI_INPUT_NEXT_TCP_INPUT,
32 HSI_INPUT_NEXT_TCP_INPUT_NOLOOKUP,
36 #define foreach_hsi4_input_next \
37 _ (UDP_INPUT, "udp4-input") \
38 _ (TCP_INPUT, "tcp4-input") \
39 _ (TCP_INPUT_NOLOOKUP, "tcp4-input-nolookup")
41 #define foreach_hsi6_input_next \
42 _ (UDP_INPUT, "udp6-input") \
43 _ (TCP_INPUT, "tcp6-input") \
44 _ (TCP_INPUT_NOLOOKUP, "tcp6-input-nolookup")
52 format_hsi_trace (u8 *s, va_list *args)
54 vlib_main_t *vm = va_arg (*args, vlib_main_t *);
55 vlib_node_t *node = va_arg (*args, vlib_node_t *);
56 hsi_trace_t *t = va_arg (*args, hsi_trace_t *);
59 nn = vlib_get_next_node (vm, node->index, t->next_node);
60 s = format (s, "session %sfound, next node: %v",
61 t->next_node < HSI_INPUT_N_NEXT ? "" : "not ", nn->name);
66 hsi_udp_lookup (vlib_buffer_t *b, void *ip_hdr, u8 is_ip4)
73 ip4_header_t *ip4 = (ip4_header_t *) ip_hdr;
74 hdr = ip4_next_header (ip4);
75 s = session_lookup_safe4 (
76 vnet_buffer (b)->ip.fib_index, &ip4->dst_address, &ip4->src_address,
77 hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_UDP);
81 ip6_header_t *ip6 = (ip6_header_t *) ip_hdr;
82 hdr = ip6_next_header (ip6);
83 s = session_lookup_safe6 (
84 vnet_buffer (b)->ip.fib_index, &ip6->dst_address, &ip6->src_address,
85 hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_UDP);
90 session_pool_remove_peeker (s->thread_index);
97 always_inline transport_connection_t *
98 hsi_tcp_lookup (vlib_buffer_t *b, void *ip_hdr, u8 is_ip4)
100 transport_connection_t *tc;
106 ip4_header_t *ip4 = (ip4_header_t *) ip_hdr;
107 hdr = ip4_next_header (ip4);
108 tc = session_lookup_connection_wt4 (
109 vnet_buffer (b)->ip.fib_index, &ip4->dst_address, &ip4->src_address,
110 hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_TCP,
111 vlib_get_thread_index (), &result);
115 ip6_header_t *ip6 = (ip6_header_t *) ip_hdr;
116 hdr = ip6_next_header (ip6);
117 tc = session_lookup_connection_wt6 (
118 vnet_buffer (b)->ip.fib_index, &ip6->dst_address, &ip6->src_address,
119 hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_TCP,
120 vlib_get_thread_index (), &result);
123 return result == 0 ? tc : 0;
127 hsi_lookup_and_update (vlib_buffer_t *b, u32 *next, u8 is_ip4)
129 transport_connection_t *tc;
130 u8 proto, state, have_udp;
134 rw_len = vnet_buffer (b)->ip.save_rewrite_length;
135 ip_hdr = vlib_buffer_get_current (b) + rw_len;
138 proto = ((ip4_header_t *) ip_hdr)->protocol;
140 proto = ((ip6_header_t *) ip_hdr)->protocol;
144 case IP_PROTOCOL_TCP:
145 tc = hsi_tcp_lookup (b, ip_hdr, is_ip4);
148 state = ((tcp_connection_t *) tc)->state;
149 if (state == TCP_STATE_LISTEN)
151 *next = HSI_INPUT_NEXT_TCP_INPUT;
153 else if (state == TCP_STATE_SYN_SENT)
155 *next = HSI_INPUT_NEXT_TCP_INPUT;
159 /* Lookup already done, use result */
160 *next = HSI_INPUT_NEXT_TCP_INPUT_NOLOOKUP;
161 vnet_buffer (b)->tcp.connection_index = tc->c_index;
163 vlib_buffer_advance (b, rw_len);
167 vnet_feature_next (next, b);
170 case IP_PROTOCOL_UDP:
171 have_udp = hsi_udp_lookup (b, ip_hdr, is_ip4);
174 *next = HSI_INPUT_NEXT_UDP_INPUT;
175 vlib_buffer_advance (b, rw_len);
179 vnet_feature_next (next, b);
183 vnet_feature_next (next, b);
189 hsi_input_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
190 vlib_buffer_t **bufs, u16 *nexts, u32 n_bufs, u8 is_ip4)
196 for (i = 0; i < n_bufs; i++)
199 if (!(b->flags & VLIB_BUFFER_IS_TRACED))
201 t = vlib_add_trace (vm, node, b, sizeof (*t));
202 t->next_node = nexts[i];
207 hsi46_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
208 vlib_frame_t *frame, int is_ip4)
210 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
211 u16 nexts[VLIB_FRAME_SIZE], *next;
212 u32 n_left_from, *from;
214 from = vlib_frame_vector_args (frame);
215 n_left_from = frame->n_vectors;
217 vlib_get_buffers (vm, from, bufs, n_left_from);
221 while (n_left_from >= 4)
225 vlib_prefetch_buffer_header (b[2], LOAD);
226 CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
228 vlib_prefetch_buffer_header (b[3], LOAD);
229 CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
231 hsi_lookup_and_update (b[0], &next0, is_ip4);
232 hsi_lookup_and_update (b[1], &next1, is_ip4);
246 hsi_lookup_and_update (b[0], &next0, is_ip4);
255 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
257 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
258 hsi_input_trace_frame (vm, node, bufs, nexts, frame->n_vectors, is_ip4);
260 return frame->n_vectors;
263 VLIB_NODE_FN (hsi4_in_node)
264 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
266 return hsi46_input_inline (vm, node, frame, 1 /* is_ip4 */);
269 VLIB_REGISTER_NODE (hsi4_in_node) = {
271 .vector_size = sizeof (u32),
272 .format_trace = format_hsi_trace,
273 .type = VLIB_NODE_TYPE_INTERNAL,
274 .n_errors = HSI_N_ERROR,
275 .error_strings = hsi_error_strings,
276 .n_next_nodes = HSI_INPUT_N_NEXT,
278 #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
279 foreach_hsi4_input_next
284 VNET_FEATURE_INIT (hsi4_in_feature, static) = {
285 .arc_name = "ip4-unicast",
286 .node_name = "hsi4-in",
287 .runs_before = VNET_FEATURES ("ip4-lookup"),
290 VLIB_NODE_FN (hsi4_out_node)
291 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
293 return hsi46_input_inline (vm, node, frame, 1 /* is_ip4 */);
296 VLIB_REGISTER_NODE (hsi4_out_node) = {
298 .vector_size = sizeof (u32),
299 .format_trace = format_hsi_trace,
300 .type = VLIB_NODE_TYPE_INTERNAL,
301 .n_errors = HSI_N_ERROR,
302 .error_strings = hsi_error_strings,
303 .n_next_nodes = HSI_INPUT_N_NEXT,
305 #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
306 foreach_hsi4_input_next
311 VNET_FEATURE_INIT (hsi4_out_feature, static) = {
312 .arc_name = "ip4-output",
313 .node_name = "hsi4-out",
314 .runs_before = VNET_FEATURES ("interface-output"),
317 VLIB_NODE_FN (hsi6_in_node)
318 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
320 return hsi46_input_inline (vm, node, frame, 0 /* is_ip4 */);
323 VLIB_REGISTER_NODE (hsi6_in_node) = {
325 .vector_size = sizeof (u32),
326 .format_trace = format_hsi_trace,
327 .type = VLIB_NODE_TYPE_INTERNAL,
328 .n_errors = HSI_N_ERROR,
329 .error_strings = hsi_error_strings,
330 .n_next_nodes = HSI_INPUT_N_NEXT,
332 #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
333 foreach_hsi6_input_next
338 VNET_FEATURE_INIT (hsi6_in_feature, static) = {
339 .arc_name = "ip6-unicast",
340 .node_name = "hsi6-in",
341 .runs_before = VNET_FEATURES ("ip6-lookup"),
344 VLIB_NODE_FN (hsi6_out_node)
345 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
347 return hsi46_input_inline (vm, node, frame, 0 /* is_ip4 */);
350 VLIB_REGISTER_NODE (hsi6_out_node) = {
352 .vector_size = sizeof (u32),
353 .format_trace = format_hsi_trace,
354 .type = VLIB_NODE_TYPE_INTERNAL,
355 .n_errors = HSI_N_ERROR,
356 .error_strings = hsi_error_strings,
357 .n_next_nodes = HSI_INPUT_N_NEXT,
359 #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
360 foreach_hsi6_input_next
365 VNET_FEATURE_INIT (hsi6_out_feature, static) = {
366 .arc_name = "ip6-output",
367 .node_name = "hsi6-out",
368 .runs_before = VNET_FEATURES ("interface-output"),
371 VLIB_PLUGIN_REGISTER () = {
372 .version = VPP_BUILD_VER,
373 .description = "Host Stack Intercept (HSI)",
374 .default_disabled = 0,
378 * fd.io coding-style-patch-verification: ON
381 * eval: (c-set-style "gnu")