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