IPSEC: header exports
[vpp.git] / src / vnet / ipsec / ah_decrypt.c
1 /*
2  * ah_decrypt.c : IPSec AH decrypt 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 #include <vnet/ipsec/ipsec_io.h>
26
27 #define foreach_ah_decrypt_next \
28   _ (DROP, "error-drop")        \
29   _ (IP4_INPUT, "ip4-input")    \
30   _ (IP6_INPUT, "ip6-input")    \
31   _ (IPSEC_GRE_INPUT, "ipsec-gre-input")
32
33 #define _(v, s) AH_DECRYPT_NEXT_##v,
34 typedef enum
35 {
36   foreach_ah_decrypt_next
37 #undef _
38     AH_DECRYPT_N_NEXT,
39 } ah_decrypt_next_t;
40
41 #define foreach_ah_decrypt_error                \
42   _ (RX_PKTS, "AH pkts received")               \
43   _ (DECRYPTION_FAILED, "AH decryption failed") \
44   _ (INTEG_ERROR, "Integrity check failed")     \
45   _ (REPLAY, "SA replayed packet")
46
47 typedef enum
48 {
49 #define _(sym,str) AH_DECRYPT_ERROR_##sym,
50   foreach_ah_decrypt_error
51 #undef _
52     AH_DECRYPT_N_ERROR,
53 } ah_decrypt_error_t;
54
55 static char *ah_decrypt_error_strings[] = {
56 #define _(sym,string) string,
57   foreach_ah_decrypt_error
58 #undef _
59 };
60
61 typedef struct
62 {
63   ipsec_integ_alg_t integ_alg;
64   u32 seq_num;
65 } ah_decrypt_trace_t;
66
67 /* packet trace format function */
68 static u8 *
69 format_ah_decrypt_trace (u8 * s, va_list * args)
70 {
71   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73   ah_decrypt_trace_t *t = va_arg (*args, ah_decrypt_trace_t *);
74
75   s = format (s, "ah: integrity %U seq-num %d",
76               format_ipsec_integ_alg, t->integ_alg, t->seq_num);
77   return s;
78 }
79
80 always_inline uword
81 ah_decrypt_inline (vlib_main_t * vm,
82                    vlib_node_runtime_t * node, vlib_frame_t * from_frame,
83                    int is_ip6)
84 {
85   u32 n_left_from, *from, next_index, *to_next, thread_index;
86   ipsec_main_t *im = &ipsec_main;
87   ipsec_proto_main_t *em = &ipsec_proto_main;
88   from = vlib_frame_vector_args (from_frame);
89   n_left_from = from_frame->n_vectors;
90   int icv_size = 0;
91
92   next_index = node->cached_next_index;
93   thread_index = vm->thread_index;
94
95   while (n_left_from > 0)
96     {
97       u32 n_left_to_next;
98
99       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100
101       while (n_left_from > 0 && n_left_to_next > 0)
102         {
103           u32 i_bi0;
104           u32 next0;
105           vlib_buffer_t *i_b0;
106           ah_header_t *ah0;
107           ipsec_sa_t *sa0;
108           u32 sa_index0 = ~0;
109           u32 seq;
110           ip4_header_t *ih4 = 0, *oh4 = 0;
111           ip6_header_t *ih6 = 0, *oh6 = 0;
112           u8 ip_hdr_size = 0;
113           u8 tos = 0;
114           u8 ttl = 0;
115           u32 ip_version_traffic_class_and_flow_label = 0;
116           u8 hop_limit = 0;
117           u8 nexthdr = 0;
118           u8 icv_padding_len = 0;
119
120
121           i_bi0 = from[0];
122           from += 1;
123           n_left_from -= 1;
124           n_left_to_next -= 1;
125
126           next0 = AH_DECRYPT_NEXT_DROP;
127
128           i_b0 = vlib_get_buffer (vm, i_bi0);
129           to_next[0] = i_bi0;
130           to_next += 1;
131           ih4 = vlib_buffer_get_current (i_b0);
132           ih6 = vlib_buffer_get_current (i_b0);
133           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
134           sa0 = pool_elt_at_index (im->sad, sa_index0);
135
136           vlib_prefetch_combined_counter (&ipsec_sa_counters,
137                                           thread_index, sa_index0);
138
139           if (is_ip6)
140             {
141               ip6_ext_header_t *prev = NULL;
142               ip6_ext_header_find_t (ih6, prev, ah0, IP_PROTOCOL_IPSEC_AH);
143               ip_hdr_size = sizeof (ip6_header_t);
144               ASSERT ((u8 *) ah0 - (u8 *) ih6 == ip_hdr_size);
145             }
146           else
147             {
148               ip_hdr_size = ip4_header_bytes (ih4);
149               ah0 = (ah_header_t *) ((u8 *) ih4 + ip_hdr_size);
150             }
151
152           seq = clib_host_to_net_u32 (ah0->seq_no);
153
154           /* anti-replay check */
155           if (sa0->use_anti_replay)
156             {
157               int rv = 0;
158
159               if (PREDICT_TRUE (sa0->use_esn))
160                 rv = esp_replay_check_esn (sa0, seq);
161               else
162                 rv = esp_replay_check (sa0, seq);
163
164               if (PREDICT_FALSE (rv))
165                 {
166                   vlib_node_increment_counter (vm, node->node_index,
167                                                AH_DECRYPT_ERROR_REPLAY, 1);
168                   goto trace;
169                 }
170             }
171
172           vlib_increment_combined_counter
173             (&ipsec_sa_counters, thread_index, sa_index0,
174              1, i_b0->current_length);
175
176           icv_size =
177             em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
178           if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
179             {
180               u8 sig[64];
181               u8 digest[64];
182               clib_memset (sig, 0, sizeof (sig));
183               clib_memset (digest, 0, sizeof (digest));
184               u8 *icv = ah0->auth_data;
185               memcpy (digest, icv, icv_size);
186               clib_memset (icv, 0, icv_size);
187
188               if (is_ip6)
189                 {
190                   ip_version_traffic_class_and_flow_label =
191                     ih6->ip_version_traffic_class_and_flow_label;
192                   hop_limit = ih6->hop_limit;
193                   ih6->ip_version_traffic_class_and_flow_label = 0x60;
194                   ih6->hop_limit = 0;
195                   nexthdr = ah0->nexthdr;
196                   icv_padding_len =
197                     ah_calc_icv_padding_len (icv_size, 1 /* is_ipv6 */ );
198                 }
199               else
200                 {
201                   tos = ih4->tos;
202                   ttl = ih4->ttl;
203                   ih4->tos = 0;
204                   ih4->ttl = 0;
205                   ih4->checksum = 0;
206                   ih4->flags_and_fragment_offset = 0;
207                   icv_padding_len =
208                     ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
209                 }
210               hmac_calc (sa0->integ_alg, sa0->integ_key.data,
211                          sa0->integ_key.len, (u8 *) ih4, i_b0->current_length,
212                          sig, sa0->use_esn, sa0->seq_hi);
213
214               if (PREDICT_FALSE (memcmp (digest, sig, icv_size)))
215                 {
216                   vlib_node_increment_counter (vm, node->node_index,
217                                                AH_DECRYPT_ERROR_INTEG_ERROR,
218                                                1);
219                   goto trace;
220                 }
221
222               if (PREDICT_TRUE (sa0->use_anti_replay))
223                 {
224                   if (PREDICT_TRUE (sa0->use_esn))
225                     esp_replay_advance_esn (sa0, seq);
226                   else
227                     esp_replay_advance (sa0, seq);
228                 }
229
230             }
231
232           vlib_buffer_advance (i_b0,
233                                ip_hdr_size + sizeof (ah_header_t) + icv_size +
234                                icv_padding_len);
235           i_b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
236
237           if (PREDICT_TRUE (sa0->is_tunnel))
238             {                   /* tunnel mode */
239               if (PREDICT_TRUE (ah0->nexthdr == IP_PROTOCOL_IP_IN_IP))
240                 next0 = AH_DECRYPT_NEXT_IP4_INPUT;
241               else if (ah0->nexthdr == IP_PROTOCOL_IPV6)
242                 next0 = AH_DECRYPT_NEXT_IP6_INPUT;
243               else
244                 {
245                   vlib_node_increment_counter (vm, node->node_index,
246                                                AH_DECRYPT_ERROR_DECRYPTION_FAILED,
247                                                1);
248                   goto trace;
249                 }
250             }
251           else
252             {                   /* transport mode */
253               if (is_ip6)
254                 {
255                   vlib_buffer_advance (i_b0, -sizeof (ip6_header_t));
256                   oh6 = vlib_buffer_get_current (i_b0);
257                   memmove (oh6, ih6, sizeof (ip6_header_t));
258
259                   next0 = AH_DECRYPT_NEXT_IP6_INPUT;
260                   oh6->protocol = nexthdr;
261                   oh6->hop_limit = hop_limit;
262                   oh6->ip_version_traffic_class_and_flow_label =
263                     ip_version_traffic_class_and_flow_label;
264                   oh6->payload_length =
265                     clib_host_to_net_u16 (vlib_buffer_length_in_chain
266                                           (vm, i_b0) - sizeof (ip6_header_t));
267                 }
268               else
269                 {
270                   vlib_buffer_advance (i_b0, -sizeof (ip4_header_t));
271                   oh4 = vlib_buffer_get_current (i_b0);
272                   memmove (oh4, ih4, sizeof (ip4_header_t));
273
274                   next0 = AH_DECRYPT_NEXT_IP4_INPUT;
275                   oh4->ip_version_and_header_length = 0x45;
276                   oh4->fragment_id = 0;
277                   oh4->flags_and_fragment_offset = 0;
278                   oh4->protocol = ah0->nexthdr;
279                   oh4->length =
280                     clib_host_to_net_u16 (vlib_buffer_length_in_chain
281                                           (vm, i_b0));
282                   oh4->ttl = ttl;
283                   oh4->tos = tos;
284                   oh4->checksum = ip4_header_checksum (oh4);
285                 }
286             }
287
288           /* for IPSec-GRE tunnel next node is ipsec-gre-input */
289           if (PREDICT_FALSE
290               ((vnet_buffer (i_b0)->ipsec.flags) &
291                IPSEC_FLAG_IPSEC_GRE_TUNNEL))
292             next0 = AH_DECRYPT_NEXT_IPSEC_GRE_INPUT;
293
294
295           vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
296         trace:
297           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
298             {
299               i_b0->flags |= VLIB_BUFFER_IS_TRACED;
300               ah_decrypt_trace_t *tr =
301                 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
302               tr->integ_alg = sa0->integ_alg;
303               tr->seq_num = seq;
304             }
305           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
306                                            n_left_to_next, i_bi0, next0);
307         }
308       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
309     }
310   vlib_node_increment_counter (vm, node->node_index, AH_DECRYPT_ERROR_RX_PKTS,
311                                from_frame->n_vectors);
312
313   return from_frame->n_vectors;
314 }
315
316 VLIB_NODE_FN (ah4_decrypt_node) (vlib_main_t * vm,
317                                  vlib_node_runtime_t * node,
318                                  vlib_frame_t * from_frame)
319 {
320   return ah_decrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
321 }
322
323 /* *INDENT-OFF* */
324 VLIB_REGISTER_NODE (ah4_decrypt_node) = {
325   .name = "ah4-decrypt",
326   .vector_size = sizeof (u32),
327   .format_trace = format_ah_decrypt_trace,
328   .type = VLIB_NODE_TYPE_INTERNAL,
329
330   .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
331   .error_strings = ah_decrypt_error_strings,
332
333   .n_next_nodes = AH_DECRYPT_N_NEXT,
334   .next_nodes = {
335 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
336     foreach_ah_decrypt_next
337 #undef _
338   },
339 };
340 /* *INDENT-ON* */
341
342 VLIB_NODE_FN (ah6_decrypt_node) (vlib_main_t * vm,
343                                  vlib_node_runtime_t * node,
344                                  vlib_frame_t * from_frame)
345 {
346   return ah_decrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
347 }
348
349 /* *INDENT-OFF* */
350 VLIB_REGISTER_NODE (ah6_decrypt_node) = {
351   .name = "ah6-decrypt",
352   .vector_size = sizeof (u32),
353   .format_trace = format_ah_decrypt_trace,
354   .type = VLIB_NODE_TYPE_INTERNAL,
355
356   .n_errors = ARRAY_LEN(ah_decrypt_error_strings),
357   .error_strings = ah_decrypt_error_strings,
358
359   .n_next_nodes = AH_DECRYPT_N_NEXT,
360   .next_nodes = {
361 #define _(s,n) [AH_DECRYPT_NEXT_##s] = n,
362     foreach_ah_decrypt_next
363 #undef _
364   },
365 };
366 /* *INDENT-ON* */
367
368 /*
369  * fd.io coding-style-patch-verification: ON
370  *
371  * Local Variables:
372  * eval: (c-set-style "gnu")
373  * End:
374  */