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