IPSEC: move SA counters into the stats segment
[vpp.git] / src / vnet / ipsec / ipsec_input.c
1 /*
2  * decap.c : IPSec tunnel decapsulation
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 #include <vnet/feature/feature.h>
22
23 #include <vnet/ipsec/ipsec.h>
24 #include <vnet/ipsec/esp.h>
25 #include <vnet/ipsec/ah.h>
26
27 #define foreach_ipsec_input_error               \
28 _(RX_PKTS, "IPSEC pkts received")               \
29 _(RX_MATCH_PKTS, "IPSEC pkts matched")
30
31 typedef enum
32 {
33 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
34   foreach_ipsec_input_error
35 #undef _
36     IPSEC_INPUT_N_ERROR,
37 } ipsec_input_error_t;
38
39 static char *ipsec_input_error_strings[] = {
40 #define _(sym,string) string,
41   foreach_ipsec_input_error
42 #undef _
43 };
44
45 typedef struct
46 {
47   ip_protocol_t proto;
48   u32 spd;
49   u32 policy_index;
50   u32 sa_id;
51   u32 spi;
52   u32 seq;
53 } ipsec_input_trace_t;
54
55 /* packet trace format function */
56 static u8 *
57 format_ipsec_input_trace (u8 * s, va_list * args)
58 {
59   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61   ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
62
63   s = format (s, "%U: sa_id %u spd %u policy %d spi %u seq %u",
64               format_ip_protocol, t->proto, t->sa_id,
65               t->spd, t->policy_index, t->spi, t->seq);
66
67   return s;
68 }
69
70 always_inline ipsec_policy_t *
71 ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
72 {
73   ipsec_main_t *im = &ipsec_main;
74   ipsec_policy_t *p;
75   ipsec_sa_t *s;
76   u32 *i;
77
78   vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP4_INBOUND_PROTECT])
79   {
80     p = pool_elt_at_index (im->policies, *i);
81     s = pool_elt_at_index (im->sad, p->sa_index);
82
83     if (spi != s->spi)
84       continue;
85
86     if (s->is_tunnel)
87       {
88         if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
89           continue;
90
91         if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
92           continue;
93
94         return p;
95       }
96
97     if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
98       continue;
99
100     if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
101       continue;
102
103     if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
104       continue;
105
106     if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
107       continue;
108
109     return p;
110   }
111   return 0;
112 }
113
114 always_inline uword
115 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
116                       ip6_address_t * ua)
117 {
118   if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
119       (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
120     return 1;
121   return 0;
122 }
123
124 always_inline ipsec_policy_t *
125 ipsec6_input_protect_policy_match (ipsec_spd_t * spd,
126                                    ip6_address_t * sa,
127                                    ip6_address_t * da, u32 spi)
128 {
129   ipsec_main_t *im = &ipsec_main;
130   ipsec_policy_t *p;
131   ipsec_sa_t *s;
132   u32 *i;
133
134   vec_foreach (i, spd->policies[IPSEC_SPD_POLICY_IP6_INBOUND_PROTECT])
135   {
136     p = pool_elt_at_index (im->policies, *i);
137     s = pool_elt_at_index (im->sad, p->sa_index);
138
139     if (spi != s->spi)
140       continue;
141
142     if (s->is_tunnel)
143       {
144         if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
145           continue;
146
147         if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
148           continue;
149
150         return p;
151       }
152
153     if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
154       continue;
155
156     if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
157       continue;
158
159     return p;
160   }
161   return 0;
162 }
163
164 static vlib_node_registration_t ipsec4_input_node;
165
166 VLIB_NODE_FN (ipsec4_input_node) (vlib_main_t * vm,
167                                   vlib_node_runtime_t * node,
168                                   vlib_frame_t * from_frame)
169 {
170   u32 n_left_from, *from, next_index, *to_next, thread_index;
171   ipsec_main_t *im = &ipsec_main;
172   u32 ipsec_unprocessed = 0;
173   u32 ipsec_matched = 0;
174
175   from = vlib_frame_vector_args (from_frame);
176   n_left_from = from_frame->n_vectors;
177   thread_index = vm->thread_index;
178
179   next_index = node->cached_next_index;
180
181   while (n_left_from > 0)
182     {
183       u32 n_left_to_next;
184
185       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
186
187       while (n_left_from > 0 && n_left_to_next > 0)
188         {
189           u32 bi0, next0, pi0;
190           vlib_buffer_t *b0;
191           ip4_header_t *ip0;
192           esp_header_t *esp0;
193           ah_header_t *ah0;
194           ip4_ipsec_config_t *c0;
195           ipsec_spd_t *spd0;
196           ipsec_policy_t *p0 = 0;
197
198           bi0 = to_next[0] = from[0];
199           from += 1;
200           n_left_from -= 1;
201           to_next += 1;
202           n_left_to_next -= 1;
203
204           b0 = vlib_get_buffer (vm, bi0);
205           b0->flags |= VNET_BUFFER_F_IS_IP4;
206           b0->flags &= ~VNET_BUFFER_F_IS_IP6;
207           c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
208
209           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
210
211           ip0 = vlib_buffer_get_current (b0);
212
213           if (PREDICT_TRUE
214               (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
215                || ip0->protocol == IP_PROTOCOL_UDP))
216             {
217 #if 0
218               clib_warning
219                 ("packet received from %U to %U spi %u size %u spd_id %u",
220                  format_ip4_address, ip0->src_address.as_u8,
221                  format_ip4_address, ip0->dst_address.as_u8,
222                  clib_net_to_host_u32 (esp0->spi),
223                  clib_net_to_host_u16 (ip0->length), spd0->id);
224 #endif
225
226               esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
227               if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
228                 {
229                   esp0 =
230                     (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
231                 }
232               /* FIXME TODO missing check whether there is enough data inside
233                * IP/UDP to contain ESP header & stuff ? */
234               p0 = ipsec_input_protect_policy_match (spd0,
235                                                      clib_net_to_host_u32
236                                                      (ip0->src_address.
237                                                       as_u32),
238                                                      clib_net_to_host_u32
239                                                      (ip0->dst_address.
240                                                       as_u32),
241                                                      clib_net_to_host_u32
242                                                      (esp0->spi));
243
244               if (PREDICT_TRUE (p0 != NULL))
245                 {
246                   ipsec_matched += 1;
247
248                   pi0 = p0 - im->policies;
249                   vlib_increment_combined_counter
250                     (&ipsec_spd_policy_counters,
251                      thread_index, pi0, 1,
252                      clib_net_to_host_u16 (ip0->length));
253
254                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
255                   vnet_buffer (b0)->ipsec.flags = 0;
256                   next0 = im->esp4_decrypt_next_index;
257                   vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
258                   goto trace0;
259                 }
260               else
261                 {
262                   pi0 = ~0;
263                 };
264
265               /* FIXME bypass and discard */
266             trace0:
267               if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
268                   PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
269                 {
270                   ipsec_input_trace_t *tr =
271                     vlib_add_trace (vm, node, b0, sizeof (*tr));
272
273                   tr->proto = ip0->protocol;
274                   if (p0)
275                     tr->sa_id = p0->sa_id;
276                   tr->spi = clib_net_to_host_u32 (esp0->spi);
277                   tr->seq = clib_net_to_host_u32 (esp0->seq);
278                   tr->spd = spd0->id;
279                   tr->policy_index = pi0;
280                 }
281             }
282           else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
283             {
284               ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
285               p0 = ipsec_input_protect_policy_match (spd0,
286                                                      clib_net_to_host_u32
287                                                      (ip0->src_address.
288                                                       as_u32),
289                                                      clib_net_to_host_u32
290                                                      (ip0->dst_address.
291                                                       as_u32),
292                                                      clib_net_to_host_u32
293                                                      (ah0->spi));
294
295               if (PREDICT_TRUE (p0 != 0))
296                 {
297                   ipsec_matched += 1;
298
299                   pi0 = p0 - im->policies;
300                   vlib_increment_combined_counter
301                     (&ipsec_spd_policy_counters,
302                      thread_index, pi0, 1,
303                      clib_net_to_host_u16 (ip0->length));
304
305                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
306                   vnet_buffer (b0)->ipsec.flags = 0;
307                   next0 = im->ah4_decrypt_next_index;
308                   goto trace1;
309                 }
310               else
311                 {
312                   pi0 = ~0;
313                 }
314               /* FIXME bypass and discard */
315             trace1:
316               if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
317                   PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
318                 {
319                   ipsec_input_trace_t *tr =
320                     vlib_add_trace (vm, node, b0, sizeof (*tr));
321
322                   tr->proto = ip0->protocol;
323                   if (p0)
324                     tr->sa_id = p0->sa_id;
325                   tr->spi = clib_net_to_host_u32 (ah0->spi);
326                   tr->seq = clib_net_to_host_u32 (ah0->seq_no);
327                   tr->spd = spd0->id;
328                   tr->policy_index = pi0;
329                 }
330             }
331           else
332             {
333               ipsec_unprocessed += 1;
334             }
335
336           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
337                                            to_next, n_left_to_next, bi0,
338                                            next0);
339         }
340       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
341     }
342
343   vlib_node_increment_counter (vm, ipsec4_input_node.index,
344                                IPSEC_INPUT_ERROR_RX_PKTS,
345                                from_frame->n_vectors - ipsec_unprocessed);
346
347   vlib_node_increment_counter (vm, ipsec4_input_node.index,
348                                IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
349                                ipsec_matched);
350   return from_frame->n_vectors;
351 }
352
353
354 /* *INDENT-OFF* */
355 VLIB_REGISTER_NODE (ipsec4_input_node,static) = {
356   .name = "ipsec4-input-feature",
357   .vector_size = sizeof (u32),
358   .format_trace = format_ipsec_input_trace,
359   .type = VLIB_NODE_TYPE_INTERNAL,
360   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
361   .error_strings = ipsec_input_error_strings,
362   .n_next_nodes = IPSEC_INPUT_N_NEXT,
363   .next_nodes = {
364 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
365     foreach_ipsec_input_next
366 #undef _
367   },
368 };
369 /* *INDENT-ON* */
370
371 static vlib_node_registration_t ipsec6_input_node;
372
373
374 VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
375                                   vlib_node_runtime_t * node,
376                                   vlib_frame_t * from_frame)
377 {
378   u32 n_left_from, *from, next_index, *to_next, thread_index;
379   ipsec_main_t *im = &ipsec_main;
380   u32 ipsec_unprocessed = 0;
381   u32 ipsec_matched = 0;
382
383   from = vlib_frame_vector_args (from_frame);
384   n_left_from = from_frame->n_vectors;
385   thread_index = vm->thread_index;
386
387   next_index = node->cached_next_index;
388
389   while (n_left_from > 0)
390     {
391       u32 n_left_to_next;
392
393       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
394
395       while (n_left_from > 0 && n_left_to_next > 0)
396         {
397           u32 bi0, next0, pi0;
398           vlib_buffer_t *b0;
399           ip6_header_t *ip0;
400           esp_header_t *esp0;
401           ip4_ipsec_config_t *c0;
402           ipsec_spd_t *spd0;
403           ipsec_policy_t *p0 = 0;
404           ah_header_t *ah0;
405           u32 header_size = sizeof (ip0[0]);
406
407           bi0 = to_next[0] = from[0];
408           from += 1;
409           n_left_from -= 1;
410           to_next += 1;
411           n_left_to_next -= 1;
412
413           b0 = vlib_get_buffer (vm, bi0);
414           b0->flags |= VNET_BUFFER_F_IS_IP6;
415           b0->flags &= ~VNET_BUFFER_F_IS_IP4;
416           c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
417
418           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
419
420           ip0 = vlib_buffer_get_current (b0);
421           esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
422           ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
423
424           if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
425             {
426 #if 0
427               clib_warning
428                 ("packet received from %U to %U spi %u size %u spd_id %u",
429                  format_ip6_address, &ip0->src_address, format_ip6_address,
430                  &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
431                  clib_net_to_host_u16 (ip0->payload_length) + header_size,
432                  spd0->id);
433 #endif
434               p0 = ipsec6_input_protect_policy_match (spd0,
435                                                       &ip0->src_address,
436                                                       &ip0->dst_address,
437                                                       clib_net_to_host_u32
438                                                       (esp0->spi));
439
440               if (PREDICT_TRUE (p0 != 0))
441                 {
442                   ipsec_matched += 1;
443
444                   pi0 = p0 - im->policies;
445                   vlib_increment_combined_counter
446                     (&ipsec_spd_policy_counters,
447                      thread_index, pi0, 1,
448                      clib_net_to_host_u16 (ip0->payload_length) +
449                      header_size);
450
451                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
452                   vnet_buffer (b0)->ipsec.flags = 0;
453                   next0 = im->esp6_decrypt_next_index;
454                   vlib_buffer_advance (b0, header_size);
455                   goto trace0;
456                 }
457               else
458                 {
459                   pi0 = ~0;
460                 }
461             }
462           else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
463             {
464               p0 = ipsec6_input_protect_policy_match (spd0,
465                                                       &ip0->src_address,
466                                                       &ip0->dst_address,
467                                                       clib_net_to_host_u32
468                                                       (ah0->spi));
469
470               if (PREDICT_TRUE (p0 != 0))
471                 {
472                   ipsec_matched += 1;
473                   pi0 = p0 - im->policies;
474                   vlib_increment_combined_counter
475                     (&ipsec_spd_policy_counters,
476                      thread_index, pi0, 1,
477                      clib_net_to_host_u16 (ip0->payload_length) +
478                      header_size);
479
480                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
481                   vnet_buffer (b0)->ipsec.flags = 0;
482                   next0 = im->ah6_decrypt_next_index;
483                   goto trace0;
484                 }
485               else
486                 {
487                   pi0 = ~0;
488                 }
489             }
490           else
491             {
492               ipsec_unprocessed += 1;
493             }
494
495         trace0:
496           if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
497               PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
498             {
499               ipsec_input_trace_t *tr =
500                 vlib_add_trace (vm, node, b0, sizeof (*tr));
501
502               if (p0)
503                 tr->sa_id = p0->sa_id;
504               tr->proto = ip0->protocol;
505               tr->spi = clib_net_to_host_u32 (esp0->spi);
506               tr->seq = clib_net_to_host_u32 (esp0->seq);
507               tr->spd = spd0->id;
508             }
509
510           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
511                                            n_left_to_next, bi0, next0);
512         }
513       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
514     }
515
516   vlib_node_increment_counter (vm, ipsec6_input_node.index,
517                                IPSEC_INPUT_ERROR_RX_PKTS,
518                                from_frame->n_vectors - ipsec_unprocessed);
519
520   vlib_node_increment_counter (vm, ipsec6_input_node.index,
521                                IPSEC_INPUT_ERROR_RX_MATCH_PKTS,
522                                ipsec_matched);
523
524   return from_frame->n_vectors;
525 }
526
527
528 /* *INDENT-OFF* */
529 VLIB_REGISTER_NODE (ipsec6_input_node,static) = {
530   .name = "ipsec6-input-feature",
531   .vector_size = sizeof (u32),
532   .format_trace = format_ipsec_input_trace,
533   .type = VLIB_NODE_TYPE_INTERNAL,
534   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
535   .error_strings = ipsec_input_error_strings,
536   .n_next_nodes = IPSEC_INPUT_N_NEXT,
537   .next_nodes = {
538 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
539     foreach_ipsec_input_next
540 #undef _
541   },
542 };
543 /* *INDENT-ON* */
544
545 /*
546  * fd.io coding-style-patch-verification: ON
547  *
548  * Local Variables:
549  * eval: (c-set-style "gnu")
550  * End:
551  */