blob: 856eab62c914664a7ba8550e5a1363752d016901 [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{
62 u32 spi;
63 u32 seq;
64 ipsec_integ_alg_t integ_alg;
65} ah_encrypt_trace_t;
66
67/* packet trace format function */
68static u8 *
69format_ah_encrypt_trace (u8 * s, va_list * args)
70{
71 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
72 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
73 ah_encrypt_trace_t *t = va_arg (*args, ah_encrypt_trace_t *);
74
75 s = format (s, "ah: spi %u seq %u integrity %U",
76 t->spi, t->seq, format_ipsec_integ_alg, t->integ_alg);
77 return s;
78}
79
Klement Sekerabe5a5dd2018-10-09 16:05:48 +020080always_inline uword
81ah_encrypt_inline (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * from_frame,
83 int is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -080084{
85 u32 n_left_from, *from, *to_next = 0, next_index;
86 int icv_size = 0;
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;
92
93 while (n_left_from > 0)
94 {
95 u32 n_left_to_next;
96
97 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
98
99 while (n_left_from > 0 && n_left_to_next > 0)
100 {
101 u32 i_bi0, next0;
102 vlib_buffer_t *i_b0 = 0;
103 u32 sa_index0;
104 ipsec_sa_t *sa0;
105 ip4_and_ah_header_t *ih0, *oh0 = 0;
106 ip6_and_ah_header_t *ih6_0, *oh6_0 = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800107 u8 ip_hdr_size;
108 u8 next_hdr_type;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800109 u8 tos = 0;
110 u8 ttl = 0;
Klement Sekera611864f2018-09-26 11:19:00 +0200111 u8 hop_limit = 0;
112 u32 ip_version_traffic_class_and_flow_label = 0;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800113
114 i_bi0 = from[0];
115 from += 1;
116 n_left_from -= 1;
117 n_left_to_next -= 1;
118 next0 = AH_ENCRYPT_NEXT_DROP;
119
120 i_b0 = vlib_get_buffer (vm, i_bi0);
121 to_next[0] = i_bi0;
122 to_next += 1;
123 sa_index0 = vnet_buffer (i_b0)->ipsec.sad_index;
124 sa0 = pool_elt_at_index (im->sad, sa_index0);
125
126 if (PREDICT_FALSE (esp_seq_advance (sa0)))
127 {
128 clib_warning ("sequence number counter has cycled SPI %u",
129 sa0->spi);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200130 if (is_ip6)
131 vlib_node_increment_counter (vm, ah6_encrypt_node.index,
132 AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
133 else
134 vlib_node_increment_counter (vm, ah4_encrypt_node.index,
135 AH_ENCRYPT_ERROR_SEQ_CYCLED, 1);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800136 //TODO need to confirm if below is needed
137 to_next[0] = i_bi0;
138 to_next += 1;
139 goto trace;
140 }
141
142
143 sa0->total_data_size += i_b0->current_length;
144
145 ssize_t adv;
146 ih0 = vlib_buffer_get_current (i_b0);
147 ttl = ih0->ip4.ttl;
148 tos = ih0->ip4.tos;
149
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800150 if (PREDICT_TRUE (sa0->is_tunnel))
151 {
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200152 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800153 adv = -sizeof (ip6_and_ah_header_t);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200154 else
155 adv = -sizeof (ip4_and_ah_header_t);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800156 }
157 else
158 {
159 adv = -sizeof (ah_header_t);
160 }
161
162 icv_size =
163 em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200164 const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
Klement Sekera611864f2018-09-26 11:19:00 +0200165 adv -= padding_len;
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200166 /* transport mode save the eth header before it is overwritten */
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800167 if (PREDICT_FALSE (!sa0->is_tunnel))
168 {
169 ethernet_header_t *ieh0 = (ethernet_header_t *)
170 ((u8 *) vlib_buffer_get_current (i_b0) -
171 sizeof (ethernet_header_t));
172 ethernet_header_t *oeh0 =
173 (ethernet_header_t *) ((u8 *) ieh0 + (adv - icv_size));
174 clib_memcpy (oeh0, ieh0, sizeof (ethernet_header_t));
175 }
176
177 vlib_buffer_advance (i_b0, adv - icv_size);
178
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200179 if (is_ip6)
180 {
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800181 ih6_0 = (ip6_and_ah_header_t *) ih0;
182 ip_hdr_size = sizeof (ip6_header_t);
183 oh6_0 = vlib_buffer_get_current (i_b0);
184
Klement Sekera611864f2018-09-26 11:19:00 +0200185 hop_limit = ih6_0->ip6.hop_limit;
186 ip_version_traffic_class_and_flow_label =
187 ih6_0->ip6.ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800188 if (PREDICT_TRUE (sa0->is_tunnel))
189 {
190 next_hdr_type = IP_PROTOCOL_IPV6;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800191 }
192 else
193 {
194 next_hdr_type = ih6_0->ip6.protocol;
195 memmove (oh6_0, ih6_0, sizeof (ip6_header_t));
196 }
197
198 oh6_0->ip6.protocol = IP_PROTOCOL_IPSEC_AH;
Klement Sekera611864f2018-09-26 11:19:00 +0200199 oh6_0->ip6.hop_limit = 0;
200 oh6_0->ip6.ip_version_traffic_class_and_flow_label = 0x60;
201 oh6_0->ah.reserved = 0;
202 oh6_0->ah.nexthdr = next_hdr_type;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800203 oh6_0->ah.spi = clib_net_to_host_u32 (sa0->spi);
204 oh6_0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
205 oh6_0->ip6.payload_length =
206 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0) -
207 sizeof (ip6_header_t));
Klement Sekera611864f2018-09-26 11:19:00 +0200208 oh6_0->ah.hdrlen =
209 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800210 }
211 else
212 {
213 ip_hdr_size = sizeof (ip4_header_t);
214 oh0 = vlib_buffer_get_current (i_b0);
215 memset (oh0, 0, sizeof (ip4_and_ah_header_t));
216
217 if (PREDICT_TRUE (sa0->is_tunnel))
218 {
219 next_hdr_type = IP_PROTOCOL_IP_IN_IP;
220 }
221 else
222 {
223 next_hdr_type = ih0->ip4.protocol;
224 memmove (oh0, ih0, sizeof (ip4_header_t));
225 }
226
227 oh0->ip4.length =
228 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, i_b0));
229 oh0->ip4.ip_version_and_header_length = 0x45;
230 oh0->ip4.fragment_id = 0;
231 oh0->ip4.flags_and_fragment_offset = 0;
232 oh0->ip4.ttl = 0;
233 oh0->ip4.tos = 0;
234 oh0->ip4.protocol = IP_PROTOCOL_IPSEC_AH;
235 oh0->ah.spi = clib_net_to_host_u32 (sa0->spi);
236 oh0->ah.seq_no = clib_net_to_host_u32 (sa0->seq);
237 oh0->ip4.checksum = 0;
238 oh0->ah.nexthdr = next_hdr_type;
Klement Sekera611864f2018-09-26 11:19:00 +0200239 oh0->ah.hdrlen =
240 (sizeof (ah_header_t) + icv_size + padding_len) / 4 - 2;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800241 }
242
243
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200244 if (PREDICT_TRUE (!is_ip6 && sa0->is_tunnel && !sa0->is_tunnel_ip6))
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800245 {
246 oh0->ip4.src_address.as_u32 = sa0->tunnel_src_addr.ip4.as_u32;
247 oh0->ip4.dst_address.as_u32 = sa0->tunnel_dst_addr.ip4.as_u32;
248
249 next0 = AH_ENCRYPT_NEXT_IP4_LOOKUP;
250 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
251 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200252 else if (is_ip6 && sa0->is_tunnel && sa0->is_tunnel_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800253 {
254 oh6_0->ip6.src_address.as_u64[0] =
255 sa0->tunnel_src_addr.ip6.as_u64[0];
256 oh6_0->ip6.src_address.as_u64[1] =
257 sa0->tunnel_src_addr.ip6.as_u64[1];
258 oh6_0->ip6.dst_address.as_u64[0] =
259 sa0->tunnel_dst_addr.ip6.as_u64[0];
260 oh6_0->ip6.dst_address.as_u64[1] =
261 sa0->tunnel_dst_addr.ip6.as_u64[1];
262
263 next0 = AH_ENCRYPT_NEXT_IP6_LOOKUP;
264 vnet_buffer (i_b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
265 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800266
267 u8 sig[64];
268 memset (sig, 0, sizeof (sig));
Klement Sekera31da2e32018-06-24 22:49:55 +0200269 u8 *digest =
Klement Sekera611864f2018-09-26 11:19:00 +0200270 vlib_buffer_get_current (i_b0) + ip_hdr_size +
271 sizeof (ah_header_t);
Klement Sekera31da2e32018-06-24 22:49:55 +0200272 memset (digest, 0, icv_size);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800273
Klement Sekera31da2e32018-06-24 22:49:55 +0200274 unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key,
275 sa0->integ_key_len,
276 vlib_buffer_get_current (i_b0),
277 i_b0->current_length, sig, sa0->use_esn,
278 sa0->seq_hi);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800279
Klement Sekera31da2e32018-06-24 22:49:55 +0200280 memcpy (digest, sig, size);
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200281 if (is_ip6)
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800282 {
Klement Sekera611864f2018-09-26 11:19:00 +0200283 oh6_0->ip6.hop_limit = hop_limit;
284 oh6_0->ip6.ip_version_traffic_class_and_flow_label =
285 ip_version_traffic_class_and_flow_label;
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800286 }
287 else
288 {
289 oh0->ip4.ttl = ttl;
290 oh0->ip4.tos = tos;
291 oh0->ip4.checksum = ip4_header_checksum (&oh0->ip4);
292 }
293
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200294 if (!sa0->is_tunnel)
295 {
296 next0 = AH_ENCRYPT_NEXT_INTERFACE_OUTPUT;
297 vlib_buffer_advance (i_b0, -sizeof (ethernet_header_t));
298 }
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800299
300 trace:
301 if (PREDICT_FALSE (i_b0->flags & VLIB_BUFFER_IS_TRACED))
302 {
303 i_b0->flags |= VLIB_BUFFER_IS_TRACED;
304 ah_encrypt_trace_t *tr =
305 vlib_add_trace (vm, node, i_b0, sizeof (*tr));
306 tr->spi = sa0->spi;
307 tr->seq = sa0->seq - 1;
308 tr->integ_alg = sa0->integ_alg;
309 }
310
311 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
312 to_next, n_left_to_next, i_bi0,
313 next0);
314 }
315 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
316 }
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200317 if (is_ip6)
318 vlib_node_increment_counter (vm, ah6_encrypt_node.index,
319 AH_ENCRYPT_ERROR_RX_PKTS,
320 from_frame->n_vectors);
321 else
322 vlib_node_increment_counter (vm, ah4_encrypt_node.index,
323 AH_ENCRYPT_ERROR_RX_PKTS,
324 from_frame->n_vectors);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800325
326 return from_frame->n_vectors;
327}
328
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200329static uword
330ah4_encrypt_node_fn (vlib_main_t * vm,
331 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
332{
333 return ah_encrypt_inline (vm, node, from_frame, 0 /* is_ip6 */ );
334}
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800335
336/* *INDENT-OFF* */
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200337VLIB_REGISTER_NODE (ah4_encrypt_node) = {
338 .function = ah4_encrypt_node_fn,
339 .name = "ah4-encrypt",
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800340 .vector_size = sizeof (u32),
341 .format_trace = format_ah_encrypt_trace,
342 .type = VLIB_NODE_TYPE_INTERNAL,
343
344 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
345 .error_strings = ah_encrypt_error_strings,
346
347 .n_next_nodes = AH_ENCRYPT_N_NEXT,
348 .next_nodes = {
349#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
350 foreach_ah_encrypt_next
351#undef _
352 },
353};
354/* *INDENT-ON* */
355
Klement Sekerabe5a5dd2018-10-09 16:05:48 +0200356VLIB_NODE_FUNCTION_MULTIARCH (ah4_encrypt_node, ah4_encrypt_node_fn);
357
358static uword
359ah6_encrypt_node_fn (vlib_main_t * vm,
360 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
361{
362 return ah_encrypt_inline (vm, node, from_frame, 1 /* is_ip6 */ );
363}
364
365/* *INDENT-OFF* */
366VLIB_REGISTER_NODE (ah6_encrypt_node) = {
367 .function = ah6_encrypt_node_fn,
368 .name = "ah6-encrypt",
369 .vector_size = sizeof (u32),
370 .format_trace = format_ah_encrypt_trace,
371 .type = VLIB_NODE_TYPE_INTERNAL,
372
373 .n_errors = ARRAY_LEN(ah_encrypt_error_strings),
374 .error_strings = ah_encrypt_error_strings,
375
376 .n_next_nodes = AH_ENCRYPT_N_NEXT,
377 .next_nodes = {
378#define _(s,n) [AH_ENCRYPT_NEXT_##s] = n,
379 foreach_ah_encrypt_next
380#undef _
381 },
382};
383/* *INDENT-ON* */
384
385VLIB_NODE_FUNCTION_MULTIARCH (ah6_encrypt_node, ah6_encrypt_node_fn);
“mukeshyadav1984”430ac932017-11-23 02:39:33 -0800386/*
387 * fd.io coding-style-patch-verification: ON
388 *
389 * Local Variables:
390 * eval: (c-set-style "gnu")
391 * End:
392 */