ipsec: rewind missing from dual loop
[vpp.git] / src / vnet / ipsec / ipsec_if_in.c
1 /*
2  * ipsec_if_in.c : IPSec interface input node
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 #include <vnet/ipsec/ipsec_io.h>
25 #include <vnet/ipsec/ipsec_punt.h>
26
27 /* Statistics (not really errors) */
28 #define foreach_ipsec_if_input_error                              \
29 _(RX, "good packets received")                                    \
30 _(DISABLED, "ipsec packets received on disabled interface")       \
31 _(NO_TUNNEL, "no matching tunnel")                                \
32 _(SPI_0, "SPI 0")
33
34 static char *ipsec_if_input_error_strings[] = {
35 #define _(sym,string) string,
36   foreach_ipsec_if_input_error
37 #undef _
38 };
39
40 typedef enum
41 {
42 #define _(sym,str) IPSEC_IF_INPUT_ERROR_##sym,
43   foreach_ipsec_if_input_error
44 #undef _
45     IPSEC_IF_INPUT_N_ERROR,
46 } ipsec_if_input_error_t;
47
48
49 typedef struct
50 {
51   u32 spi;
52   u32 seq;
53 } ipsec_if_input_trace_t;
54
55 static u8 *
56 format_ipsec_if_input_trace (u8 * s, va_list * args)
57 {
58   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60   ipsec_if_input_trace_t *t = va_arg (*args, ipsec_if_input_trace_t *);
61
62   s = format (s, "IPSec: spi %u (0x%08x) seq %u", t->spi, t->spi, t->seq);
63   return s;
64 }
65
66 always_inline u16
67 ipsec_ip4_if_no_tunnel (vlib_node_runtime_t * node,
68                         vlib_buffer_t * b,
69                         const esp_header_t * esp,
70                         const ip4_header_t * ip4, u16 offset)
71 {
72   if (PREDICT_FALSE (0 == esp->spi))
73     {
74       b->error = node->errors[IPSEC_IF_INPUT_ERROR_SPI_0];
75       b->punt_reason =
76         ipsec_punt_reason[(ip4->protocol == IP_PROTOCOL_UDP ?
77                            IPSEC_PUNT_IP4_SPI_UDP_0 :
78                            IPSEC_PUNT_IP4_NO_SUCH_TUNNEL)];
79     }
80   else
81     {
82       b->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
83       b->punt_reason = ipsec_punt_reason[IPSEC_PUNT_IP4_NO_SUCH_TUNNEL];
84     }
85   vlib_buffer_advance (b, -offset);
86   return IPSEC_INPUT_NEXT_PUNT;
87 }
88
89 always_inline u16
90 ipsec_ip6_if_no_tunnel (vlib_node_runtime_t * node,
91                         vlib_buffer_t * b,
92                         const esp_header_t * esp, u16 offset)
93 {
94   b->error = node->errors[IPSEC_IF_INPUT_ERROR_NO_TUNNEL];
95   b->punt_reason = ipsec_punt_reason[IPSEC_PUNT_IP6_NO_SUCH_TUNNEL];
96
97   vlib_buffer_advance (b, -offset);
98   return (IPSEC_INPUT_NEXT_PUNT);
99 }
100
101 always_inline uword
102 ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
103                        vlib_frame_t * from_frame, int is_ip6)
104 {
105   ipsec_main_t *im = &ipsec_main;
106   vnet_main_t *vnm = im->vnet_main;
107   vnet_interface_main_t *vim = &vnm->interface_main;
108
109   int is_trace = node->flags & VLIB_NODE_FLAG_TRACE;
110   u32 thread_index = vm->thread_index;
111
112   u32 n_left_from, *from;
113   u16 nexts[VLIB_FRAME_SIZE], *next;
114   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
115
116   from = vlib_frame_vector_args (from_frame);
117   n_left_from = from_frame->n_vectors;
118
119   vlib_get_buffers (vm, from, bufs, n_left_from);
120   b = bufs;
121   next = nexts;
122
123   clib_memset_u16 (nexts, im->esp4_decrypt_next_index, n_left_from);
124
125   u64 n_bytes = 0, n_packets = 0;
126   u32 n_disabled = 0, n_no_tunnel = 0;
127
128   u32 last_sw_if_index = ~0;
129   u32 last_tunnel_id = ~0;
130   ipsec4_tunnel_key_t last_key4;
131   ipsec6_tunnel_key_t last_key6;
132
133   vlib_combined_counter_main_t *rx_counter;
134   vlib_combined_counter_main_t *drop_counter;
135
136   if (is_ip6)
137     clib_memset (&last_key6, 0xff, sizeof (last_key6));
138   else
139     last_key4.as_u64 = ~0;
140
141   rx_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
142   drop_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP;
143
144   while (n_left_from >= 2)
145     {
146       u32 sw_if_index0, sw_if_index1;
147       ip4_header_t *ip40, *ip41;
148       ip6_header_t *ip60, *ip61;
149       esp_header_t *esp0, *esp1;
150       u32 len0, len1;
151       u16 buf_adv0, buf_adv1;
152       u16 buf_rewind0, buf_rewind1;
153       u32 tid0, tid1;
154       ipsec_tunnel_if_t *t0, *t1;
155       ipsec4_tunnel_key_t key40, key41;
156       ipsec6_tunnel_key_t key60, key61;
157
158       if (n_left_from >= 4)
159         {
160           CLIB_PREFETCH (b[2], CLIB_CACHE_LINE_BYTES, STORE);
161           CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
162           CLIB_PREFETCH (b[3], CLIB_CACHE_LINE_BYTES, STORE);
163           CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
164         }
165
166       ip40 =
167         (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
168       ip41 =
169         (ip4_header_t *) (b[1]->data + vnet_buffer (b[1])->l3_hdr_offset);
170
171       if (is_ip6)
172         {
173           ip60 = (ip6_header_t *) ip40;
174           ip61 = (ip6_header_t *) ip41;
175           esp0 = (esp_header_t *) ((u8 *) ip60 + sizeof (ip6_header_t));
176           esp1 = (esp_header_t *) ((u8 *) ip61 + sizeof (ip6_header_t));
177           buf_adv0 = sizeof (ip6_header_t);
178           buf_adv1 = sizeof (ip6_header_t);
179         }
180       else
181         {
182           /* NAT UDP port 4500 case, don't advance any more */
183           if (ip40->protocol == IP_PROTOCOL_UDP)
184             {
185               esp0 =
186                 (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40) +
187                                   sizeof (udp_header_t));
188               buf_adv0 = 0;
189               buf_rewind0 = ip4_header_bytes (ip40) + sizeof (udp_header_t);
190             }
191           else
192             {
193               esp0 = (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40));
194               buf_rewind0 = buf_adv0 = ip4_header_bytes (ip40);
195             }
196           /* NAT UDP port 4500 case, don't advance any more */
197           if (ip41->protocol == IP_PROTOCOL_UDP)
198             {
199               esp1 =
200                 (esp_header_t *) ((u8 *) ip41 + ip4_header_bytes (ip41) +
201                                   sizeof (udp_header_t));
202               buf_adv1 = 0;
203               buf_rewind1 = ip4_header_bytes (ip40) + sizeof (udp_header_t);
204             }
205           else
206             {
207               esp1 = (esp_header_t *) ((u8 *) ip41 + ip4_header_bytes (ip41));
208               buf_rewind1 = buf_adv1 = ip4_header_bytes (ip41);
209             }
210         }
211
212       vlib_buffer_advance (b[0], buf_adv0);
213       vlib_buffer_advance (b[1], buf_adv1);
214
215       len0 = vlib_buffer_length_in_chain (vm, b[0]);
216       len1 = vlib_buffer_length_in_chain (vm, b[1]);
217
218       if (is_ip6)
219         {
220           key60.remote_ip = ip60->src_address;
221           key60.spi = esp0->spi;
222
223           if (memcmp (&key60, &last_key6, sizeof (last_key6)) == 0)
224             {
225               tid0 = last_tunnel_id;
226             }
227           else
228             {
229               uword *p =
230                 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key60);
231               if (p)
232                 {
233                   tid0 = p[0];
234                   last_tunnel_id = tid0;
235                   clib_memcpy_fast (&last_key6, &key60, sizeof (key60));
236                 }
237               else
238                 {
239                   next[0] =
240                     ipsec_ip6_if_no_tunnel (node, b[0], esp0, buf_adv0);
241                   n_no_tunnel++;
242                   goto pkt1;
243                 }
244             }
245         }
246       else                      /* !is_ip6 */
247         {
248           key40.remote_ip = ip40->src_address.as_u32;
249           key40.spi = esp0->spi;
250
251           if (key40.as_u64 == last_key4.as_u64)
252             {
253               tid0 = last_tunnel_id;
254             }
255           else
256             {
257               uword *p =
258                 hash_get (im->ipsec4_if_pool_index_by_key, key40.as_u64);
259               if (p)
260                 {
261                   tid0 = p[0];
262                   last_tunnel_id = tid0;
263                   last_key4.as_u64 = key40.as_u64;
264                 }
265               else
266                 {
267                   next[0] =
268                     ipsec_ip4_if_no_tunnel (node, b[0], esp0, ip40,
269                                             buf_rewind0);
270                   n_no_tunnel++;
271                   goto pkt1;
272                 }
273             }
274         }
275
276       t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
277       vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
278
279       if (PREDICT_TRUE (t0->hw_if_index != ~0))
280         {
281           sw_if_index0 = t0->sw_if_index;
282           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
283
284           if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
285             {
286               vlib_increment_combined_counter
287                 (drop_counter, thread_index, sw_if_index0, 1, len0);
288               n_disabled++;
289               b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_DISABLED];
290               next[0] = IPSEC_INPUT_NEXT_DROP;
291               goto pkt1;
292             }
293
294           if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
295             {
296               n_packets++;
297               n_bytes += len0;
298             }
299           else
300             {
301               if (n_packets)
302                 {
303                   vlib_increment_combined_counter
304                     (rx_counter, thread_index, last_sw_if_index,
305                      n_packets, n_bytes);
306                 }
307
308               last_sw_if_index = sw_if_index0;
309               n_packets = 1;
310               n_bytes = len0;
311             }
312         }
313
314     pkt1:
315       if (is_ip6)
316         {
317           key61.remote_ip = ip61->src_address;
318           key61.spi = esp1->spi;
319
320           if (memcmp (&key61, &last_key6, sizeof (last_key6)) == 0)
321             {
322               tid1 = last_tunnel_id;
323             }
324           else
325             {
326               uword *p =
327                 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key61);
328               if (p)
329                 {
330                   tid1 = p[0];
331                   last_tunnel_id = tid1;
332                   clib_memcpy_fast (&last_key6, &key61, sizeof (key61));
333                 }
334               else
335                 {
336                   next[1] =
337                     ipsec_ip6_if_no_tunnel (node, b[1], esp1, buf_adv1);
338                   n_no_tunnel++;
339                   goto trace1;
340                 }
341             }
342         }
343       else                      /* !is_ip6 */
344         {
345           key41.remote_ip = ip41->src_address.as_u32;
346           key41.spi = esp1->spi;
347
348           if (key41.as_u64 == last_key4.as_u64)
349             {
350               tid1 = last_tunnel_id;
351             }
352           else
353             {
354               uword *p =
355                 hash_get (im->ipsec4_if_pool_index_by_key, key41.as_u64);
356               if (p)
357                 {
358                   tid1 = p[0];
359                   last_tunnel_id = tid1;
360                   last_key4.as_u64 = key41.as_u64;
361                 }
362               else
363                 {
364                   next[1] =
365                     ipsec_ip4_if_no_tunnel (node, b[1], esp1, ip41,
366                                             buf_rewind1);
367                   n_no_tunnel++;
368                   goto trace1;
369                 }
370             }
371         }
372
373       t1 = pool_elt_at_index (im->tunnel_interfaces, tid1);
374       vnet_buffer (b[1])->ipsec.sad_index = t1->input_sa_index;
375
376       if (PREDICT_TRUE (t1->hw_if_index != ~0))
377         {
378           sw_if_index1 = t1->sw_if_index;
379           vnet_buffer (b[1])->sw_if_index[VLIB_RX] = sw_if_index1;
380
381           if (PREDICT_FALSE (!(t1->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
382             {
383               vlib_increment_combined_counter
384                 (drop_counter, thread_index, sw_if_index1, 1, len1);
385               n_disabled++;
386               b[1]->error = node->errors[IPSEC_IF_INPUT_ERROR_DISABLED];
387               next[1] = IPSEC_INPUT_NEXT_DROP;
388               goto trace1;
389             }
390
391           if (PREDICT_TRUE (sw_if_index1 == last_sw_if_index))
392             {
393               n_packets++;
394               n_bytes += len1;
395             }
396           else
397             {
398               if (n_packets)
399                 {
400                   vlib_increment_combined_counter
401                     (rx_counter, thread_index, last_sw_if_index,
402                      n_packets, n_bytes);
403                 }
404
405               last_sw_if_index = sw_if_index1;
406               n_packets = 1;
407               n_bytes = len1;
408             }
409         }
410
411     trace1:
412       if (PREDICT_FALSE (is_trace))
413         {
414           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
415             {
416               ipsec_if_input_trace_t *tr =
417                 vlib_add_trace (vm, node, b[0], sizeof (*tr));
418               tr->spi = clib_host_to_net_u32 (esp0->spi);
419               tr->seq = clib_host_to_net_u32 (esp0->seq);
420             }
421           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
422             {
423               ipsec_if_input_trace_t *tr =
424                 vlib_add_trace (vm, node, b[1], sizeof (*tr));
425               tr->spi = clib_host_to_net_u32 (esp1->spi);
426               tr->seq = clib_host_to_net_u32 (esp1->seq);
427             }
428         }
429
430       /* next */
431       b += 2;
432       next += 2;
433       n_left_from -= 2;
434     }
435   while (n_left_from > 0)
436     {
437       u32 sw_if_index0;
438       ip4_header_t *ip40;
439       ip6_header_t *ip60;
440       esp_header_t *esp0;
441       u32 len0;
442       u16 buf_adv0, buf_rewind0;
443       u32 tid0;
444       ipsec_tunnel_if_t *t0;
445       ipsec4_tunnel_key_t key40;
446       ipsec6_tunnel_key_t key60;
447
448       ip40 =
449         (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
450
451       if (is_ip6)
452         {
453           ip60 = (ip6_header_t *) ip40;
454           esp0 = (esp_header_t *) ((u8 *) ip60 + sizeof (ip6_header_t));
455           buf_adv0 = sizeof (ip6_header_t);
456         }
457       else
458         {
459           /* NAT UDP port 4500 case, don't advance any more */
460           if (ip40->protocol == IP_PROTOCOL_UDP)
461             {
462               esp0 =
463                 (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40) +
464                                   sizeof (udp_header_t));
465               buf_adv0 = 0;
466               buf_rewind0 = ip4_header_bytes (ip40) + sizeof (udp_header_t);
467             }
468           else
469             {
470               esp0 = (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40));
471               buf_rewind0 = buf_adv0 = ip4_header_bytes (ip40);
472             }
473         }
474
475       /* stats for the tunnel include all the data after the IP header
476          just like a norml IP-IP tunnel */
477       vlib_buffer_advance (b[0], buf_adv0);
478       len0 = vlib_buffer_length_in_chain (vm, b[0]);
479
480       if (is_ip6)
481         {
482           key60.remote_ip = ip60->src_address;
483           key60.spi = esp0->spi;
484
485           if (memcmp (&key60, &last_key6, sizeof (last_key6)) == 0)
486             {
487               tid0 = last_tunnel_id;
488             }
489           else
490             {
491               uword *p =
492                 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key60);
493               if (p)
494                 {
495                   tid0 = p[0];
496                   last_tunnel_id = tid0;
497                   clib_memcpy_fast (&last_key6, &key60, sizeof (key60));
498                 }
499               else
500                 {
501                   next[0] =
502                     ipsec_ip6_if_no_tunnel (node, b[0], esp0, buf_adv0);
503                   n_no_tunnel++;
504                   goto trace00;
505                 }
506             }
507         }
508       else                      /* !is_ip6 */
509         {
510           key40.remote_ip = ip40->src_address.as_u32;
511           key40.spi = esp0->spi;
512
513           if (key40.as_u64 == last_key4.as_u64)
514             {
515               tid0 = last_tunnel_id;
516             }
517           else
518             {
519               uword *p =
520                 hash_get (im->ipsec4_if_pool_index_by_key, key40.as_u64);
521               if (p)
522                 {
523                   tid0 = p[0];
524                   last_tunnel_id = tid0;
525                   last_key4.as_u64 = key40.as_u64;
526                 }
527               else
528                 {
529                   next[0] =
530                     ipsec_ip4_if_no_tunnel (node, b[0], esp0, ip40,
531                                             buf_rewind0);
532                   n_no_tunnel++;
533                   goto trace00;
534                 }
535             }
536         }
537
538       t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
539       vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
540
541       if (PREDICT_TRUE (t0->hw_if_index != ~0))
542         {
543           sw_if_index0 = t0->sw_if_index;
544           vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
545
546           if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
547             {
548               vlib_increment_combined_counter
549                 (drop_counter, thread_index, sw_if_index0, 1, len0);
550               n_disabled++;
551               b[0]->error = node->errors[IPSEC_IF_INPUT_ERROR_DISABLED];
552               next[0] = IPSEC_INPUT_NEXT_DROP;
553               goto trace00;
554             }
555
556           if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
557             {
558               n_packets++;
559               n_bytes += len0;
560             }
561           else
562             {
563               if (n_packets)
564                 {
565                   vlib_increment_combined_counter
566                     (rx_counter, thread_index, last_sw_if_index,
567                      n_packets, n_bytes);
568                 }
569
570               last_sw_if_index = sw_if_index0;
571               n_packets = 1;
572               n_bytes = len0;
573             }
574         }
575
576     trace00:
577       if (PREDICT_FALSE (is_trace))
578         {
579           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
580             {
581               ipsec_if_input_trace_t *tr =
582                 vlib_add_trace (vm, node, b[0], sizeof (*tr));
583               tr->spi = clib_host_to_net_u32 (esp0->spi);
584               tr->seq = clib_host_to_net_u32 (esp0->seq);
585             }
586         }
587
588       /* next */
589       b += 1;
590       next += 1;
591       n_left_from -= 1;
592     }
593
594   if (n_packets)
595     {
596       vlib_increment_combined_counter (rx_counter,
597                                        thread_index,
598                                        last_sw_if_index, n_packets, n_bytes);
599     }
600
601   vlib_node_increment_counter (vm, node->node_index,
602                                IPSEC_IF_INPUT_ERROR_RX,
603                                from_frame->n_vectors - (n_disabled +
604                                                         n_no_tunnel));
605
606   vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
607
608   return from_frame->n_vectors;
609 }
610
611 VLIB_NODE_FN (ipsec4_if_input_node) (vlib_main_t * vm,
612                                      vlib_node_runtime_t * node,
613                                      vlib_frame_t * from_frame)
614 {
615   return ipsec_if_input_inline (vm, node, from_frame, 0 /* is_ip6 */ );
616 }
617
618 /* *INDENT-OFF* */
619 VLIB_REGISTER_NODE (ipsec4_if_input_node) = {
620   .name = "ipsec4-if-input",
621   .vector_size = sizeof (u32),
622   .format_trace = format_ipsec_if_input_trace,
623   .type = VLIB_NODE_TYPE_INTERNAL,
624   .n_errors = ARRAY_LEN(ipsec_if_input_error_strings),
625   .error_strings = ipsec_if_input_error_strings,
626   .sibling_of = "ipsec4-input-feature",
627 };
628 /* *INDENT-ON* */
629
630 VLIB_NODE_FN (ipsec6_if_input_node) (vlib_main_t * vm,
631                                      vlib_node_runtime_t * node,
632                                      vlib_frame_t * from_frame)
633 {
634   return ipsec_if_input_inline (vm, node, from_frame, 1 /* is_ip6 */ );
635 }
636
637 /* *INDENT-OFF* */
638 VLIB_REGISTER_NODE (ipsec6_if_input_node) = {
639   .name = "ipsec6-if-input",
640   .vector_size = sizeof (u32),
641   .format_trace = format_ipsec_if_input_trace,
642   .type = VLIB_NODE_TYPE_INTERNAL,
643   .n_errors = ARRAY_LEN(ipsec_if_input_error_strings),
644   .error_strings = ipsec_if_input_error_strings,
645   .sibling_of = "ipsec6-input-feature",
646 };
647 /* *INDENT-ON* */
648
649 /*
650  * fd.io coding-style-patch-verification: ON
651  *
652  * Local Variables:
653  * eval: (c-set-style "gnu")
654  * End:
655  */