LCOV - code coverage report
Current view: top level - plugins/hsi - hsi.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 12 132 9.1 %
Date: 2023-07-05 22:20:52 Functions: 20 30 66.7 %

          Line data    Source code
       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             : #include <vnet/plugin/plugin.h>
      17             : #include <vpp/app/version.h>
      18             : 
      19             : #include <hsi/hsi.h>
      20             : #include <vnet/tcp/tcp_types.h>
      21             : 
      22             : char *hsi_error_strings[] = {
      23             : #define hsi_error(n, s) s,
      24             : #include <hsi/hsi_error.def>
      25             : #undef hsi_error
      26             : };
      27             : 
      28             : typedef enum hsi_input_next_
      29             : {
      30             :   HSI_INPUT_NEXT_UDP_INPUT,
      31             :   HSI_INPUT_NEXT_TCP_INPUT,
      32             :   HSI_INPUT_NEXT_TCP_INPUT_NOLOOKUP,
      33             :   HSI_INPUT_N_NEXT
      34             : } hsi_input_next_t;
      35             : 
      36             : #define foreach_hsi4_input_next                                               \
      37             :   _ (UDP_INPUT, "udp4-input")                                                 \
      38             :   _ (TCP_INPUT, "tcp4-input")                                                 \
      39             :   _ (TCP_INPUT_NOLOOKUP, "tcp4-input-nolookup")
      40             : 
      41             : #define foreach_hsi6_input_next                                               \
      42             :   _ (UDP_INPUT, "udp6-input")                                                 \
      43             :   _ (TCP_INPUT, "tcp6-input")                                                 \
      44             :   _ (TCP_INPUT_NOLOOKUP, "tcp6-input-nolookup")
      45             : 
      46             : typedef struct
      47             : {
      48             :   u32 next_node;
      49             : } hsi_trace_t;
      50             : 
      51             : static u8 *
      52           0 : format_hsi_trace (u8 *s, va_list *args)
      53             : {
      54           0 :   vlib_main_t *vm = va_arg (*args, vlib_main_t *);
      55           0 :   vlib_node_t *node = va_arg (*args, vlib_node_t *);
      56           0 :   hsi_trace_t *t = va_arg (*args, hsi_trace_t *);
      57             :   vlib_node_t *nn;
      58             : 
      59           0 :   nn = vlib_get_next_node (vm, node->index, t->next_node);
      60           0 :   s = format (s, "session %sfound, next node: %v",
      61           0 :               t->next_node < HSI_INPUT_N_NEXT ? "" : "not ", nn->name);
      62           0 :   return s;
      63             : }
      64             : 
      65             : always_inline u8
      66           0 : hsi_udp_lookup (vlib_buffer_t *b, void *ip_hdr, u8 is_ip4)
      67             : {
      68             :   udp_header_t *hdr;
      69             :   session_t *s;
      70             : 
      71           0 :   if (is_ip4)
      72             :     {
      73           0 :       ip4_header_t *ip4 = (ip4_header_t *) ip_hdr;
      74           0 :       hdr = ip4_next_header (ip4);
      75           0 :       s = session_lookup_safe4 (
      76           0 :         vnet_buffer (b)->ip.fib_index, &ip4->dst_address, &ip4->src_address,
      77           0 :         hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_UDP);
      78             :     }
      79             :   else
      80             :     {
      81           0 :       ip6_header_t *ip6 = (ip6_header_t *) ip_hdr;
      82           0 :       hdr = ip6_next_header (ip6);
      83           0 :       s = session_lookup_safe6 (
      84           0 :         vnet_buffer (b)->ip.fib_index, &ip6->dst_address, &ip6->src_address,
      85           0 :         hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_UDP);
      86             :     }
      87             : 
      88           0 :   return s ? 1 : 0;
      89             : }
      90             : 
      91             : always_inline transport_connection_t *
      92           0 : hsi_tcp_lookup (vlib_buffer_t *b, void *ip_hdr, tcp_header_t **rhdr, u8 is_ip4)
      93             : {
      94             :   transport_connection_t *tc;
      95             :   tcp_header_t *hdr;
      96           0 :   u8 result = 0;
      97             : 
      98           0 :   if (is_ip4)
      99             :     {
     100           0 :       ip4_header_t *ip4 = (ip4_header_t *) ip_hdr;
     101           0 :       *rhdr = hdr = ip4_next_header (ip4);
     102           0 :       tc = session_lookup_connection_wt4 (
     103           0 :         vnet_buffer (b)->ip.fib_index, &ip4->dst_address, &ip4->src_address,
     104           0 :         hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_TCP,
     105           0 :         vlib_get_thread_index (), &result);
     106             :     }
     107             :   else
     108             :     {
     109           0 :       ip6_header_t *ip6 = (ip6_header_t *) ip_hdr;
     110           0 :       *rhdr = hdr = ip6_next_header (ip6);
     111           0 :       tc = session_lookup_connection_wt6 (
     112           0 :         vnet_buffer (b)->ip.fib_index, &ip6->dst_address, &ip6->src_address,
     113           0 :         hdr->dst_port, hdr->src_port, TRANSPORT_PROTO_TCP,
     114           0 :         vlib_get_thread_index (), &result);
     115             :     }
     116             : 
     117           0 :   return result == 0 ? tc : 0;
     118             : }
     119             : 
     120             : always_inline void
     121           0 : hsi_lookup_and_update (vlib_buffer_t *b, u32 *next, u8 is_ip4, u8 is_input)
     122             : {
     123             :   u8 proto, state, have_udp;
     124           0 :   tcp_header_t *tcp_hdr = 0;
     125             :   tcp_connection_t *tc;
     126           0 :   u32 rw_len = 0;
     127             :   void *ip_hdr;
     128             : 
     129           0 :   if (is_input)
     130             :     {
     131           0 :       ip_hdr = vlib_buffer_get_current (b);
     132           0 :       if (is_ip4)
     133           0 :         ip_lookup_set_buffer_fib_index (ip4_main.fib_index_by_sw_if_index, b);
     134             :       else
     135           0 :         ip_lookup_set_buffer_fib_index (ip6_main.fib_index_by_sw_if_index, b);
     136             :     }
     137             :   else
     138             :     {
     139           0 :       rw_len = vnet_buffer (b)->ip.save_rewrite_length;
     140           0 :       ip_hdr = vlib_buffer_get_current (b) + rw_len;
     141             :     }
     142             : 
     143           0 :   if (is_ip4)
     144           0 :     proto = ((ip4_header_t *) ip_hdr)->protocol;
     145             :   else
     146           0 :     proto = ((ip6_header_t *) ip_hdr)->protocol;
     147             : 
     148           0 :   switch (proto)
     149             :     {
     150           0 :     case IP_PROTOCOL_TCP:
     151           0 :       tc = (tcp_connection_t *) hsi_tcp_lookup (b, ip_hdr, &tcp_hdr, is_ip4);
     152           0 :       if (tc)
     153             :         {
     154           0 :           state = tc->state;
     155           0 :           if (state == TCP_STATE_LISTEN)
     156             :             {
     157             :               /* Avoid processing non syn packets that match listener */
     158           0 :               if (!tcp_syn (tcp_hdr))
     159             :                 {
     160           0 :                   vnet_feature_next (next, b);
     161           0 :                   break;
     162             :                 }
     163           0 :               *next = HSI_INPUT_NEXT_TCP_INPUT;
     164             :             }
     165           0 :           else if (state == TCP_STATE_SYN_SENT)
     166             :             {
     167           0 :               *next = HSI_INPUT_NEXT_TCP_INPUT;
     168             :             }
     169             :           else
     170             :             {
     171             :               /* Lookup already done, use result */
     172           0 :               *next = HSI_INPUT_NEXT_TCP_INPUT_NOLOOKUP;
     173           0 :               vnet_buffer (b)->tcp.connection_index = tc->c_c_index;
     174             :             }
     175           0 :           vlib_buffer_advance (b, rw_len);
     176             :         }
     177             :       else
     178             :         {
     179           0 :           vnet_feature_next (next, b);
     180             :         }
     181           0 :       break;
     182           0 :     case IP_PROTOCOL_UDP:
     183           0 :       have_udp = hsi_udp_lookup (b, ip_hdr, is_ip4);
     184           0 :       if (have_udp)
     185             :         {
     186           0 :           *next = HSI_INPUT_NEXT_UDP_INPUT;
     187             :           /* Emulate udp-local and consume headers up to udp payload */
     188           0 :           rw_len += is_ip4 ? sizeof (ip4_header_t) : sizeof (ip6_header_t);
     189           0 :           rw_len += sizeof (udp_header_t);
     190           0 :           vlib_buffer_advance (b, rw_len);
     191             :         }
     192             :       else
     193             :         {
     194           0 :           vnet_feature_next (next, b);
     195             :         }
     196           0 :       break;
     197           0 :     default:
     198           0 :       vnet_feature_next (next, b);
     199           0 :       break;
     200             :     }
     201           0 : }
     202             : 
     203             : static void
     204           0 : hsi_input_trace_frame (vlib_main_t *vm, vlib_node_runtime_t *node,
     205             :                        vlib_buffer_t **bufs, u16 *nexts, u32 n_bufs, u8 is_ip4)
     206             : {
     207             :   vlib_buffer_t *b;
     208             :   hsi_trace_t *t;
     209             :   int i;
     210             : 
     211           0 :   for (i = 0; i < n_bufs; i++)
     212             :     {
     213           0 :       b = bufs[i];
     214           0 :       if (!(b->flags & VLIB_BUFFER_IS_TRACED))
     215           0 :         continue;
     216           0 :       t = vlib_add_trace (vm, node, b, sizeof (*t));
     217           0 :       t->next_node = nexts[i];
     218             :     }
     219           0 : }
     220             : 
     221             : always_inline uword
     222           0 : hsi46_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
     223             :                     vlib_frame_t *frame, u8 is_ip4, u8 is_input)
     224             : {
     225             :   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
     226             :   u16 nexts[VLIB_FRAME_SIZE], *next;
     227             :   u32 n_left_from, *from;
     228             : 
     229           0 :   from = vlib_frame_vector_args (frame);
     230           0 :   n_left_from = frame->n_vectors;
     231             : 
     232           0 :   vlib_get_buffers (vm, from, bufs, n_left_from);
     233           0 :   b = bufs;
     234           0 :   next = nexts;
     235             : 
     236           0 :   while (n_left_from >= 4)
     237             :     {
     238             :       u32 next0, next1;
     239             : 
     240           0 :       vlib_prefetch_buffer_header (b[2], LOAD);
     241           0 :       CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
     242             : 
     243           0 :       vlib_prefetch_buffer_header (b[3], LOAD);
     244           0 :       CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
     245             : 
     246           0 :       hsi_lookup_and_update (b[0], &next0, is_ip4, is_input);
     247           0 :       hsi_lookup_and_update (b[1], &next1, is_ip4, is_input);
     248             : 
     249           0 :       next[0] = next0;
     250           0 :       next[1] = next1;
     251             : 
     252           0 :       b += 2;
     253           0 :       next += 2;
     254           0 :       n_left_from -= 2;
     255             :     }
     256             : 
     257           0 :   while (n_left_from)
     258             :     {
     259             :       u32 next0;
     260             : 
     261           0 :       hsi_lookup_and_update (b[0], &next0, is_ip4, is_input);
     262             : 
     263           0 :       next[0] = next0;
     264             : 
     265           0 :       b += 1;
     266           0 :       next += 1;
     267           0 :       n_left_from -= 1;
     268             :     }
     269             : 
     270           0 :   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
     271             : 
     272           0 :   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
     273           0 :     hsi_input_trace_frame (vm, node, bufs, nexts, frame->n_vectors, is_ip4);
     274             : 
     275           0 :   return frame->n_vectors;
     276             : }
     277             : 
     278         559 : VLIB_NODE_FN (hsi4_in_node)
     279             : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
     280             : {
     281           0 :   return hsi46_input_inline (vm, node, frame, 1 /* is_ip4 */,
     282             :                              1 /* is_input */);
     283             : }
     284             : 
     285      127160 : VLIB_REGISTER_NODE (hsi4_in_node) = {
     286             :   .name = "hsi4-in",
     287             :   .vector_size = sizeof (u32),
     288             :   .format_trace = format_hsi_trace,
     289             :   .type = VLIB_NODE_TYPE_INTERNAL,
     290             :   .n_errors = HSI_N_ERROR,
     291             :   .error_strings = hsi_error_strings,
     292             :   .n_next_nodes = HSI_INPUT_N_NEXT,
     293             :   .next_nodes = {
     294             : #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
     295             :       foreach_hsi4_input_next
     296             : #undef _
     297             :   },
     298             : };
     299             : 
     300       48183 : VNET_FEATURE_INIT (hsi4_in_feature, static) = {
     301             :   .arc_name = "ip4-unicast",
     302             :   .node_name = "hsi4-in",
     303             :   .runs_before = VNET_FEATURES ("ip4-lookup"),
     304             :   .runs_after = VNET_FEATURES ("ip4-full-reassembly-feature"),
     305             : };
     306             : 
     307         559 : VLIB_NODE_FN (hsi4_out_node)
     308             : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
     309             : {
     310           0 :   return hsi46_input_inline (vm, node, frame, 1 /* is_ip4 */,
     311             :                              0 /* is_input */);
     312             : }
     313             : 
     314      127160 : VLIB_REGISTER_NODE (hsi4_out_node) = {
     315             :   .name = "hsi4-out",
     316             :   .vector_size = sizeof (u32),
     317             :   .format_trace = format_hsi_trace,
     318             :   .type = VLIB_NODE_TYPE_INTERNAL,
     319             :   .n_errors = HSI_N_ERROR,
     320             :   .error_strings = hsi_error_strings,
     321             :   .n_next_nodes = HSI_INPUT_N_NEXT,
     322             :   .next_nodes = {
     323             : #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
     324             :       foreach_hsi4_input_next
     325             : #undef _
     326             :   },
     327             : };
     328             : 
     329       48183 : VNET_FEATURE_INIT (hsi4_out_feature, static) = {
     330             :   .arc_name = "ip4-output",
     331             :   .node_name = "hsi4-out",
     332             :   .runs_before = VNET_FEATURES ("interface-output"),
     333             : };
     334             : 
     335         559 : VLIB_NODE_FN (hsi6_in_node)
     336             : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
     337             : {
     338           0 :   return hsi46_input_inline (vm, node, frame, 0 /* is_ip4 */,
     339             :                              1 /* is_input */);
     340             : }
     341             : 
     342      127160 : VLIB_REGISTER_NODE (hsi6_in_node) = {
     343             :   .name = "hsi6-in",
     344             :   .vector_size = sizeof (u32),
     345             :   .format_trace = format_hsi_trace,
     346             :   .type = VLIB_NODE_TYPE_INTERNAL,
     347             :   .n_errors = HSI_N_ERROR,
     348             :   .error_strings = hsi_error_strings,
     349             :   .n_next_nodes = HSI_INPUT_N_NEXT,
     350             :   .next_nodes = {
     351             : #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
     352             :       foreach_hsi6_input_next
     353             : #undef _
     354             :   },
     355             : };
     356             : 
     357       48183 : VNET_FEATURE_INIT (hsi6_in_feature, static) = {
     358             :   .arc_name = "ip6-unicast",
     359             :   .node_name = "hsi6-in",
     360             :   .runs_before = VNET_FEATURES ("ip6-lookup"),
     361             :   .runs_after = VNET_FEATURES ("ip6-full-reassembly-feature"),
     362             : };
     363             : 
     364         559 : VLIB_NODE_FN (hsi6_out_node)
     365             : (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
     366             : {
     367           0 :   return hsi46_input_inline (vm, node, frame, 0 /* is_ip4 */,
     368             :                              0 /* is_input */);
     369             : }
     370             : 
     371      127160 : VLIB_REGISTER_NODE (hsi6_out_node) = {
     372             :   .name = "hsi6-out",
     373             :   .vector_size = sizeof (u32),
     374             :   .format_trace = format_hsi_trace,
     375             :   .type = VLIB_NODE_TYPE_INTERNAL,
     376             :   .n_errors = HSI_N_ERROR,
     377             :   .error_strings = hsi_error_strings,
     378             :   .n_next_nodes = HSI_INPUT_N_NEXT,
     379             :   .next_nodes = {
     380             : #define _(s, n) [HSI_INPUT_NEXT_##s] = n,
     381             :       foreach_hsi6_input_next
     382             : #undef _
     383             :   },
     384             : };
     385             : 
     386       48183 : VNET_FEATURE_INIT (hsi6_out_feature, static) = {
     387             :   .arc_name = "ip6-output",
     388             :   .node_name = "hsi6-out",
     389             :   .runs_before = VNET_FEATURES ("interface-output"),
     390             : };
     391             : 
     392             : VLIB_PLUGIN_REGISTER () = {
     393             :   .version = VPP_BUILD_VER,
     394             :   .description = "Host Stack Intercept (HSI)",
     395             :   .default_disabled = 0,
     396             : };
     397             : 
     398             : /*
     399             :  * fd.io coding-style-patch-verification: ON
     400             :  *
     401             :  * Local Variables:
     402             :  * eval: (c-set-style "gnu")
     403             :  * End:
     404             :  */

Generated by: LCOV version 1.14