2 * ipsec_if_in.c : IPSec interface input node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>
20 #include <vnet/ip/ip.h>
22 #include <vnet/ipsec/ipsec.h>
23 #include <vnet/ipsec/esp.h>
24 #include <vnet/ipsec/ipsec_io.h>
26 /* Statistics (not really errors) */
27 #define foreach_ipsec_if_input_error \
28 _(RX, "good packets received") \
29 _(DISABLED, "ipsec packets received on disabled interface") \
30 _(NO_TUNNEL, "no matching tunnel")
32 static char *ipsec_if_input_error_strings[] = {
33 #define _(sym,string) string,
34 foreach_ipsec_if_input_error
40 #define _(sym,str) IPSEC_IF_INPUT_ERROR_##sym,
41 foreach_ipsec_if_input_error
43 IPSEC_IF_INPUT_N_ERROR,
44 } ipsec_if_input_error_t;
51 } ipsec_if_input_trace_t;
54 format_ipsec_if_input_trace (u8 * s, va_list * args)
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_if_input_trace_t *t = va_arg (*args, ipsec_if_input_trace_t *);
60 s = format (s, "IPSec: spi %u seq %u", t->spi, t->seq);
66 ipsec_if_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
67 vlib_frame_t * from_frame, int is_ip6)
69 ipsec_main_t *im = &ipsec_main;
70 vnet_main_t *vnm = im->vnet_main;
71 vnet_interface_main_t *vim = &vnm->interface_main;
73 int is_trace = node->flags & VLIB_NODE_FLAG_TRACE;
74 u32 thread_index = vm->thread_index;
76 u32 n_left_from, *from;
77 u16 nexts[VLIB_FRAME_SIZE], *next;
78 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
80 from = vlib_frame_vector_args (from_frame);
81 n_left_from = from_frame->n_vectors;
83 vlib_get_buffers (vm, from, bufs, n_left_from);
87 clib_memset_u16 (nexts, im->esp4_decrypt_next_index, n_left_from);
89 u64 n_bytes = 0, n_packets = 0;
90 u32 n_disabled = 0, n_no_tunnel = 0;
92 u32 last_sw_if_index = ~0;
93 u32 last_tunnel_id = ~0;
94 ipsec4_tunnel_key_t last_key4;
95 ipsec6_tunnel_key_t last_key6;
97 vlib_combined_counter_main_t *rx_counter;
98 vlib_combined_counter_main_t *drop_counter;
101 clib_memset (&last_key6, 0xff, sizeof (last_key6));
103 last_key4.as_u64 = ~0;
105 rx_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
106 drop_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP;
108 while (n_left_from >= 2)
110 u32 sw_if_index0, sw_if_index1;
111 ip4_header_t *ip40, *ip41;
112 ip6_header_t *ip60, *ip61;
113 esp_header_t *esp0, *esp1;
115 u16 buf_adv0, buf_adv1;
117 ipsec_tunnel_if_t *t0, *t1;
118 ipsec4_tunnel_key_t key40, key41;
119 ipsec6_tunnel_key_t key60, key61;
121 if (n_left_from >= 4)
123 CLIB_PREFETCH (b[2], CLIB_CACHE_LINE_BYTES, STORE);
124 CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
125 CLIB_PREFETCH (b[3], CLIB_CACHE_LINE_BYTES, STORE);
126 CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
130 (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
132 (ip4_header_t *) (b[1]->data + vnet_buffer (b[1])->l3_hdr_offset);
136 ip60 = (ip6_header_t *) ip40;
137 ip61 = (ip6_header_t *) ip41;
138 esp0 = (esp_header_t *) ((u8 *) ip60 + sizeof (ip6_header_t));
139 esp1 = (esp_header_t *) ((u8 *) ip61 + sizeof (ip6_header_t));
140 buf_adv0 = sizeof (ip6_header_t);
141 buf_adv1 = sizeof (ip6_header_t);
145 /* NAT UDP port 4500 case, don't advance any more */
146 if (ip40->protocol == IP_PROTOCOL_UDP)
149 (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40) +
150 sizeof (udp_header_t));
155 esp0 = (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40));
156 buf_adv0 = ip4_header_bytes (ip40);
158 /* NAT UDP port 4500 case, don't advance any more */
159 if (ip41->protocol == IP_PROTOCOL_UDP)
162 (esp_header_t *) ((u8 *) ip41 + ip4_header_bytes (ip41) +
163 sizeof (udp_header_t));
168 esp1 = (esp_header_t *) ((u8 *) ip41 + ip4_header_bytes (ip41));
169 buf_adv1 = ip4_header_bytes (ip41);
173 vlib_buffer_advance (b[0], buf_adv0);
174 vlib_buffer_advance (b[1], buf_adv1);
176 len0 = vlib_buffer_length_in_chain (vm, b[0]);
177 len1 = vlib_buffer_length_in_chain (vm, b[1]);
181 key60.remote_ip = ip60->src_address;
182 key60.spi = esp0->spi;
184 if (memcmp (&key60, &last_key6, sizeof (last_key6)) == 0)
186 tid0 = last_tunnel_id;
191 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key60);
195 last_tunnel_id = tid0;
196 clib_memcpy_fast (&last_key6, &key60, sizeof (key60));
201 next[0] = IPSEC_INPUT_NEXT_DROP;
208 key40.remote_ip = ip40->src_address.as_u32;
209 key40.spi = esp0->spi;
211 if (key40.as_u64 == last_key4.as_u64)
213 tid0 = last_tunnel_id;
218 hash_get (im->ipsec4_if_pool_index_by_key, key40.as_u64);
222 last_tunnel_id = tid0;
223 last_key4.as_u64 = key40.as_u64;
228 next[0] = IPSEC_INPUT_NEXT_DROP;
234 t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
235 vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
237 if (PREDICT_TRUE (t0->hw_if_index != ~0))
239 vnet_buffer (b[0])->ipsec.flags = 0;
240 sw_if_index0 = t0->sw_if_index;
241 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
243 if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
245 vlib_increment_combined_counter
246 (drop_counter, thread_index, sw_if_index0, 1, len0);
248 next[0] = IPSEC_INPUT_NEXT_DROP;
252 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
261 vlib_increment_combined_counter
262 (rx_counter, thread_index, last_sw_if_index,
266 last_sw_if_index = sw_if_index0;
273 vnet_buffer (b[0])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
279 key61.remote_ip = ip61->src_address;
280 key61.spi = esp1->spi;
282 if (memcmp (&key61, &last_key6, sizeof (last_key6)) == 0)
284 tid1 = last_tunnel_id;
289 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key61);
293 last_tunnel_id = tid1;
294 clib_memcpy_fast (&last_key6, &key61, sizeof (key61));
299 next[1] = IPSEC_INPUT_NEXT_DROP;
306 key41.remote_ip = ip41->src_address.as_u32;
307 key41.spi = esp1->spi;
309 if (key41.as_u64 == last_key4.as_u64)
311 tid1 = last_tunnel_id;
316 hash_get (im->ipsec4_if_pool_index_by_key, key41.as_u64);
320 last_tunnel_id = tid1;
321 last_key4.as_u64 = key41.as_u64;
326 next[1] = IPSEC_INPUT_NEXT_DROP;
332 t1 = pool_elt_at_index (im->tunnel_interfaces, tid1);
333 vnet_buffer (b[1])->ipsec.sad_index = t1->input_sa_index;
335 if (PREDICT_TRUE (t1->hw_if_index != ~0))
337 vnet_buffer (b[1])->ipsec.flags = 0;
338 sw_if_index1 = t1->sw_if_index;
339 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = sw_if_index1;
341 if (PREDICT_FALSE (!(t1->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
343 vlib_increment_combined_counter
344 (drop_counter, thread_index, sw_if_index1, 1, len1);
346 next[1] = IPSEC_INPUT_NEXT_DROP;
350 if (PREDICT_TRUE (sw_if_index1 == last_sw_if_index))
359 vlib_increment_combined_counter
360 (rx_counter, thread_index, last_sw_if_index,
364 last_sw_if_index = sw_if_index1;
371 vnet_buffer (b[1])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
375 if (PREDICT_FALSE (is_trace))
377 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
379 ipsec_if_input_trace_t *tr =
380 vlib_add_trace (vm, node, b[0], sizeof (*tr));
381 tr->spi = clib_host_to_net_u32 (esp0->spi);
382 tr->seq = clib_host_to_net_u32 (esp0->seq);
384 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
386 ipsec_if_input_trace_t *tr =
387 vlib_add_trace (vm, node, b[1], sizeof (*tr));
388 tr->spi = clib_host_to_net_u32 (esp1->spi);
389 tr->seq = clib_host_to_net_u32 (esp1->seq);
398 while (n_left_from > 0)
407 ipsec_tunnel_if_t *t0;
408 ipsec4_tunnel_key_t key40;
409 ipsec6_tunnel_key_t key60;
412 (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
416 ip60 = (ip6_header_t *) ip40;
417 esp0 = (esp_header_t *) ((u8 *) ip60 + sizeof (ip6_header_t));
418 buf_adv0 = sizeof (ip6_header_t);
422 /* NAT UDP port 4500 case, don't advance any more */
423 if (ip40->protocol == IP_PROTOCOL_UDP)
426 (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40) +
427 sizeof (udp_header_t));
432 esp0 = (esp_header_t *) ((u8 *) ip40 + ip4_header_bytes (ip40));
433 buf_adv0 = ip4_header_bytes (ip40);
437 /* stats for the tunnel include all the data after the IP header
438 just like a norml IP-IP tunnel */
439 vlib_buffer_advance (b[0], buf_adv0);
440 len0 = vlib_buffer_length_in_chain (vm, b[0]);
444 key60.remote_ip = ip60->src_address;
445 key60.spi = esp0->spi;
447 if (memcmp (&key60, &last_key6, sizeof (last_key6)) == 0)
449 tid0 = last_tunnel_id;
454 hash_get_mem (im->ipsec6_if_pool_index_by_key, &key60);
458 last_tunnel_id = tid0;
459 clib_memcpy_fast (&last_key6, &key60, sizeof (key60));
464 next[0] = IPSEC_INPUT_NEXT_DROP;
471 key40.remote_ip = ip40->src_address.as_u32;
472 key40.spi = esp0->spi;
474 if (key40.as_u64 == last_key4.as_u64)
476 tid0 = last_tunnel_id;
481 hash_get (im->ipsec4_if_pool_index_by_key, key40.as_u64);
485 last_tunnel_id = tid0;
486 last_key4.as_u64 = key40.as_u64;
491 next[0] = IPSEC_INPUT_NEXT_DROP;
497 t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
498 vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
500 if (PREDICT_TRUE (t0->hw_if_index != ~0))
502 vnet_buffer (b[0])->ipsec.flags = 0;
503 sw_if_index0 = t0->sw_if_index;
504 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
506 if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
508 vlib_increment_combined_counter
509 (drop_counter, thread_index, sw_if_index0, 1, len0);
511 next[0] = IPSEC_INPUT_NEXT_DROP;
515 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
524 vlib_increment_combined_counter
525 (rx_counter, thread_index, last_sw_if_index,
529 last_sw_if_index = sw_if_index0;
536 vnet_buffer (b[0])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
540 if (PREDICT_FALSE (is_trace))
542 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
544 ipsec_if_input_trace_t *tr =
545 vlib_add_trace (vm, node, b[0], sizeof (*tr));
546 tr->spi = clib_host_to_net_u32 (esp0->spi);
547 tr->seq = clib_host_to_net_u32 (esp0->seq);
559 vlib_increment_combined_counter (rx_counter,
561 last_sw_if_index, n_packets, n_bytes);
564 vlib_node_increment_counter (vm, node->node_index,
565 IPSEC_IF_INPUT_ERROR_RX,
566 from_frame->n_vectors - n_disabled);
567 vlib_node_increment_counter (vm, node->node_index,
568 IPSEC_IF_INPUT_ERROR_DISABLED, n_disabled);
569 vlib_node_increment_counter (vm, node->node_index,
570 IPSEC_IF_INPUT_ERROR_NO_TUNNEL, n_no_tunnel);
572 vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
574 return from_frame->n_vectors;
577 VLIB_NODE_FN (ipsec4_if_input_node) (vlib_main_t * vm,
578 vlib_node_runtime_t * node,
579 vlib_frame_t * from_frame)
581 return ipsec_if_input_inline (vm, node, from_frame, 0 /* is_ip6 */ );
585 VLIB_REGISTER_NODE (ipsec4_if_input_node) = {
586 .name = "ipsec4-if-input",
587 .vector_size = sizeof (u32),
588 .format_trace = format_ipsec_if_input_trace,
589 .type = VLIB_NODE_TYPE_INTERNAL,
590 .n_errors = ARRAY_LEN(ipsec_if_input_error_strings),
591 .error_strings = ipsec_if_input_error_strings,
592 .sibling_of = "ipsec4-input-feature",
596 VLIB_NODE_FN (ipsec6_if_input_node) (vlib_main_t * vm,
597 vlib_node_runtime_t * node,
598 vlib_frame_t * from_frame)
600 return ipsec_if_input_inline (vm, node, from_frame, 1 /* is_ip6 */ );
604 VLIB_REGISTER_NODE (ipsec6_if_input_node) = {
605 .name = "ipsec6-if-input",
606 .vector_size = sizeof (u32),
607 .format_trace = format_ipsec_if_input_trace,
608 .type = VLIB_NODE_TYPE_INTERNAL,
609 .n_errors = ARRAY_LEN(ipsec_if_input_error_strings),
610 .error_strings = ipsec_if_input_error_strings,
611 .sibling_of = "ipsec6-input-feature",
616 * fd.io coding-style-patch-verification: ON
619 * eval: (c-set-style "gnu")