IPSEC: move SA counters into the stats segment
[vpp.git] / src / vnet / ipsec / ah_encrypt.c
1 /*
2  * ah_encrypt.c : IPSec AH encrypt node
3  *
4  * Copyright (c) 2015 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 <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
21
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ah.h>
25
26 #define foreach_ah_encrypt_next \
27   _ (DROP, "error-drop")            \
28   _ (IP4_LOOKUP, "ip4-lookup")      \
29   _ (IP6_LOOKUP, "ip6-lookup")      \
30   _ (INTERFACE_OUTPUT, "interface-output")
31
32
33 #define _(v, s) AH_ENCRYPT_NEXT_##v,
34 typedef enum
35 {
36   foreach_ah_encrypt_next
37 #undef _
38     AH_ENCRYPT_N_NEXT,
39 } ah_encrypt_next_t;
40
41 #define foreach_ah_encrypt_error                   \
42  _(RX_PKTS, "AH pkts received")                    \
43  _(SEQ_CYCLED, "sequence number cycled")
44
45
46 typedef enum
47 {
48 #define _(sym,str) AH_ENCRYPT_ERROR_##sym,
49   foreach_ah_encrypt_error
50 #undef _
51     AH_ENCRYPT_N_ERROR,
52 } ah_encrypt_error_t;
53
54 static char *ah_encrypt_error_strings[] = {
55 #define _(sym,string) string,
56   foreach_ah_encrypt_error
57 #undef _
58 };
59
60 typedef struct
61 {
62   u32 sa_index;
63   u32 spi;
64   u32 seq;
65   ipsec_integ_alg_t integ_alg;
66 } ah_encrypt_trace_t;
67
68 /* packet trace format function */
69 static u8 *
70 format_ah_encrypt_trace (u8 * s, va_list * args)
71 {
72   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74   ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
75
76   s = format (s, "ah: sa-index %d spi %u seq %u integrity %U",
77               t->sa_index, t->spi, t->seq,
78               format_ipsec_integ_alg, t->integ_alg);
79   return s;
80 }
81
82 always_inline uword
83 ah_encrypt_inline (vlib_main_t * vm,
84                    vlib_node_runtime_t * node, vlib_frame_t * from_frame,
85                    int is_ip6)
86 {
87   u32 n_left_from, *from, *to_next = 0, next_index, thread_index;
88   int icv_size = 0;
89   from = vlib_frame_vector_args (from_frame);
90   n_left_from = from_frame->n_vectors;
91   ipsec_main_t *im = &ipsec_main;
92   ipsec_proto_main_t *em = &ipsec_proto_main;
93   next_index = node->cached_next_index;
94   thread_index = vm->thread_index;
95
96   while (n_left_from > 0)
97     {
98       u32 n_left_to_next;
99
100       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
101
102       while (n_left_from > 0 && n_left_to_next > 0)
103         {
104           u32 i_bi0, next0;
105           vlib_buffer_t *i_b0 = 0;
106           u32 sa_index0;
107           ipsec_sa_t *sa0;
108           ip4_and_ah_header_t *ih0, *oh0 = 0;
109           ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
110           u8 ip_hdr_size;
111           u8 next_hdr_type;
112           u8 tos = 0;
113           u8 ttl = 0;
114           u8 hop_limit = 0;
115           u32 ip_version_traffic_class_and_flow_label = 0;
116
117           i_bi0 = from[0];
118           from += 1;
119           n_left_from -= 1;
120           n_left_to_next -= 1;
121           next0 = AH_ENCRYPT_NEXT_DROP;
122
123           i_b0 = vlib_get_buffer (vm, i_bi0);
124           to_next[0] = i_bi0;
125           to_next += 1;
126           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
127           sa0 = pool_elt_at_index (im->sad, sa_index0);
128
129           if (PREDICT_FALSE (esp_seq_advance (sa0)))
130             {
131               vlib_node_increment_counter (vm, node->node_index,
132                                            AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
133               goto trace;
134             }
135           vlib_increment_combined_counter
136             (&ipsec_sa_counters, thread_index, sa_index0,
137              1, i_b0->current_length);
138
139           ssize_t adv;
140           ih0 = vlib_buffer_get_current (i_b0);
141           ttl = ih0->ip4.ttl;
142           tos = ih0->ip4.tos;
143
144           if (PREDICT_TRUE (sa0->is_tunnel))
145             {
146               if (is_ip6)
147                 adv = -sizeof (ip6_and_ah_header_t);
148               else
149                 adv = -sizeof (ip4_and_ah_header_t);
150             }
151           else
152             {
153               adv = -sizeof (ah_header_t);
154             }
155
156           icv_size =
157             em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
158           const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
159           adv -= padding_len;
160           /* transport mode save the eth header before it is overwritten */
161           if (PREDICT_FALSE (!sa0->is_tunnel))
162             {
163               ethernet_header_t *ieh0 = (ethernet_header_t *)
164                 ((u8 *) vlib_buffer_get_current (i_b0) -
165                  sizeof (ethernet_header_t));
166               ethernet_header_t *oeh0 =
167                 (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
168               clib_memcpy_fast (oeh0, ieh0, sizeof (ethernet_header_t));
169             }
170
171           vlib_buffer_advance (i_b0, adv - icv_size);
172
173           if (is_ip6)
174             {
175               ih6_0 = (ip6_and_ah_header_t *) ih0;
176               ip_hdr_size = sizeof (ip6_header_t);
177               oh6_0 = vlib_buffer_get_current (i_b0);
178
179               hop_limit = ih6_0->ip6.hop_limit;
180               ip_version_traffic_class_and_flow_label =
181                 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
182               if (PREDICT_TRUE (sa0->is_tunnel))
183                 {
184                   next_hdr_type = IP_PROTOCOL_IPV6;
185                 }
186               else
187                 {
188                   next_hdr_type = ih6_0->ip6.protocol;
189                   memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
190                 }
191
192               oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
193               oh6_0->ip6.hop_limit = 0;
194               oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0x60;
195               oh6_0->ah.reserved = 0;
196               oh6_0->ah.nexthdr = next_hdr_type;
197               oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
198               oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
199               oh6_0->ip6.payload_length =
200                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) -
201                                       sizeof (ip6_header_t));
202               oh6_0->ah.hdrlen =
203                 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
204             }
205           else
206             {
207               ip_hdr_size = sizeof (ip4_header_t);
208               oh0 = vlib_buffer_get_current (i_b0);
209               clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
210
211               if (PREDICT_TRUE (sa0->is_tunnel))
212                 {
213                   next_hdr_type = IP_PROTOCOL_IP_IN_IP;
214                 }
215               else
216                 {
217                   next_hdr_type = ih0->ip4.protocol;
218                   memmove (oh0, ih0, sizeof (ip4_header_t));
219                 }
220
221               oh0->ip4.length =
222                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
223               oh0->ip4.ip_version_and_header_length = 0x45;
224               oh0->ip4.fragment_id = 0;
225               oh0->ip4.flags_and_fragment_offset = 0;
226               oh0->ip4.ttl = 0;
227               oh0->ip4.tos = 0;
228               oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
229               oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
230               oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
231               oh0->ip4.checksum = 0;
232               oh0->ah.nexthdr = next_hdr_type;
233               oh0->ah.hdrlen =
234                 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
235             }
236
237
238           if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
239             {
240               oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
241               oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
242
243               next0 = sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_next_node;
244               vnet_buffer (i_b0)->ip.adj_index[VLIB_TX] =
245                 sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_index;
246             }
247           else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
248             {
249               oh6_0->ip6.src_address.as_u64[0] =
250                 sa0->tunnel_src_addr.ip6.as_u64[0];
251               oh6_0->ip6.src_address.as_u64[1] =
252                 sa0->tunnel_src_addr.ip6.as_u64[1];
253               oh6_0->ip6.dst_address.as_u64[0] =
254                 sa0->tunnel_dst_addr.ip6.as_u64[0];
255               oh6_0->ip6.dst_address.as_u64[1] =
256                 sa0->tunnel_dst_addr.ip6.as_u64[1];
257
258               next0 = sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_next_node;
259               vnet_buffer (i_b0)->ip.adj_index[VLIB_TX] =
260                 sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_index;
261             }
262
263           u8 sig[64];
264           clib_memset (sig, 0, sizeof (sig));
265           u8 *digest =
266             vlib_buffer_get_current (i_b0) + ip_hdr_size +
267             sizeof (ah_header_t);
268           clib_memset (digest, 0, icv_size);
269
270           unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key.data,
271                                      sa0->integ_key.len,
272                                      vlib_buffer_get_current (i_b0),
273                                      i_b0->current_length, sig, sa0->use_esn,
274                                      sa0->seq_hi);
275
276           memcpy (digest, sig, size);
277           if (is_ip6)
278             {
279               oh6_0->ip6.hop_limit = hop_limit;
280               oh6_0->ip6.ip_version_traffic_class_and_flow_label =
281                 ip_version_traffic_class_and_flow_label;
282             }
283           else
284             {
285               oh0->ip4.ttl = ttl;
286               oh0->ip4.tos = tos;
287               oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
288             }
289
290           if (!sa0->is_tunnel)
291             {
292               next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
293               vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));
294             }
295
296         trace:
297           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
298             {
299               i_b0->flags |= VLIB_BUFFER_IS_TRACED;
300               ah_encrypt_trace_t *tr =
301                 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
302               tr->spi = sa0->spi;
303               tr->seq = sa0->seq - 1;
304               tr->integ_alg = sa0->integ_alg;
305               tr->sa_index = sa_index0;
306             }
307
308           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
309                                            to_next, n_left_to_next, i_bi0,
310                                            next0);
311         }
312       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
313     }
314   vlib_node_increment_counter (vm, node->node_index,
315                                AH_ENCRYPT_ERROR_RX_PKTS,
316                                from_frame->n_vectors);
317
318   return from_frame->n_vectors;
319 }
320
321 VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
322                                  vlib_node_runtime_t * node,
323                                  vlib_frame_t * from_frame)
324 {
325   return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
326 }
327
328 /* *INDENT-OFF* */
329 VLIB_REGISTER_NODE (ah4_encrypt_node) = {
330   .name = "ah4-encrypt",
331   .vector_size = sizeof (u32),
332   .format_trace = format_ah_encrypt_trace,
333   .type = VLIB_NODE_TYPE_INTERNAL,
334
335   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
336   .error_strings = ah_encrypt_error_strings,
337
338   .n_next_nodes = AH_ENCRYPT_N_NEXT,
339   .next_nodes = {
340 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
341     foreach_ah_encrypt_next
342 #undef _
343   },
344 };
345 /* *INDENT-ON* */
346
347 VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
348                                  vlib_node_runtime_t * node,
349                                  vlib_frame_t * from_frame)
350 {
351   return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
352 }
353
354 /* *INDENT-OFF* */
355 VLIB_REGISTER_NODE (ah6_encrypt_node) = {
356   .name = "ah6-encrypt",
357   .vector_size = sizeof (u32),
358   .format_trace = format_ah_encrypt_trace,
359   .type = VLIB_NODE_TYPE_INTERNAL,
360
361   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
362   .error_strings = ah_encrypt_error_strings,
363
364   .n_next_nodes = AH_ENCRYPT_N_NEXT,
365   .next_nodes = {
366 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
367     foreach_ah_encrypt_next
368 #undef _
369   },
370 };
371 /* *INDENT-ON* */
372
373 /*
374  * fd.io coding-style-patch-verification: ON
375  *
376  * Local Variables:
377  * eval: (c-set-style "gnu")
378  * End:
379  */