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