IPSEC: SPD counters in the stats sgement
[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  _(DECRYPTION_FAILED, "IPSEC decryption failed")
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
173   from = vlib_frame_vector_args (from_frame);
174   n_left_from = from_frame->n_vectors;
175   thread_index = vm->thread_index;
176
177   next_index = node->cached_next_index;
178
179   while (n_left_from > 0)
180     {
181       u32 n_left_to_next;
182
183       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
184
185       while (n_left_from > 0 && n_left_to_next > 0)
186         {
187           u32 bi0, next0, pi0;
188           vlib_buffer_t *b0;
189           ip4_header_t *ip0;
190           esp_header_t *esp0;
191           ah_header_t *ah0;
192           ip4_ipsec_config_t *c0;
193           ipsec_spd_t *spd0;
194           ipsec_policy_t *p0 = 0;
195
196           bi0 = to_next[0] = from[0];
197           from += 1;
198           n_left_from -= 1;
199           to_next += 1;
200           n_left_to_next -= 1;
201
202           b0 = vlib_get_buffer (vm, bi0);
203           b0->flags |= VNET_BUFFER_F_IS_IP4;
204           b0->flags &= ~VNET_BUFFER_F_IS_IP6;
205           c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
206
207           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
208
209           ip0 = vlib_buffer_get_current (b0);
210
211           if (PREDICT_TRUE
212               (ip0->protocol == IP_PROTOCOL_IPSEC_ESP
213                || ip0->protocol == IP_PROTOCOL_UDP))
214             {
215 #if 0
216               clib_warning
217                 ("packet received from %U to %U spi %u size %u spd_id %u",
218                  format_ip4_address, ip0->src_address.as_u8,
219                  format_ip4_address, ip0->dst_address.as_u8,
220                  clib_net_to_host_u32 (esp0->spi),
221                  clib_net_to_host_u16 (ip0->length), spd0->id);
222 #endif
223
224               esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
225               if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_UDP))
226                 {
227                   esp0 =
228                     (esp_header_t *) ((u8 *) esp0 + sizeof (udp_header_t));
229                 }
230               /* FIXME TODO missing check whether there is enough data inside
231                * IP/UDP to contain ESP header & stuff ? */
232               p0 = ipsec_input_protect_policy_match (spd0,
233                                                      clib_net_to_host_u32
234                                                      (ip0->src_address.
235                                                       as_u32),
236                                                      clib_net_to_host_u32
237                                                      (ip0->dst_address.
238                                                       as_u32),
239                                                      clib_net_to_host_u32
240                                                      (esp0->spi));
241
242               if (PREDICT_TRUE (p0 != NULL))
243                 {
244                   pi0 = p0 - im->policies;
245                   vlib_increment_combined_counter
246                     (&ipsec_spd_policy_counters,
247                      thread_index, pi0, 1,
248                      clib_net_to_host_u16 (ip0->length));
249
250                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
251                   vnet_buffer (b0)->ipsec.flags = 0;
252                   next0 = im->esp4_decrypt_next_index;
253                   vlib_buffer_advance (b0, ((u8 *) esp0 - (u8 *) ip0));
254                   goto trace0;
255                 }
256               else
257                 {
258                   pi0 = ~0;
259                 };
260
261               /* FIXME bypass and discard */
262             trace0:
263               if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
264                   PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
265                 {
266                   ipsec_input_trace_t *tr =
267                     vlib_add_trace (vm, node, b0, sizeof (*tr));
268
269                   tr->proto = ip0->protocol;
270                   if (p0)
271                     tr->sa_id = p0->sa_id;
272                   tr->spi = clib_net_to_host_u32 (esp0->spi);
273                   tr->seq = clib_net_to_host_u32 (esp0->seq);
274                   tr->spd = spd0->id;
275                   tr->policy_index = pi0;
276                 }
277             }
278
279           if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_AH))
280             {
281               ah0 = (ah_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
282               p0 = ipsec_input_protect_policy_match (spd0,
283                                                      clib_net_to_host_u32
284                                                      (ip0->src_address.
285                                                       as_u32),
286                                                      clib_net_to_host_u32
287                                                      (ip0->dst_address.
288                                                       as_u32),
289                                                      clib_net_to_host_u32
290                                                      (ah0->spi));
291
292               if (PREDICT_TRUE (p0 != 0))
293                 {
294                   pi0 = p0 - im->policies;
295                   vlib_increment_combined_counter
296                     (&ipsec_spd_policy_counters,
297                      thread_index, pi0, 1,
298                      clib_net_to_host_u16 (ip0->length));
299                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
300                   vnet_buffer (b0)->ipsec.flags = 0;
301                   next0 = im->ah4_decrypt_next_index;
302                   goto trace1;
303                 }
304               else
305                 {
306                   pi0 = ~0;
307                 }
308               /* FIXME bypass and discard */
309             trace1:
310               if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
311                   PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
312                 {
313                   ipsec_input_trace_t *tr =
314                     vlib_add_trace (vm, node, b0, sizeof (*tr));
315
316                   tr->proto = ip0->protocol;
317                   if (p0)
318                     tr->sa_id = p0->sa_id;
319                   tr->spi = clib_net_to_host_u32 (ah0->spi);
320                   tr->seq = clib_net_to_host_u32 (ah0->seq_no);
321                   tr->spd = spd0->id;
322                   tr->policy_index = pi0;
323                 }
324             }
325
326           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
327                                            to_next, n_left_to_next, bi0,
328                                            next0);
329         }
330       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
331     }
332   vlib_node_increment_counter (vm, ipsec4_input_node.index,
333                                IPSEC_INPUT_ERROR_RX_PKTS,
334                                from_frame->n_vectors);
335
336   return from_frame->n_vectors;
337 }
338
339
340 /* *INDENT-OFF* */
341 VLIB_REGISTER_NODE (ipsec4_input_node,static) = {
342   .name = "ipsec4-input-feature",
343   .vector_size = sizeof (u32),
344   .format_trace = format_ipsec_input_trace,
345   .type = VLIB_NODE_TYPE_INTERNAL,
346
347   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
348   .error_strings = ipsec_input_error_strings,
349
350   .n_next_nodes = IPSEC_INPUT_N_NEXT,
351   .next_nodes = {
352 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
353     foreach_ipsec_input_next
354 #undef _
355   },
356 };
357 /* *INDENT-ON* */
358
359 static vlib_node_registration_t ipsec6_input_node;
360
361
362 VLIB_NODE_FN (ipsec6_input_node) (vlib_main_t * vm,
363                                   vlib_node_runtime_t * node,
364                                   vlib_frame_t * from_frame)
365 {
366   u32 n_left_from, *from, next_index, *to_next, thread_index;
367   ipsec_main_t *im = &ipsec_main;
368
369   from = vlib_frame_vector_args (from_frame);
370   n_left_from = from_frame->n_vectors;
371   thread_index = vm->thread_index;
372
373   next_index = node->cached_next_index;
374
375   while (n_left_from > 0)
376     {
377       u32 n_left_to_next;
378
379       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
380
381       while (n_left_from > 0 && n_left_to_next > 0)
382         {
383           u32 bi0, next0, pi0;
384           vlib_buffer_t *b0;
385           ip6_header_t *ip0;
386           esp_header_t *esp0;
387           ip4_ipsec_config_t *c0;
388           ipsec_spd_t *spd0;
389           ipsec_policy_t *p0 = 0;
390           ah_header_t *ah0;
391           u32 header_size = sizeof (ip0[0]);
392
393           bi0 = to_next[0] = from[0];
394           from += 1;
395           n_left_from -= 1;
396           to_next += 1;
397           n_left_to_next -= 1;
398
399           b0 = vlib_get_buffer (vm, bi0);
400           b0->flags |= VNET_BUFFER_F_IS_IP6;
401           b0->flags &= ~VNET_BUFFER_F_IS_IP4;
402           c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
403
404           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
405
406           ip0 = vlib_buffer_get_current (b0);
407           esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
408           ah0 = (ah_header_t *) ((u8 *) ip0 + header_size);
409
410           if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
411             {
412 #if 0
413               clib_warning
414                 ("packet received from %U to %U spi %u size %u spd_id %u",
415                  format_ip6_address, &ip0->src_address, format_ip6_address,
416                  &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
417                  clib_net_to_host_u16 (ip0->payload_length) + header_size,
418                  spd0->id);
419 #endif
420               p0 = ipsec6_input_protect_policy_match (spd0,
421                                                       &ip0->src_address,
422                                                       &ip0->dst_address,
423                                                       clib_net_to_host_u32
424                                                       (esp0->spi));
425
426               if (PREDICT_TRUE (p0 != 0))
427                 {
428                   pi0 = p0 - im->policies;
429                   vlib_increment_combined_counter
430                     (&ipsec_spd_policy_counters,
431                      thread_index, pi0, 1,
432                      clib_net_to_host_u16 (ip0->payload_length) +
433                      header_size);
434                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
435                   vnet_buffer (b0)->ipsec.flags = 0;
436                   next0 = im->esp6_decrypt_next_index;
437                   vlib_buffer_advance (b0, header_size);
438                   goto trace0;
439                 }
440               else
441                 {
442                   pi0 = ~0;
443                 }
444             }
445           else if (ip0->protocol == IP_PROTOCOL_IPSEC_AH)
446             {
447               p0 = ipsec6_input_protect_policy_match (spd0,
448                                                       &ip0->src_address,
449                                                       &ip0->dst_address,
450                                                       clib_net_to_host_u32
451                                                       (ah0->spi));
452
453               if (PREDICT_TRUE (p0 != 0))
454                 {
455                   pi0 = p0 - im->policies;
456                   vlib_increment_combined_counter
457                     (&ipsec_spd_policy_counters,
458                      thread_index, pi0, 1,
459                      clib_net_to_host_u16 (ip0->payload_length) +
460                      header_size);
461                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
462                   vnet_buffer (b0)->ipsec.flags = 0;
463                   next0 = im->ah6_decrypt_next_index;
464                   goto trace0;
465                 }
466               else
467                 {
468                   pi0 = ~0;
469                 }
470             }
471
472         trace0:
473           if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE) &&
474               PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
475             {
476               ipsec_input_trace_t *tr =
477                 vlib_add_trace (vm, node, b0, sizeof (*tr));
478
479               if (p0)
480                 tr->sa_id = p0->sa_id;
481               tr->proto = ip0->protocol;
482               tr->spi = clib_net_to_host_u32 (esp0->spi);
483               tr->seq = clib_net_to_host_u32 (esp0->seq);
484               tr->spd = spd0->id;
485             }
486
487           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
488                                            n_left_to_next, bi0, next0);
489         }
490       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
491     }
492   vlib_node_increment_counter (vm, ipsec6_input_node.index,
493                                IPSEC_INPUT_ERROR_RX_PKTS,
494                                from_frame->n_vectors);
495
496   return from_frame->n_vectors;
497 }
498
499
500 /* *INDENT-OFF* */
501 VLIB_REGISTER_NODE (ipsec6_input_node,static) = {
502   .name = "ipsec6-input-feature",
503   .vector_size = sizeof (u32),
504   .format_trace = format_ipsec_input_trace,
505   .type = VLIB_NODE_TYPE_INTERNAL,
506
507   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
508   .error_strings = ipsec_input_error_strings,
509
510   .sibling_of = "ipsec4-input-feature",
511 };
512 /* *INDENT-ON* */
513
514 /*
515  * fd.io coding-style-patch-verification: ON
516  *
517  * Local Variables:
518  * eval: (c-set-style "gnu")
519  * End:
520  */