LCOV - code coverage report
Current view: top level - plugins/dhcp - dhcp_client_detect.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 40 106 37.7 %
Date: 2023-10-26 01:39:38 Functions: 10 13 76.9 %

          Line data    Source code
       1             : /*
       2             :  * DHCP feature; applied as an input feature to select DHCP packets
       3             :  *
       4             :  * Copyright (c) 2013 Cisco and/or its affiliates.
       5             :  * Licensed under the Apache License, Version 2.0 (the "License");
       6             :  * you may not use this file except in compliance with the License.
       7             :  * You may obtain a copy of the License at:
       8             :  *
       9             :  *     http://www.apache.org/licenses/LICENSE-2.0
      10             :  *
      11             :  * Unless required by applicable law or agreed to in writing, software
      12             :  * distributed under the License is distributed on an "AS IS" BASIS,
      13             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14             :  * See the License for the specific language governing permissions and
      15             :  * limitations under the License.
      16             :  */
      17             : 
      18             : #include <dhcp/client.h>
      19             : #include <vnet/udp/udp_local.h>
      20             : 
      21             : #define foreach_dhcp_client_detect                    \
      22             :   _(EXTRACT, "Extract")
      23             : 
      24             : typedef enum
      25             : {
      26             : #define _(sym,str) DHCP_CLIENT_DETECT_ERROR_##sym,
      27             :   foreach_dhcp_client_detect
      28             : #undef _
      29             :     DHCP_CLIENT_DETECT_N_ERROR,
      30             : } dhcp_client_detect_error_t;
      31             : 
      32             : static char *dhcp_client_detect_error_strings[] = {
      33             : #define _(sym,string) string,
      34             :   foreach_dhcp_client_detect
      35             : #undef _
      36             : };
      37             : 
      38             : typedef enum
      39             : {
      40             : #define _(sym,str) DHCP_CLIENT_DETECT_NEXT_##sym,
      41             :   foreach_dhcp_client_detect
      42             : #undef _
      43             :     DHCP_CLIENT_DETECT_N_NEXT,
      44             : } dhcp_client_detect_next_t;
      45             : 
      46             : /**
      47             :  * per-packet trace data
      48             :  */
      49             : typedef struct dhcp_client_detect_trace_t_
      50             : {
      51             :   /* per-pkt trace data */
      52             :   u8 extracted;
      53             : } dhcp_client_detect_trace_t;
      54             : 
      55        2309 : VLIB_NODE_FN (dhcp_client_detect_node) (vlib_main_t * vm,
      56             :                                         vlib_node_runtime_t * node,
      57             :                                         vlib_frame_t * frame)
      58             : {
      59             :   dhcp_client_detect_next_t next_index;
      60             :   u16 dhcp_client_port_network_order;
      61             :   u32 n_left_from, *from, *to_next;
      62             :   u32 extractions;
      63             : 
      64             :   dhcp_client_port_network_order =
      65           9 :     clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
      66           9 :   next_index = 0;
      67           9 :   extractions = 0;
      68           9 :   n_left_from = frame->n_vectors;
      69           9 :   from = vlib_frame_vector_args (frame);
      70             : 
      71          18 :   while (n_left_from > 0)
      72             :     {
      73             :       u32 n_left_to_next;
      74             : 
      75           9 :       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
      76             : 
      77             :       /*
      78             :        * This loop is optimised not so we can really quickly process DHCp
      79             :        * offers... but so we can quickly sift them out when the interface
      80             :        * is also receiving 'normal' packets
      81             :        */
      82           9 :       while (n_left_from >= 8 && n_left_to_next >= 4)
      83             :         {
      84             :           udp_header_t *udp0, *udp1, *udp2, *udp3;
      85             :           ip4_header_t *ip0, *ip1, *ip2, *ip3;
      86             :           vlib_buffer_t *b0, *b1, *b2, *b3;
      87             :           u32 next0, next1, next2, next3;
      88             :           u32 bi0, bi1, bi2, bi3;
      89             : 
      90           0 :           next0 = next1 = next2 = next3 = ~0;
      91           0 :           bi0 = to_next[0] = from[0];
      92           0 :           bi1 = to_next[1] = from[1];
      93           0 :           bi2 = to_next[2] = from[2];
      94           0 :           bi3 = to_next[3] = from[3];
      95             : 
      96             :           /* Prefetch next iteration. */
      97             :           {
      98             :             vlib_buffer_t *p2, *p3, *p4, *p5;
      99             : 
     100           0 :             p2 = vlib_get_buffer (vm, from[2]);
     101           0 :             p3 = vlib_get_buffer (vm, from[3]);
     102           0 :             p4 = vlib_get_buffer (vm, from[4]);
     103           0 :             p5 = vlib_get_buffer (vm, from[5]);
     104             : 
     105           0 :             vlib_prefetch_buffer_header (p2, STORE);
     106           0 :             vlib_prefetch_buffer_header (p3, STORE);
     107           0 :             vlib_prefetch_buffer_header (p4, STORE);
     108           0 :             vlib_prefetch_buffer_header (p5, STORE);
     109             : 
     110           0 :             CLIB_PREFETCH (p2->data, sizeof (ip0[0]) + sizeof (udp0[0]),
     111             :                            STORE);
     112           0 :             CLIB_PREFETCH (p3->data, sizeof (ip0[0]) + sizeof (udp0[0]),
     113             :                            STORE);
     114           0 :             CLIB_PREFETCH (p4->data, sizeof (ip0[0]) + sizeof (udp0[0]),
     115             :                            STORE);
     116           0 :             CLIB_PREFETCH (p5->data, sizeof (ip0[0]) + sizeof (udp0[0]),
     117             :                            STORE);
     118             :           }
     119             : 
     120           0 :           from += 4;
     121           0 :           to_next += 4;
     122           0 :           n_left_from -= 4;
     123           0 :           n_left_to_next -= 4;
     124             : 
     125           0 :           b0 = vlib_get_buffer (vm, bi0);
     126           0 :           b1 = vlib_get_buffer (vm, bi1);
     127           0 :           b2 = vlib_get_buffer (vm, bi2);
     128           0 :           b3 = vlib_get_buffer (vm, bi3);
     129           0 :           ip0 = vlib_buffer_get_current (b0);
     130           0 :           ip1 = vlib_buffer_get_current (b1);
     131           0 :           ip2 = vlib_buffer_get_current (b2);
     132           0 :           ip3 = vlib_buffer_get_current (b2);
     133             : 
     134           0 :           vnet_feature_next (&next0, b0);
     135           0 :           vnet_feature_next (&next1, b1);
     136           0 :           vnet_feature_next (&next2, b2);
     137           0 :           vnet_feature_next (&next3, b3);
     138             : 
     139           0 :           if (ip0->protocol == IP_PROTOCOL_UDP)
     140             :             {
     141           0 :               udp0 = (udp_header_t *) (ip0 + 1);
     142             : 
     143           0 :               if (dhcp_client_port_network_order == udp0->dst_port)
     144             :                 {
     145           0 :                   next0 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
     146           0 :                   extractions++;
     147             :                 }
     148             :             }
     149           0 :           if (ip1->protocol == IP_PROTOCOL_UDP)
     150             :             {
     151           0 :               udp1 = (udp_header_t *) (ip1 + 1);
     152             : 
     153           0 :               if (dhcp_client_port_network_order == udp1->dst_port)
     154             :                 {
     155           0 :                   next1 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
     156           0 :                   extractions++;
     157             :                 }
     158             :             }
     159           0 :           if (ip2->protocol == IP_PROTOCOL_UDP)
     160             :             {
     161           0 :               udp2 = (udp_header_t *) (ip2 + 1);
     162             : 
     163           0 :               if (dhcp_client_port_network_order == udp2->dst_port)
     164             :                 {
     165           0 :                   next2 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
     166           0 :                   extractions++;
     167             :                 }
     168             :             }
     169           0 :           if (ip3->protocol == IP_PROTOCOL_UDP)
     170             :             {
     171           0 :               udp3 = (udp_header_t *) (ip3 + 1);
     172             : 
     173           0 :               if (dhcp_client_port_network_order == udp3->dst_port)
     174             :                 {
     175           0 :                   next3 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
     176           0 :                   extractions++;
     177             :                 }
     178             :             }
     179             : 
     180           0 :           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
     181             :             {
     182             :               dhcp_client_detect_trace_t *t =
     183           0 :                 vlib_add_trace (vm, node, b0, sizeof (*t));
     184           0 :               t->extracted = (next0 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
     185             :             }
     186           0 :           if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
     187             :             {
     188             :               dhcp_client_detect_trace_t *t =
     189           0 :                 vlib_add_trace (vm, node, b1, sizeof (*t));
     190           0 :               t->extracted = (next1 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
     191             :             }
     192           0 :           if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
     193             :             {
     194             :               dhcp_client_detect_trace_t *t =
     195           0 :                 vlib_add_trace (vm, node, b2, sizeof (*t));
     196           0 :               t->extracted = (next2 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
     197             :             }
     198           0 :           if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
     199             :             {
     200             :               dhcp_client_detect_trace_t *t =
     201           0 :                 vlib_add_trace (vm, node, b3, sizeof (*t));
     202           0 :               t->extracted = (next3 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
     203             :             }
     204             : 
     205             :           /* verify speculative enqueue, maybe switch current next frame */
     206           0 :           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
     207             :                                            to_next, n_left_to_next,
     208             :                                            bi0, bi1, bi2, bi3,
     209             :                                            next0, next1, next2, next3);
     210             :         }
     211             : 
     212          18 :       while (n_left_from > 0 && n_left_to_next > 0)
     213             :         {
     214             :           udp_header_t *udp0;
     215             :           vlib_buffer_t *b0;
     216             :           ip4_header_t *ip0;
     217           9 :           u32 next0 = ~0;
     218             :           u32 bi0;
     219             : 
     220           9 :           bi0 = from[0];
     221           9 :           to_next[0] = bi0;
     222           9 :           from += 1;
     223           9 :           to_next += 1;
     224           9 :           n_left_from -= 1;
     225           9 :           n_left_to_next -= 1;
     226             : 
     227           9 :           b0 = vlib_get_buffer (vm, bi0);
     228           9 :           ip0 = vlib_buffer_get_current (b0);
     229             : 
     230             :           /*
     231             :            * when this feature is applied on an interface that is already
     232             :            * accepting packets (because e.g. the interface has other addresses
     233             :            * assigned) we are looking for the preverbial needle in the haystack
     234             :            * so assume the packet is not the one we are looking for.
     235             :            */
     236           9 :           vnet_feature_next (&next0, b0);
     237             : 
     238             :           /*
     239             :            * all we are looking for here is DHCP/BOOTP packet-to-client
     240             :            * UDO port.
     241             :            */
     242           9 :           if (ip0->protocol == IP_PROTOCOL_UDP)
     243             :             {
     244           9 :               udp0 = (udp_header_t *) (ip0 + 1);
     245             : 
     246           9 :               if (dhcp_client_port_network_order == udp0->dst_port)
     247             :                 {
     248           9 :                   next0 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
     249           9 :                   extractions++;
     250             :                 }
     251             :             }
     252             : 
     253           9 :           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
     254             :             {
     255             :               dhcp_client_detect_trace_t *t =
     256           9 :                 vlib_add_trace (vm, node, b0, sizeof (*t));
     257           9 :               t->extracted = (next0 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
     258             :             }
     259             : 
     260             :           /* verify speculative enqueue, maybe switch current next frame */
     261           9 :           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
     262             :                                            to_next, n_left_to_next,
     263             :                                            bi0, next0);
     264             :         }
     265             : 
     266           9 :       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
     267             :     }
     268             : 
     269           9 :   vlib_node_increment_counter (vm, node->node_index,
     270             :                                DHCP_CLIENT_DETECT_ERROR_EXTRACT, extractions);
     271             : 
     272           9 :   return frame->n_vectors;
     273             : }
     274             : 
     275             : /* packet trace format function */
     276             : static u8 *
     277           1 : format_dhcp_client_detect_trace (u8 * s, va_list * args)
     278             : {
     279           1 :   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
     280           1 :   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
     281           1 :   dhcp_client_detect_trace_t *t =
     282             :     va_arg (*args, dhcp_client_detect_trace_t *);
     283             : 
     284           1 :   s = format (s, "dhcp-client-detect: %s", (t->extracted ? "yes" : "no"));
     285             : 
     286           1 :   return s;
     287             : }
     288             : 
     289             : /* *INDENT-OFF* */
     290      153836 : VLIB_REGISTER_NODE (dhcp_client_detect_node) = {
     291             :   .name = "ip4-dhcp-client-detect",
     292             :   .vector_size = sizeof (u32),
     293             :   .format_trace = format_dhcp_client_detect_trace,
     294             :   .type = VLIB_NODE_TYPE_INTERNAL,
     295             : 
     296             :   .n_errors = ARRAY_LEN(dhcp_client_detect_error_strings),
     297             :   .error_strings = dhcp_client_detect_error_strings,
     298             : 
     299             :   .n_next_nodes = DHCP_CLIENT_DETECT_N_NEXT,
     300             :   .next_nodes = {
     301             :     /*
     302             :      * Jump straight to the UDP dispatch node thus avoiding
     303             :      * the RPF checks in ip4-local that will fail
     304             :      */
     305             :     [DHCP_CLIENT_DETECT_NEXT_EXTRACT] = "ip4-udp-lookup",
     306             :   },
     307             : };
     308             : 
     309       61659 : VNET_FEATURE_INIT (ip4_dvr_reinject_feat_node, static) =
     310             : {
     311             :   .arc_name = "ip4-unicast",
     312             :   .node_name = "ip4-dhcp-client-detect",
     313             :   .runs_before = VNET_FEATURES ("ip4-not-enabled"),
     314             : };
     315             : 
     316             : /* *INDENT-ON* */
     317             : 
     318             : /*
     319             :  * fd.io coding-style-patch-verification: ON
     320             :  *
     321             :  * Local Variables:
     322             :  * eval: (c-set-style "gnu")
     323             :  * End:
     324             :  */

Generated by: LCOV version 1.14