8360a1d5f9be6956580a993d2aa0592a6fd3ffd3
[vpp.git] / vnet / 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
26 #define foreach_ipsec_input_next                \
27 _(DROP, "error-drop")                           \
28 _(ESP_DECRYPT, "esp-decrypt")
29
30 #define _(v, s) IPSEC_INPUT_NEXT_##v,
31 typedef enum
32 {
33   foreach_ipsec_input_next
34 #undef _
35     IPSEC_INPUT_N_NEXT,
36 } ipsec_input_next_t;
37
38
39 #define foreach_ipsec_input_error                   \
40  _(RX_PKTS, "IPSEC pkts received")                  \
41  _(DECRYPTION_FAILED, "IPSEC decryption failed")
42
43
44 typedef enum
45 {
46 #define _(sym,str) IPSEC_INPUT_ERROR_##sym,
47   foreach_ipsec_input_error
48 #undef _
49     IPSEC_INPUT_N_ERROR,
50 } ipsec_input_error_t;
51
52 static char *ipsec_input_error_strings[] = {
53 #define _(sym,string) string,
54   foreach_ipsec_input_error
55 #undef _
56 };
57
58 typedef struct
59 {
60   u32 sa_id;
61   u32 spi;
62   u32 seq;
63 } ipsec_input_trace_t;
64
65 /* packet trace format function */
66 static u8 *
67 format_ipsec_input_trace (u8 * s, va_list * args)
68 {
69   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
70   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
71   ipsec_input_trace_t *t = va_arg (*args, ipsec_input_trace_t *);
72
73   if (t->spi == 0 && t->seq == 0)
74     {
75       s = format (s, "esp: no esp packet");
76       return s;
77     }
78
79   if (t->sa_id != 0)
80     {
81       s = format (s, "esp: sa_id %u spi %u seq %u", t->sa_id, t->spi, t->seq);
82     }
83   else
84     {
85       s = format (s, "esp: no sa spi %u seq %u", t->spi, t->seq);
86     }
87   return s;
88 }
89
90 always_inline ipsec_policy_t *
91 ipsec_input_protect_policy_match (ipsec_spd_t * spd, u32 sa, u32 da, u32 spi)
92 {
93   ipsec_main_t *im = &ipsec_main;
94   ipsec_policy_t *p;
95   ipsec_sa_t *s;
96   u32 *i;
97
98   vec_foreach (i, spd->ipv4_inbound_protect_policy_indices)
99   {
100     p = pool_elt_at_index (spd->policies, *i);
101     s = pool_elt_at_index (im->sad, p->sa_index);
102
103     if (spi != s->spi)
104       continue;
105
106     if (s->is_tunnel)
107       {
108         if (da != clib_net_to_host_u32 (s->tunnel_dst_addr.ip4.as_u32))
109           continue;
110
111         if (sa != clib_net_to_host_u32 (s->tunnel_src_addr.ip4.as_u32))
112           continue;
113
114         return p;
115       }
116
117     if (da < clib_net_to_host_u32 (p->laddr.start.ip4.as_u32))
118       continue;
119
120     if (da > clib_net_to_host_u32 (p->laddr.stop.ip4.as_u32))
121       continue;
122
123     if (sa < clib_net_to_host_u32 (p->raddr.start.ip4.as_u32))
124       continue;
125
126     if (sa > clib_net_to_host_u32 (p->raddr.stop.ip4.as_u32))
127       continue;
128
129     return p;
130   }
131   return 0;
132 }
133
134 always_inline uword
135 ip6_addr_match_range (ip6_address_t * a, ip6_address_t * la,
136                       ip6_address_t * ua)
137 {
138   if ((memcmp (a->as_u64, la->as_u64, 2 * sizeof (u64)) >= 0) &&
139       (memcmp (a->as_u64, ua->as_u64, 2 * sizeof (u64)) <= 0))
140     return 1;
141   return 0;
142 }
143
144 always_inline ipsec_policy_t *
145 ipsec_input_ip6_protect_policy_match (ipsec_spd_t * spd,
146                                       ip6_address_t * sa,
147                                       ip6_address_t * da, u32 spi)
148 {
149   ipsec_main_t *im = &ipsec_main;
150   ipsec_policy_t *p;
151   ipsec_sa_t *s;
152   u32 *i;
153
154   vec_foreach (i, spd->ipv6_inbound_protect_policy_indices)
155   {
156     p = pool_elt_at_index (spd->policies, *i);
157     s = pool_elt_at_index (im->sad, p->sa_index);
158
159     if (spi != s->spi)
160       continue;
161
162     if (s->is_tunnel)
163       {
164         if (!ip6_address_is_equal (sa, &s->tunnel_src_addr.ip6))
165           continue;
166
167         if (!ip6_address_is_equal (da, &s->tunnel_dst_addr.ip6))
168           continue;
169
170         return p;
171       }
172
173     if (!ip6_addr_match_range (sa, &p->raddr.start.ip6, &p->raddr.stop.ip6))
174       continue;
175
176     if (!ip6_addr_match_range (da, &p->laddr.start.ip6, &p->laddr.stop.ip6))
177       continue;
178
179     return p;
180   }
181   return 0;
182 }
183
184 static vlib_node_registration_t ipsec_input_ip4_node;
185
186 static uword
187 ipsec_input_ip4_node_fn (vlib_main_t * vm,
188                          vlib_node_runtime_t * node,
189                          vlib_frame_t * from_frame)
190 {
191   u32 n_left_from, *from, next_index, *to_next;
192   ipsec_main_t *im = &ipsec_main;
193
194   from = vlib_frame_vector_args (from_frame);
195   n_left_from = from_frame->n_vectors;
196
197   next_index = node->cached_next_index;
198
199   while (n_left_from > 0)
200     {
201       u32 n_left_to_next;
202
203       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
204
205       while (n_left_from > 0 && n_left_to_next > 0)
206         {
207           u32 bi0, next0;
208           vlib_buffer_t *b0;
209           ip4_header_t *ip0;
210           esp_header_t *esp0;
211           ip4_ipsec_config_t *c0;
212           ipsec_spd_t *spd0;
213           ipsec_policy_t *p0 = 0;
214
215           bi0 = to_next[0] = from[0];
216           from += 1;
217           n_left_from -= 1;
218           to_next += 1;
219           n_left_to_next -= 1;
220
221           b0 = vlib_get_buffer (vm, bi0);
222           c0 =
223             vnet_feature_next_with_data (vnet_buffer (b0)->sw_if_index
224                                          [VLIB_RX], &next0, b0,
225                                          sizeof (c0[0]));
226
227           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
228
229           ip0 = vlib_buffer_get_current (b0);
230           esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
231
232           if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
233             {
234 #if 0
235               clib_warning
236                 ("packet received from %U to %U spi %u size %u spd_id %u",
237                  format_ip4_address, ip0->src_address.as_u8,
238                  format_ip4_address, ip0->dst_address.as_u8,
239                  clib_net_to_host_u32 (esp0->spi),
240                  clib_net_to_host_u16 (ip0->length), spd0->id);
241 #endif
242
243               p0 = ipsec_input_protect_policy_match (spd0,
244                                                      clib_net_to_host_u32
245                                                      (ip0->src_address.
246                                                       as_u32),
247                                                      clib_net_to_host_u32
248                                                      (ip0->dst_address.
249                                                       as_u32),
250                                                      clib_net_to_host_u32
251                                                      (esp0->spi));
252
253               if (PREDICT_TRUE (p0 != 0))
254                 {
255                   p0->counter.packets++;
256                   p0->counter.bytes += clib_net_to_host_u16 (ip0->length);
257                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
258                   vnet_buffer (b0)->ipsec.flags = 0;
259                   next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
260                   vlib_buffer_advance (b0, ip4_header_bytes (ip0));
261                   goto trace0;
262                 }
263             }
264
265           /* FIXME bypass and discard */
266
267         trace0:
268           if (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               if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
273                 {
274                   if (p0)
275                     tr->sa_id = p0->sa_id;
276                   tr->spi = clib_host_to_net_u32 (esp0->spi);
277                   tr->seq = clib_host_to_net_u32 (esp0->seq);
278                 }
279             }
280
281           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
282                                            to_next, n_left_to_next, bi0,
283                                            next0);
284         }
285       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
286     }
287   vlib_node_increment_counter (vm, ipsec_input_ip4_node.index,
288                                IPSEC_INPUT_ERROR_RX_PKTS,
289                                from_frame->n_vectors);
290
291   return from_frame->n_vectors;
292 }
293
294
295 /* *INDENT-OFF* */
296 VLIB_REGISTER_NODE (ipsec_input_ip4_node,static) = {
297   .function = ipsec_input_ip4_node_fn,
298   .name = "ipsec-input-ip4",
299   .vector_size = sizeof (u32),
300   .format_trace = format_ipsec_input_trace,
301   .type = VLIB_NODE_TYPE_INTERNAL,
302
303   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
304   .error_strings = ipsec_input_error_strings,
305
306   .n_next_nodes = IPSEC_INPUT_N_NEXT,
307   .next_nodes = {
308 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
309     foreach_ipsec_input_next
310 #undef _
311   },
312 };
313 /* *INDENT-ON* */
314
315 VLIB_NODE_FUNCTION_MULTIARCH (ipsec_input_ip4_node, ipsec_input_ip4_node_fn)
316      static vlib_node_registration_t ipsec_input_ip6_node;
317
318      static uword
319        ipsec_input_ip6_node_fn (vlib_main_t * vm,
320                                 vlib_node_runtime_t * node,
321                                 vlib_frame_t * from_frame)
322 {
323   u32 n_left_from, *from, next_index, *to_next;
324   ipsec_main_t *im = &ipsec_main;
325
326   from = vlib_frame_vector_args (from_frame);
327   n_left_from = from_frame->n_vectors;
328
329   next_index = node->cached_next_index;
330
331   while (n_left_from > 0)
332     {
333       u32 n_left_to_next;
334
335       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
336
337       while (n_left_from > 0 && n_left_to_next > 0)
338         {
339           u32 bi0, next0;
340           vlib_buffer_t *b0;
341           ip6_header_t *ip0;
342           esp_header_t *esp0;
343           ip4_ipsec_config_t *c0;
344           ipsec_spd_t *spd0;
345           ipsec_policy_t *p0 = 0;
346           u32 header_size = sizeof (ip0[0]);
347
348           bi0 = to_next[0] = from[0];
349           from += 1;
350           n_left_from -= 1;
351           to_next += 1;
352           n_left_to_next -= 1;
353
354           b0 = vlib_get_buffer (vm, bi0);
355           c0 =
356             vnet_feature_next_with_data (vnet_buffer (b0)->sw_if_index
357                                          [VLIB_RX], &next0, b0,
358                                          sizeof (c0[0]));
359
360           spd0 = pool_elt_at_index (im->spds, c0->spd_index);
361
362           ip0 = vlib_buffer_get_current (b0);
363           esp0 = (esp_header_t *) ((u8 *) ip0 + header_size);
364
365           if (PREDICT_TRUE (ip0->protocol == IP_PROTOCOL_IPSEC_ESP))
366             {
367 #if 0
368               clib_warning
369                 ("packet received from %U to %U spi %u size %u spd_id %u",
370                  format_ip6_address, &ip0->src_address, format_ip6_address,
371                  &ip0->dst_address, clib_net_to_host_u32 (esp0->spi),
372                  clib_net_to_host_u16 (ip0->payload_length) + header_size,
373                  spd0->id);
374 #endif
375               p0 = ipsec_input_ip6_protect_policy_match (spd0,
376                                                          &ip0->src_address,
377                                                          &ip0->dst_address,
378                                                          clib_net_to_host_u32
379                                                          (esp0->spi));
380
381               if (PREDICT_TRUE (p0 != 0))
382                 {
383                   p0->counter.packets++;
384                   p0->counter.bytes +=
385                     clib_net_to_host_u16 (ip0->payload_length);
386                   p0->counter.bytes += header_size;
387                   vnet_buffer (b0)->ipsec.sad_index = p0->sa_index;
388                   vnet_buffer (b0)->ipsec.flags = 0;
389                   next0 = IPSEC_INPUT_NEXT_ESP_DECRYPT;
390                   vlib_buffer_advance (b0, header_size);
391                   goto trace0;
392                 }
393             }
394
395         trace0:
396           if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
397             {
398               ipsec_input_trace_t *tr =
399                 vlib_add_trace (vm, node, b0, sizeof (*tr));
400               if (ip0->protocol == IP_PROTOCOL_IPSEC_ESP)
401                 {
402                   if (p0)
403                     tr->sa_id = p0->sa_id;
404                   tr->spi = clib_host_to_net_u32 (esp0->spi);
405                   tr->seq = clib_host_to_net_u32 (esp0->seq);
406                 }
407             }
408
409           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
410                                            n_left_to_next, bi0, next0);
411         }
412       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
413     }
414   vlib_node_increment_counter (vm, ipsec_input_ip6_node.index,
415                                IPSEC_INPUT_ERROR_RX_PKTS,
416                                from_frame->n_vectors);
417
418   return from_frame->n_vectors;
419 }
420
421
422 /* *INDENT-OFF* */
423 VLIB_REGISTER_NODE (ipsec_input_ip6_node,static) = {
424   .function = ipsec_input_ip6_node_fn,
425   .name = "ipsec-input-ip6",
426   .vector_size = sizeof (u32),
427   .format_trace = format_ipsec_input_trace,
428   .type = VLIB_NODE_TYPE_INTERNAL,
429
430   .n_errors = ARRAY_LEN(ipsec_input_error_strings),
431   .error_strings = ipsec_input_error_strings,
432
433   .n_next_nodes = IPSEC_INPUT_N_NEXT,
434   .next_nodes = {
435 #define _(s,n) [IPSEC_INPUT_NEXT_##s] = n,
436     foreach_ipsec_input_next
437 #undef _
438   },
439 };
440 /* *INDENT-ON* */
441
442 VLIB_NODE_FUNCTION_MULTIARCH (ipsec_input_ip6_node, ipsec_input_ip6_node_fn)
443 /*
444  * fd.io coding-style-patch-verification: ON
445  *
446  * Local Variables:
447  * eval: (c-set-style "gnu")
448  * End:
449  */