blob: 66286094682e8fbaf2e7377eff43e6b75cc4d334 [file] [log] [blame]
“mukeshyadav1984”430ac932017-11-23 02:39:33 -08001/*
2 * ah_encrypt.c : IPSec AH encrypt 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/ah.h>
25
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020026#define foreach_ah_encrypt_next \
27 _ (DROP, "error-drop") \
28 _ (IP4_LOOKUP, "ip4-lookup") \
29 _ (IP6_LOOKUP, "ip6-lookup") \
30 _ (INTERFACE_OUTPUT, "interface-output")
31
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080032
33#define _(v, s) AH_ENCRYPT_NEXT_##v,
34typedef enum
35{
36 foreach_ah_encrypt_next
37#undef _
38 AH_ENCRYPT_N_NEXT,
39} ah_encrypt_next_t;
40
41#define foreach_ah_encrypt_error \
42 _(RX_PKTS, "AH pkts received") \
43 _(SEQ_CYCLED, "sequence number cycled")
44
45
46typedef enum
47{
48#define _(sym,str) AH_ENCRYPT_ERROR_##sym,
49 foreach_ah_encrypt_error
50#undef _
51 AH_ENCRYPT_N_ERROR,
52} ah_encrypt_error_t;
53
54static char *ah_encrypt_error_strings[] = {
55#define _(sym,string) string,
56 foreach_ah_encrypt_error
57#undef _
58};
59
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080060typedef struct
61{
Neale Ranns8d7c5022019-02-06 01:41:05 -080062 u32 sa_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080063 u32 spi;
64 u32 seq;
65 ipsec_integ_alg_t integ_alg;
66} ah_encrypt_trace_t;
67
68/* packet trace format function */
69static u8 *
70format_ah_encrypt_trace (u8 * s, va_list * args)
71{
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 *);
75
Neale Ranns8d7c5022019-02-06 01:41:05 -080076 s = format (s, "ah: sa-index %d spi %u seq %u integrity %U",
77 t->sa_index, t->spi, t->seq,
78 format_ipsec_integ_alg, t->integ_alg);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080079 return s;
80}
81
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020082always_inline uword
83ah_encrypt_inline (vlib_main_t * vm,
84 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
85 int is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080086{
87 u32 n_left_from, *from, *to_next = 0, next_index;
88 int icv_size = 0;
89 from = vlib_frame_vector_args (from_frame);
90 n_left_from = from_frame->n_vectors;
91 ipsec_main_t *im = &ipsec_main;
92 ipsec_proto_main_t *em = &ipsec_proto_main;
93 next_index = node->cached_next_index;
94
95 while (n_left_from > 0)
96 {
97 u32 n_left_to_next;
98
99 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100
101 while (n_left_from > 0 && n_left_to_next > 0)
102 {
103 u32 i_bi0, next0;
104 vlib_buffer_t *i_b0 = 0;
105 u32 sa_index0;
106 ipsec_sa_t *sa0;
107 ip4_and_ah_header_t *ih0, *oh0 = 0;
108 ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800109 u8 ip_hdr_size;
110 u8 next_hdr_type;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800111 u8 tos = 0;
112 u8 ttl = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200113 u8 hop_limit = 0;
114 u32 ip_version_traffic_class_and_flow_label = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800115
116 i_bi0 = from[0];
117 from += 1;
118 n_left_from -= 1;
119 n_left_to_next -= 1;
120 next0 = AH_ENCRYPT_NEXT_DROP;
121
122 i_b0 = vlib_get_buffer (vm, i_bi0);
123 to_next[0] = i_bi0;
124 to_next += 1;
125 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
126 sa0 = pool_elt_at_index (im->sad, sa_index0);
127
128 if (PREDICT_FALSE (esp_seq_advance (sa0)))
129 {
Klement Sekera2e02ba02018-11-30 14:37:03 +0100130 vlib_node_increment_counter (vm, node->node_index,
131 AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800132 goto trace;
133 }
134
135
136 sa0->total_data_size += i_b0->current_length;
137
138 ssize_t adv;
139 ih0 = vlib_buffer_get_current (i_b0);
140 ttl = ih0->ip4.ttl;
141 tos = ih0->ip4.tos;
142
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800143 if (PREDICT_TRUE (sa0->is_tunnel))
144 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200145 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800146 adv = -sizeof (ip6_and_ah_header_t);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200147 else
148 adv = -sizeof (ip4_and_ah_header_t);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800149 }
150 else
151 {
152 adv = -sizeof (ah_header_t);
153 }
154
155 icv_size =
156 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200157 const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
Klement Sekera611864f2018-09-26 11:19:00 +0200158 adv -= padding_len;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200159 /* transport mode save the eth header before it is overwritten */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800160 if (PREDICT_FALSE (!sa0->is_tunnel))
161 {
162 ethernet_header_t *ieh0 = (ethernet_header_t *)
163 ((u8 *) vlib_buffer_get_current (i_b0) -
164 sizeof (ethernet_header_t));
165 ethernet_header_t *oeh0 =
166 (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
Dave Barach178cf492018-11-13 16:34:13 -0500167 clib_memcpy_fast (oeh0, ieh0, sizeof (ethernet_header_t));
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800168 }
169
170 vlib_buffer_advance (i_b0, adv - icv_size);
171
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200172 if (is_ip6)
173 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800174 ih6_0 = (ip6_and_ah_header_t *) ih0;
175 ip_hdr_size = sizeof (ip6_header_t);
176 oh6_0 = vlib_buffer_get_current (i_b0);
177
Klement Sekera611864f2018-09-26 11:19:00 +0200178 hop_limit = ih6_0->ip6.hop_limit;
179 ip_version_traffic_class_and_flow_label =
180 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800181 if (PREDICT_TRUE (sa0->is_tunnel))
182 {
183 next_hdr_type = IP_PROTOCOL_IPV6;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800184 }
185 else
186 {
187 next_hdr_type = ih6_0->ip6.protocol;
188 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
189 }
190
191 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
Klement Sekera611864f2018-09-26 11:19:00 +0200192 oh6_0->ip6.hop_limit = 0;
193 oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0x60;
194 oh6_0->ah.reserved = 0;
195 oh6_0->ah.nexthdr = next_hdr_type;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800196 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));
Klement Sekera611864f2018-09-26 11:19:00 +0200201 oh6_0->ah.hdrlen =
202 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800203 }
204 else
205 {
206 ip_hdr_size = sizeof (ip4_header_t);
207 oh0 = vlib_buffer_get_current (i_b0);
Dave Barachb7b92992018-10-17 10:38:51 -0400208 clib_memset (oh0, 0, sizeof (ip4_and_ah_header_t));
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800209
210 if (PREDICT_TRUE (sa0->is_tunnel))
211 {
212 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
213 }
214 else
215 {
216 next_hdr_type = ih0->ip4.protocol;
217 memmove (oh0, ih0, sizeof (ip4_header_t));
218 }
219
220 oh0->ip4.length =
221 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
222 oh0->ip4.ip_version_and_header_length = 0x45;
223 oh0->ip4.fragment_id = 0;
224 oh0->ip4.flags_and_fragment_offset = 0;
225 oh0->ip4.ttl = 0;
226 oh0->ip4.tos = 0;
227 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
228 oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
229 oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
230 oh0->ip4.checksum = 0;
231 oh0->ah.nexthdr = next_hdr_type;
Klement Sekera611864f2018-09-26 11:19:00 +0200232 oh0->ah.hdrlen =
233 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800234 }
235
236
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200237 if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800238 {
239 oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
240 oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
241
Neale Ranns8d7c5022019-02-06 01:41:05 -0800242 next0 = sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_next_node;
243 vnet_buffer (i_b0)->ip.adj_index[VLIB_TX] =
244 sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800245 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200246 else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800247 {
248 oh6_0->ip6.src_address.as_u64[0] =
249 sa0->tunnel_src_addr.ip6.as_u64[0];
250 oh6_0->ip6.src_address.as_u64[1] =
251 sa0->tunnel_src_addr.ip6.as_u64[1];
252 oh6_0->ip6.dst_address.as_u64[0] =
253 sa0->tunnel_dst_addr.ip6.as_u64[0];
254 oh6_0->ip6.dst_address.as_u64[1] =
255 sa0->tunnel_dst_addr.ip6.as_u64[1];
256
Neale Ranns8d7c5022019-02-06 01:41:05 -0800257 next0 = sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_next_node;
258 vnet_buffer (i_b0)->ip.adj_index[VLIB_TX] =
259 sa0->dpo[IPSEC_PROTOCOL_AH].dpoi_index;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800260 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800261
262 u8 sig[64];
Dave Barachb7b92992018-10-17 10:38:51 -0400263 clib_memset (sig, 0, sizeof (sig));
Klement Sekera31da2e32018-06-24 22:49:55 +0200264 u8 *digest =
Klement Sekera611864f2018-09-26 11:19:00 +0200265 vlib_buffer_get_current (i_b0) + ip_hdr_size +
266 sizeof (ah_header_t);
Dave Barachb7b92992018-10-17 10:38:51 -0400267 clib_memset (digest, 0, icv_size);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800268
Neale Ranns8d7c5022019-02-06 01:41:05 -0800269 unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key.data,
270 sa0->integ_key.len,
Klement Sekera31da2e32018-06-24 22:49:55 +0200271 vlib_buffer_get_current (i_b0),
272 i_b0->current_length, sig, sa0->use_esn,
273 sa0->seq_hi);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800274
Klement Sekera31da2e32018-06-24 22:49:55 +0200275 memcpy (digest, sig, size);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200276 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800277 {
Klement Sekera611864f2018-09-26 11:19:00 +0200278 oh6_0->ip6.hop_limit = hop_limit;
279 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
280 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800281 }
282 else
283 {
284 oh0->ip4.ttl = ttl;
285 oh0->ip4.tos = tos;
286 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
287 }
288
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200289 if (!sa0->is_tunnel)
290 {
291 next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
292 vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));
293 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800294
295 trace:
296 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
297 {
298 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
299 ah_encrypt_trace_t *tr =
300 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
301 tr->spi = sa0->spi;
302 tr->seq = sa0->seq - 1;
303 tr->integ_alg = sa0->integ_alg;
Neale Ranns8d7c5022019-02-06 01:41:05 -0800304 tr->sa_index = sa_index0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800305 }
306
307 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
308 to_next, n_left_to_next, i_bi0,
309 next0);
310 }
311 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
312 }
Klement Sekera2e02ba02018-11-30 14:37:03 +0100313 vlib_node_increment_counter (vm, node->node_index,
314 AH_ENCRYPT_ERROR_RX_PKTS,
315 from_frame->n_vectors);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800316
317 return from_frame->n_vectors;
318}
319
Klement Sekerab8f35442018-10-29 13:38:19 +0100320VLIB_NODE_FN (ah4_encrypt_node) (vlib_main_t * vm,
321 vlib_node_runtime_t * node,
322 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200323{
324 return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
325}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800326
327/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200328VLIB_REGISTER_NODE (ah4_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200329 .name = "ah4-encrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800330 .vector_size = sizeof (u32),
331 .format_trace = format_ah_encrypt_trace,
332 .type = VLIB_NODE_TYPE_INTERNAL,
333
334 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
335 .error_strings = ah_encrypt_error_strings,
336
337 .n_next_nodes = AH_ENCRYPT_N_NEXT,
338 .next_nodes = {
339#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
340 foreach_ah_encrypt_next
341#undef _
342 },
343};
344/* *INDENT-ON* */
345
Klement Sekerab8f35442018-10-29 13:38:19 +0100346VLIB_NODE_FN (ah6_encrypt_node) (vlib_main_t * vm,
347 vlib_node_runtime_t * node,
348 vlib_frame_t * from_frame)
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200349{
350 return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
351}
352
353/* *INDENT-OFF* */
354VLIB_REGISTER_NODE (ah6_encrypt_node) = {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200355 .name = "ah6-encrypt",
356 .vector_size = sizeof (u32),
357 .format_trace = format_ah_encrypt_trace,
358 .type = VLIB_NODE_TYPE_INTERNAL,
359
360 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
361 .error_strings = ah_encrypt_error_strings,
362
363 .n_next_nodes = AH_ENCRYPT_N_NEXT,
364 .next_nodes = {
365#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
366 foreach_ah_encrypt_next
367#undef _
368 },
369};
370/* *INDENT-ON* */
371
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800372/*
373 * fd.io coding-style-patch-verification: ON
374 *
375 * Local Variables:
376 * eval: (c-set-style "gnu")
377 * End:
378 */