8b0c14f0cf3064f98c3a1608699f5a187d9ca7bf
[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 spi;
63   u32 seq;
64   ipsec_integ_alg_t integ_alg;
65 } ah_encrypt_trace_t;
66
67 /* packet trace format function */
68 static u8 *
69 format_ah_encrypt_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_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
74
75   s = format (s, "ah: spi %u seq %u integrity %U",
76               t->spi, t->seq, format_ipsec_integ_alg, t->integ_alg);
77   return s;
78 }
79
80 always_inline uword
81 ah_encrypt_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, *to_next = 0, next_index;
86   int icv_size = 0;
87   from = vlib_frame_vector_args (from_frame);
88   n_left_from = from_frame->n_vectors;
89   ipsec_main_t *im = &ipsec_main;
90   ipsec_proto_main_t *em = &ipsec_proto_main;
91   next_index = node->cached_next_index;
92
93   while (n_left_from > 0)
94     {
95       u32 n_left_to_next;
96
97       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
98
99       while (n_left_from > 0 && n_left_to_next > 0)
100         {
101           u32 i_bi0, next0;
102           vlib_buffer_t *i_b0 = 0;
103           u32 sa_index0;
104           ipsec_sa_t *sa0;
105           ip4_and_ah_header_t *ih0, *oh0 = 0;
106           ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
107           u8 ip_hdr_size;
108           u8 next_hdr_type;
109           u8 tos = 0;
110           u8 ttl = 0;
111           u8 hop_limit = 0;
112           u32 ip_version_traffic_class_and_flow_label = 0;
113
114           i_bi0 = from[0];
115           from += 1;
116           n_left_from -= 1;
117           n_left_to_next -= 1;
118           next0 = AH_ENCRYPT_NEXT_DROP;
119
120           i_b0 = vlib_get_buffer (vm, i_bi0);
121           to_next[0] = i_bi0;
122           to_next += 1;
123           sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
124           sa0 = pool_elt_at_index (im->sad, sa_index0);
125
126           if (PREDICT_FALSE (esp_seq_advance (sa0)))
127             {
128               vlib_node_increment_counter (vm, node->node_index,
129                                            AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
130               goto trace;
131             }
132
133
134           sa0->total_data_size += i_b0->current_length;
135
136           ssize_t adv;
137           ih0 = vlib_buffer_get_current (i_b0);
138           ttl = ih0->ip4.ttl;
139           tos = ih0->ip4.tos;
140
141           if (PREDICT_TRUE (sa0->is_tunnel))
142             {
143               if (is_ip6)
144                 adv = -sizeof (ip6_and_ah_header_t);
145               else
146                 adv = -sizeof (ip4_and_ah_header_t);
147             }
148           else
149             {
150               adv = -sizeof (ah_header_t);
151             }
152
153           icv_size =
154             em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
155           const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
156           adv -= padding_len;
157           /* transport mode save the eth header before it is overwritten */
158           if (PREDICT_FALSE (!sa0->is_tunnel))
159             {
160               ethernet_header_t *ieh0 = (ethernet_header_t *)
161                 ((u8 *) vlib_buffer_get_current (i_b0) -
162                  sizeof (ethernet_header_t));
163               ethernet_header_t *oeh0 =
164                 (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
165               clib_memcpy_fast (oeh0, ieh0, sizeof (ethernet_header_t));
166             }
167
168           vlib_buffer_advance (i_b0, adv - icv_size);
169
170           if (is_ip6)
171             {
172               ih6_0 = (ip6_and_ah_header_t *) ih0;
173               ip_hdr_size = sizeof (ip6_header_t);
174               oh6_0 = vlib_buffer_get_current (i_b0);
175
176               hop_limit = ih6_0->ip6.hop_limit;
177               ip_version_traffic_class_and_flow_label =
178                 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
179               if (PREDICT_TRUE (sa0->is_tunnel))
180                 {
181                   next_hdr_type = IP_PROTOCOL_IPV6;
182                 }
183               else
184                 {
185                   next_hdr_type = ih6_0->ip6.protocol;
186                   memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
187                 }
188
189               oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
190               oh6_0->ip6.hop_limit = 0;
191               oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0x60;
192               oh6_0->ah.reserved = 0;
193               oh6_0->ah.nexthdr = next_hdr_type;
194               oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
195               oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
196               oh6_0->ip6.payload_length =
197                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) -
198                                       sizeof (ip6_header_t));
199               oh6_0->ah.hdrlen =
200                 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
201             }
202           else
203             {
204               ip_hdr_size = sizeof (ip4_header_t);
205               oh0 = vlib_buffer_get_current (i_b0);
206               clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
207
208               if (PREDICT_TRUE (sa0->is_tunnel))
209                 {
210                   next_hdr_type = IP_PROTOCOL_IP_IN_IP;
211                 }
212               else
213                 {
214                   next_hdr_type = ih0->ip4.protocol;
215                   memmove (oh0, ih0, sizeof (ip4_header_t));
216                 }
217
218               oh0->ip4.length =
219                 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
220               oh0->ip4.ip_version_and_header_length = 0x45;
221               oh0->ip4.fragment_id = 0;
222               oh0->ip4.flags_and_fragment_offset = 0;
223               oh0->ip4.ttl = 0;
224               oh0->ip4.tos = 0;
225               oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
226               oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
227               oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
228               oh0->ip4.checksum = 0;
229               oh0->ah.nexthdr = next_hdr_type;
230               oh0->ah.hdrlen =
231                 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
232             }
233
234
235           if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
236             {
237               oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
238               oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
239
240               next0 = AH_ENCRYPT_NEXT_IP4_LOOKUP;
241               vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
242             }
243           else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
244             {
245               oh6_0->ip6.src_address.as_u64[0] =
246                 sa0->tunnel_src_addr.ip6.as_u64[0];
247               oh6_0->ip6.src_address.as_u64[1] =
248                 sa0->tunnel_src_addr.ip6.as_u64[1];
249               oh6_0->ip6.dst_address.as_u64[0] =
250                 sa0->tunnel_dst_addr.ip6.as_u64[0];
251               oh6_0->ip6.dst_address.as_u64[1] =
252                 sa0->tunnel_dst_addr.ip6.as_u64[1];
253
254               next0 = AH_ENCRYPT_NEXT_IP6_LOOKUP;
255               vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
256             }
257
258           u8 sig[64];
259           clib_memset (sig, 0, sizeof (sig));
260           u8 *digest =
261             vlib_buffer_get_current (i_b0) + ip_hdr_size +
262             sizeof (ah_header_t);
263           clib_memset (digest, 0, icv_size);
264
265           unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key,
266                                      sa0->integ_key_len,
267                                      vlib_buffer_get_current (i_b0),
268                                      i_b0->current_length, sig, sa0->use_esn,
269                                      sa0->seq_hi);
270
271           memcpy (digest, sig, size);
272           if (is_ip6)
273             {
274               oh6_0->ip6.hop_limit = hop_limit;
275               oh6_0->ip6.ip_version_traffic_class_and_flow_label =
276                 ip_version_traffic_class_and_flow_label;
277             }
278           else
279             {
280               oh0->ip4.ttl = ttl;
281               oh0->ip4.tos = tos;
282               oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
283             }
284
285           if (!sa0->is_tunnel)
286             {
287               next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
288               vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));
289             }
290
291         trace:
292           if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
293             {
294               i_b0->flags |= VLIB_BUFFER_IS_TRACED;
295               ah_encrypt_trace_t *tr =
296                 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
297               tr->spi = sa0->spi;
298               tr->seq = sa0->seq - 1;
299               tr->integ_alg = sa0->integ_alg;
300             }
301
302           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
303                                            to_next, n_left_to_next, i_bi0,
304                                            next0);
305         }
306       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307     }
308   vlib_node_increment_counter (vm, node->node_index,
309                                AH_ENCRYPT_ERROR_RX_PKTS,
310                                from_frame->n_vectors);
311
312   return from_frame->n_vectors;
313 }
314
315 VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
316                                  vlib_node_runtime_t * node,
317                                  vlib_frame_t * from_frame)
318 {
319   return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
320 }
321
322 /* *INDENT-OFF* */
323 VLIB_REGISTER_NODE (ah4_encrypt_node) = {
324   .name = "ah4-encrypt",
325   .vector_size = sizeof (u32),
326   .format_trace = format_ah_encrypt_trace,
327   .type = VLIB_NODE_TYPE_INTERNAL,
328
329   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
330   .error_strings = ah_encrypt_error_strings,
331
332   .n_next_nodes = AH_ENCRYPT_N_NEXT,
333   .next_nodes = {
334 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
335     foreach_ah_encrypt_next
336 #undef _
337   },
338 };
339 /* *INDENT-ON* */
340
341 VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
342                                  vlib_node_runtime_t * node,
343                                  vlib_frame_t * from_frame)
344 {
345   return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
346 }
347
348 /* *INDENT-OFF* */
349 VLIB_REGISTER_NODE (ah6_encrypt_node) = {
350   .name = "ah6-encrypt",
351   .vector_size = sizeof (u32),
352   .format_trace = format_ah_encrypt_trace,
353   .type = VLIB_NODE_TYPE_INTERNAL,
354
355   .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
356   .error_strings = ah_encrypt_error_strings,
357
358   .n_next_nodes = AH_ENCRYPT_N_NEXT,
359   .next_nodes = {
360 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
361     foreach_ah_encrypt_next
362 #undef _
363   },
364 };
365 /* *INDENT-ON* */
366
367 /*
368  * fd.io coding-style-patch-verification: ON
369  *
370  * Local Variables:
371  * eval: (c-set-style "gnu")
372  * End:
373  */