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,
67 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
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;
96 vlib_combined_counter_main_t *rx_counter;
97 vlib_combined_counter_main_t *drop_counter;
99 rx_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
100 drop_counter = vim->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP;
102 while (n_left_from >= 2)
104 u32 sw_if_index0, sw_if_index1;
105 ip4_header_t *ip0, *ip1;
106 esp_header_t *esp0, *esp1;
108 u16 buf_adv0, buf_adv1;
110 ipsec_tunnel_if_t *t0, *t1;
113 if (n_left_from >= 4)
115 CLIB_PREFETCH (b[2], CLIB_CACHE_LINE_BYTES, STORE);
116 CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
117 CLIB_PREFETCH (b[3], CLIB_CACHE_LINE_BYTES, STORE);
118 CLIB_PREFETCH (b[3]->data, CLIB_CACHE_LINE_BYTES, LOAD);
121 ip0 = (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
122 ip1 = (ip4_header_t *) (b[1]->data + vnet_buffer (b[1])->l3_hdr_offset);
124 /* NAT UDP port 4500 case, don't advance any more */
125 if (ip0->protocol == IP_PROTOCOL_UDP)
128 (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0) +
129 sizeof (udp_header_t));
134 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
135 buf_adv0 = ip4_header_bytes (ip0);
137 if (ip1->protocol == IP_PROTOCOL_UDP)
140 (esp_header_t *) ((u8 *) ip1 + ip4_header_bytes (ip1) +
141 sizeof (udp_header_t));
146 esp1 = (esp_header_t *) ((u8 *) ip1 + ip4_header_bytes (ip1));
147 buf_adv1 = ip4_header_bytes (ip1);
150 vlib_buffer_advance (b[0], buf_adv0);
151 vlib_buffer_advance (b[1], buf_adv1);
153 len0 = vlib_buffer_length_in_chain (vm, b[0]);
154 len1 = vlib_buffer_length_in_chain (vm, b[1]);
156 key0 = (u64) ip0->src_address.as_u32 << 32 | (u64) esp0->spi;
157 key1 = (u64) ip1->src_address.as_u32 << 32 | (u64) esp1->spi;
159 if (key0 == last_key)
161 tid0 = last_tunnel_id;
165 uword *p = hash_get (im->ipsec_if_pool_index_by_key, key0);
169 last_tunnel_id = tid0;
175 next[0] = IPSEC_INPUT_NEXT_DROP;
180 t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
181 vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
183 if (PREDICT_TRUE (t0->hw_if_index != ~0))
185 vnet_buffer (b[0])->ipsec.flags = 0;
186 sw_if_index0 = t0->sw_if_index;
187 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
189 if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
191 vlib_increment_combined_counter
192 (drop_counter, thread_index, sw_if_index0, 1, len0);
194 next[0] = IPSEC_INPUT_NEXT_DROP;
198 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
207 vlib_increment_combined_counter
208 (rx_counter, thread_index, last_sw_if_index,
212 last_sw_if_index = sw_if_index0;
219 vnet_buffer (b[0])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
223 if (key1 == last_key)
225 tid1 = last_tunnel_id;
229 uword *p = hash_get (im->ipsec_if_pool_index_by_key, key1);
233 last_tunnel_id = tid1;
239 next[1] = IPSEC_INPUT_NEXT_DROP;
244 t1 = pool_elt_at_index (im->tunnel_interfaces, tid1);
245 vnet_buffer (b[1])->ipsec.sad_index = t1->input_sa_index;
247 if (PREDICT_TRUE (t1->hw_if_index != ~0))
249 vnet_buffer (b[1])->ipsec.flags = 0;
250 sw_if_index1 = t1->sw_if_index;
251 vnet_buffer (b[1])->sw_if_index[VLIB_RX] = sw_if_index1;
253 if (PREDICT_FALSE (!(t1->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
255 vlib_increment_combined_counter
256 (drop_counter, thread_index, sw_if_index1, 1, len1);
258 next[1] = IPSEC_INPUT_NEXT_DROP;
262 if (PREDICT_TRUE (sw_if_index1 == last_sw_if_index))
271 vlib_increment_combined_counter
272 (rx_counter, thread_index, last_sw_if_index,
276 last_sw_if_index = sw_if_index1;
283 vnet_buffer (b[1])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
289 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
291 ipsec_if_input_trace_t *tr =
292 vlib_add_trace (vm, node, b[0], sizeof (*tr));
293 tr->spi = clib_host_to_net_u32 (esp0->spi);
294 tr->seq = clib_host_to_net_u32 (esp0->seq);
296 if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
298 ipsec_if_input_trace_t *tr =
299 vlib_add_trace (vm, node, b[1], sizeof (*tr));
300 tr->spi = clib_host_to_net_u32 (esp1->spi);
301 tr->seq = clib_host_to_net_u32 (esp1->seq);
310 while (n_left_from > 0)
318 ipsec_tunnel_if_t *t0;
321 ip0 = (ip4_header_t *) (b[0]->data + vnet_buffer (b[0])->l3_hdr_offset);
323 /* NAT UDP port 4500 case, don't advance any more */
324 if (ip0->protocol == IP_PROTOCOL_UDP)
327 (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0) +
328 sizeof (udp_header_t));
333 esp0 = (esp_header_t *) ((u8 *) ip0 + ip4_header_bytes (ip0));
334 buf_adv0 = ip4_header_bytes (ip0);
337 /* stats for the tunnel include all the data after the IP header
338 just like a norml IP-IP tunnel */
339 vlib_buffer_advance (b[0], buf_adv0);
340 len0 = vlib_buffer_length_in_chain (vm, b[0]);
342 key0 = (u64) ip0->src_address.as_u32 << 32 | (u64) esp0->spi;
343 if (key0 == last_key)
345 tid0 = last_tunnel_id;
349 uword *p = hash_get (im->ipsec_if_pool_index_by_key, key0);
353 last_tunnel_id = tid0;
359 next[0] = IPSEC_INPUT_NEXT_DROP;
364 t0 = pool_elt_at_index (im->tunnel_interfaces, tid0);
365 vnet_buffer (b[0])->ipsec.sad_index = t0->input_sa_index;
367 if (PREDICT_TRUE (t0->hw_if_index != ~0))
369 vnet_buffer (b[0])->ipsec.flags = 0;
370 sw_if_index0 = t0->sw_if_index;
371 vnet_buffer (b[0])->sw_if_index[VLIB_RX] = sw_if_index0;
373 if (PREDICT_FALSE (!(t0->flags & VNET_HW_INTERFACE_FLAG_LINK_UP)))
375 vlib_increment_combined_counter
376 (drop_counter, thread_index, sw_if_index0, 1, len0);
378 next[0] = IPSEC_INPUT_NEXT_DROP;
382 if (PREDICT_TRUE (sw_if_index0 == last_sw_if_index))
391 vlib_increment_combined_counter
392 (rx_counter, thread_index, last_sw_if_index,
396 last_sw_if_index = sw_if_index0;
403 vnet_buffer (b[0])->ipsec.flags = IPSEC_FLAG_IPSEC_GRE_TUNNEL;
409 if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
411 ipsec_if_input_trace_t *tr =
412 vlib_add_trace (vm, node, b[0], sizeof (*tr));
413 tr->spi = clib_host_to_net_u32 (esp0->spi);
414 tr->seq = clib_host_to_net_u32 (esp0->seq);
426 vlib_increment_combined_counter (rx_counter,
428 last_sw_if_index, n_packets, n_bytes);
431 vlib_node_increment_counter (vm, ipsec_if_input_node.index,
432 IPSEC_IF_INPUT_ERROR_RX,
433 from_frame->n_vectors - n_disabled);
434 vlib_node_increment_counter (vm, ipsec_if_input_node.index,
435 IPSEC_IF_INPUT_ERROR_DISABLED, n_disabled);
436 vlib_node_increment_counter (vm, ipsec_if_input_node.index,
437 IPSEC_IF_INPUT_ERROR_NO_TUNNEL, n_no_tunnel);
439 vlib_buffer_enqueue_to_next (vm, node, from, nexts, from_frame->n_vectors);
441 return from_frame->n_vectors;
444 VLIB_NODE_FN (ipsec_if_input_node) (vlib_main_t * vm,
445 vlib_node_runtime_t * node,
446 vlib_frame_t * from_frame)
448 return ipsec_if_input_inline (vm, node, from_frame);
452 VLIB_REGISTER_NODE (ipsec_if_input_node) = {
453 .name = "ipsec-if-input",
454 .vector_size = sizeof (u32),
455 .format_trace = format_ipsec_if_input_trace,
456 .type = VLIB_NODE_TYPE_INTERNAL,
457 .n_errors = ARRAY_LEN(ipsec_if_input_error_strings),
458 .error_strings = ipsec_if_input_error_strings,
459 .sibling_of = "ipsec4-input-feature",
464 * fd.io coding-style-patch-verification: ON
467 * eval: (c-set-style "gnu")