c11 safe string handling support
[vpp.git] / src / plugins / nsh / nsh-md2-ioam / md2_ioam_transit.c
1  /*
2   * Copyright (c) 2017 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 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/udp/udp.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/lisp-gpe/lisp_gpe.h>
22 #include <vnet/lisp-gpe/lisp_gpe_packet.h>
23 #include <nsh/nsh.h>
24 #include <nsh/nsh_packet.h>
25 #include <nsh/nsh-md2-ioam/nsh_md2_ioam.h>
26 #include <nsh/nsh-md2-ioam/nsh_md2_ioam_util.h>
27 #include <vnet/fib/ip6_fib.h>
28 #include <vnet/fib/ip4_fib.h>
29 #include <vnet/fib/fib_entry.h>
30
31 /* Statistics (not really errors) */
32 #define foreach_nsh_md2_ioam_encap_transit_error    \
33 _(ENCAPSULATED, "good packets encapsulated")
34
35 static char *nsh_md2_ioam_encap_transit_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nsh_md2_ioam_encap_transit_error
38 #undef _
39 };
40
41 typedef enum
42 {
43 #define _(sym,str) NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_ERROR_##sym,
44   foreach_nsh_md2_ioam_encap_transit_error
45 #undef _
46     NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_ERROR,
47 } nsh_md2_ioam_encap_transit_error_t;
48
49 typedef enum
50 {
51   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT,
52   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP,
53   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_NEXT
54 } nsh_md2_ioam_encap_transit_next_t;
55
56
57 /* *INDENT-OFF* */
58 VNET_FEATURE_INIT (nsh_md2_ioam_encap_transit, static) =
59 {
60   .arc_name = "ip4-output",
61   .node_name = "nsh-md2-ioam-encap-transit",
62   .runs_before = VNET_FEATURES ("adj-midchain-tx"),
63 };
64 /* *INDENT-ON* */
65
66
67 static uword
68 nsh_md2_ioam_encap_transit (vlib_main_t * vm,
69                             vlib_node_runtime_t * node,
70                             vlib_frame_t * from_frame)
71 {
72   u32 n_left_from, next_index, *from, *to_next;
73
74   from = vlib_frame_vector_args (from_frame);
75   n_left_from = from_frame->n_vectors;
76
77   next_index = node->cached_next_index;
78
79   while (n_left_from > 0)
80     {
81       u32 n_left_to_next;
82
83       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
84
85
86       while (n_left_from > 0 && n_left_to_next > 0)
87         {
88           u32 bi0;
89           vlib_buffer_t *b0;
90           u32 next0 = NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT;
91
92           bi0 = from[0];
93           to_next[0] = bi0;
94           from += 1;
95           to_next += 1;
96           n_left_from -= 1;
97           n_left_to_next -= 1;
98           ip4_header_t *ip0;
99           u32 iph_offset = 0;
100
101           b0 = vlib_get_buffer (vm, bi0);
102           iph_offset = vnet_buffer (b0)->ip.save_rewrite_length;
103           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0)
104                                   + iph_offset);
105
106           /* just forward non ipv4 packets */
107           if (PREDICT_FALSE
108               ((ip0->ip_version_and_header_length & 0xF0) == 0x40))
109             {
110               /* ipv4 packets */
111               udp_header_t *udp_hdr0 = (udp_header_t *) (ip0 + 1);
112               if (PREDICT_FALSE
113                   ((ip0->protocol == IP_PROTOCOL_UDP) &&
114                    (clib_net_to_host_u16 (udp_hdr0->dst_port) ==
115                     UDP_DST_PORT_lisp_gpe)))
116                 {
117
118                   /* Check the iOAM header */
119                   lisp_gpe_header_t *lisp_gpe_hdr0 =
120                     (lisp_gpe_header_t *) (udp_hdr0 + 1);
121                   nsh_base_header_t *nsh_hdr =
122                     (nsh_base_header_t *) (lisp_gpe_hdr0 + 1);
123
124                   if (PREDICT_FALSE
125                       (lisp_gpe_hdr0->next_protocol ==
126                        LISP_GPE_NEXT_PROTO_NSH) && (nsh_hdr->md_type == 2))
127                     {
128                       uword *t = NULL;
129                       nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
130                       fib_prefix_t key4;
131                       clib_memset (&key4, 0, sizeof (key4));
132                       key4.fp_proto = FIB_PROTOCOL_IP4;
133                       key4.fp_addr.ip4.as_u32 = ip0->dst_address.as_u32;
134                       t = hash_get_mem (hm->dst_by_ip4, &key4);
135                       if (t)
136                         {
137                           vlib_buffer_advance (b0,
138                                                (word) (sizeof
139                                                        (ethernet_header_t)));
140                           nsh_md2_ioam_encap_decap_ioam_v4_one_inline (vm,
141                                                                        node,
142                                                                        b0,
143                                                                        &next0,
144                                                                        NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP,
145                                                                        1
146                                                                        /* use_adj */
147                             );
148                           vlib_buffer_advance (b0,
149                                                -(word) (sizeof
150                                                         (ethernet_header_t)));
151                         }
152                     }
153                 }
154             }
155
156           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
157                                            n_left_to_next, bi0, next0);
158         }
159
160       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
161     }
162
163   return from_frame->n_vectors;
164 }
165
166 extern u8 *format_nsh_node_map_trace (u8 * s, va_list * args);
167 /* *INDENT-OFF* */
168 VLIB_REGISTER_NODE (nsh_md2_ioam_encap_transit_node) = {
169   .function = nsh_md2_ioam_encap_transit,
170   .name = "nsh-md2-ioam-encap-transit",
171   .vector_size = sizeof (u32),
172   .format_trace = format_nsh_node_map_trace,
173   .type = VLIB_NODE_TYPE_INTERNAL,
174
175   .n_errors = ARRAY_LEN(nsh_md2_ioam_encap_transit_error_strings),
176   .error_strings = nsh_md2_ioam_encap_transit_error_strings,
177
178   .n_next_nodes = NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_NEXT,
179
180   .next_nodes = {
181         [NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT] = "interface-output",
182         [NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP] = "error-drop",
183   },
184
185 };
186 /* *INDENT-ON* */
187
188
189 /*
190  * fd.io coding-style-patch-verification: ON
191  *
192  * Local Variables:
193  * eval: (c-set-style "gnu")
194  * End:
195  */