2 * ah_encrypt.c : IPSec AH encrypt 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/ah.h>
26 #define foreach_ah_encrypt_next \
27 _(DROP, "error-drop") \
28 _(IP4_LOOKUP, "ip4-lookup") \
29 _(IP6_LOOKUP, "ip6-lookup") \
30 _(INTERFACE_OUTPUT, "interface-output")
32 #define _(v, s) AH_ENCRYPT_NEXT_##v,
35 foreach_ah_encrypt_next
40 #define foreach_ah_encrypt_error \
41 _(RX_PKTS, "AH pkts received") \
42 _(SEQ_CYCLED, "sequence number cycled")
47 #define _(sym,str) AH_ENCRYPT_ERROR_##sym,
48 foreach_ah_encrypt_error
53 static char *ah_encrypt_error_strings[] = {
54 #define _(sym,string) string,
55 foreach_ah_encrypt_error
59 vlib_node_registration_t ah_encrypt_node;
65 ipsec_integ_alg_t integ_alg;
68 /* packet trace format function */
70 format_ah_encrypt_trace (u8 * s, va_list * args)
72 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
73 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
74 ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
76 s = format (s, "ah: spi %u seq %u integrity %U",
77 t->spi, t->seq, format_ipsec_integ_alg, t->integ_alg);
82 ah_encrypt_node_fn (vlib_main_t * vm,
83 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
85 u32 n_left_from, *from, *to_next = 0, next_index;
87 from = vlib_frame_vector_args (from_frame);
88 n_left_from = from_frame->n_vectors;
89 ipsec_main_t *im = &ipsec_main;
90 ipsec_proto_main_t *em = &ipsec_proto_main;
91 next_index = node->cached_next_index;
93 while (n_left_from > 0)
97 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
99 while (n_left_from > 0 && n_left_to_next > 0)
102 vlib_buffer_t *i_b0 = 0;
105 ip4_and_ah_header_t *ih0, *oh0 = 0;
106 ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
110 u8 transport_mode = 0;
118 next0 = AH_ENCRYPT_NEXT_DROP;
120 i_b0 = vlib_get_buffer (vm, i_bi0);
123 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
124 sa0 = pool_elt_at_index (im->sad, sa_index0);
126 if (PREDICT_FALSE (esp_seq_advance (sa0)))
128 clib_warning ("sequence number counter has cycled SPI %u",
130 vlib_node_increment_counter (vm, ah_encrypt_node.index,
131 AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
132 //TODO need to confirm if below is needed
139 sa0->total_data_size += i_b0->current_length;
142 ih0 = vlib_buffer_get_current (i_b0);
146 is_ipv6 = (ih0->ip4.ip_version_and_header_length & 0xF0) == 0x60;
148 if (PREDICT_TRUE (sa0->is_tunnel))
150 if (PREDICT_TRUE (!is_ipv6))
151 adv = -sizeof (ip4_and_ah_header_t);
153 adv = -sizeof (ip6_and_ah_header_t);
157 adv = -sizeof (ah_header_t);
161 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
162 /*transport mode save the eth header before it is overwritten */
163 if (PREDICT_FALSE (!sa0->is_tunnel))
165 ethernet_header_t *ieh0 = (ethernet_header_t *)
166 ((u8 *) vlib_buffer_get_current (i_b0) -
167 sizeof (ethernet_header_t));
168 ethernet_header_t *oeh0 =
169 (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
170 clib_memcpy (oeh0, ieh0, sizeof (ethernet_header_t));
173 vlib_buffer_advance (i_b0, adv - icv_size);
176 if (PREDICT_FALSE (is_ipv6))
178 ih6_0 = (ip6_and_ah_header_t *) ih0;
179 ip_hdr_size = sizeof (ip6_header_t);
180 oh6_0 = vlib_buffer_get_current (i_b0);
182 if (PREDICT_TRUE (sa0->is_tunnel))
184 next_hdr_type = IP_PROTOCOL_IPV6;
185 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
186 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
190 next_hdr_type = ih6_0->ip6.protocol;
191 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
194 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
195 oh6_0->ip6.hop_limit = 254;
196 oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
197 oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
198 oh6_0->ip6.payload_length =
199 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) -
200 sizeof (ip6_header_t));
204 ip_hdr_size = sizeof (ip4_header_t);
205 oh0 = vlib_buffer_get_current (i_b0);
206 memset (oh0, 0, sizeof (ip4_and_ah_header_t));
208 if (PREDICT_TRUE (sa0->is_tunnel))
210 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
214 next_hdr_type = ih0->ip4.protocol;
215 memmove (oh0, ih0, sizeof (ip4_header_t));
219 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
220 oh0->ip4.ip_version_and_header_length = 0x45;
221 oh0->ip4.fragment_id = 0;
222 oh0->ip4.flags_and_fragment_offset = 0;
225 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
226 oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
227 oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
228 oh0->ip4.checksum = 0;
229 oh0->ah.nexthdr = next_hdr_type;
236 (!is_ipv6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
238 oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
239 oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
241 next0 = AH_ENCRYPT_NEXT_IP4_LOOKUP;
242 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
244 else if (is_ipv6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
246 oh6_0->ip6.src_address.as_u64[0] =
247 sa0->tunnel_src_addr.ip6.as_u64[0];
248 oh6_0->ip6.src_address.as_u64[1] =
249 sa0->tunnel_src_addr.ip6.as_u64[1];
250 oh6_0->ip6.dst_address.as_u64[0] =
251 sa0->tunnel_dst_addr.ip6.as_u64[0];
252 oh6_0->ip6.dst_address.as_u64[1] =
253 sa0->tunnel_dst_addr.ip6.as_u64[1];
255 next0 = AH_ENCRYPT_NEXT_IP6_LOOKUP;
256 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
261 next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
265 memset (sig, 0, sizeof (sig));
268 digest = vlib_buffer_get_current (i_b0) + ip_hdr_size + icv_size;
269 memset (digest, 0, icv_size);
272 hmac_calc (sa0->integ_alg, sa0->integ_key,
274 (u8 *) vlib_buffer_get_current (i_b0),
275 i_b0->current_length, sig, sa0->use_esn, sa0->seq_hi);
277 memcpy (digest, (char *) &sig[0], 12);
279 if (PREDICT_FALSE (is_ipv6))
286 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
290 vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));;
293 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
295 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
296 ah_encrypt_trace_t *tr =
297 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
299 tr->seq = sa0->seq - 1;
300 tr->integ_alg = sa0->integ_alg;
303 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
304 to_next, n_left_to_next, i_bi0,
307 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
309 vlib_node_increment_counter (vm, ah_encrypt_node.index,
310 AH_ENCRYPT_ERROR_RX_PKTS,
311 from_frame->n_vectors);
313 return from_frame->n_vectors;
318 VLIB_REGISTER_NODE (ah_encrypt_node) = {
319 .function = ah_encrypt_node_fn,
320 .name = "ah-encrypt",
321 .vector_size = sizeof (u32),
322 .format_trace = format_ah_encrypt_trace,
323 .type = VLIB_NODE_TYPE_INTERNAL,
325 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
326 .error_strings = ah_encrypt_error_strings,
328 .n_next_nodes = AH_ENCRYPT_N_NEXT,
330 #define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
331 foreach_ah_encrypt_next
337 VLIB_NODE_FUNCTION_MULTIARCH (ah_encrypt_node, ah_encrypt_node_fn)
339 * fd.io coding-style-patch-verification: ON
342 * eval: (c-set-style "gnu")