blob: e4f5e8052894f70147e8794cddb360fae9868ddc [file] [log] [blame]
Matus Fabian694265d2016-08-10 01:55:36 -07001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/**
Chris Luke16bcf7d2016-09-01 14:31:46 -040016 * @file
Matus Fabian694265d2016-08-10 01:55:36 -070017 * @brief L2-GRE over IPSec packet processing.
18 *
19 * Add GRE header to thr packet and send it to the esp-encrypt node.
20*/
21
22#include <vnet/vnet.h>
23#include <vnet/ipsec-gre/ipsec_gre.h>
24
Filip Tehlaraee73642019-03-13 05:50:44 -070025extern ipsec_gre_main_t ipsec_gre_main;
26
27#ifndef CLIB_MARCH_VARIANT
Matus Fabian694265d2016-08-10 01:55:36 -070028ipsec_gre_main_t ipsec_gre_main;
Filip Tehlaraee73642019-03-13 05:50:44 -070029#endif /* CLIB_MARCH_VARIANT */
Matus Fabian694265d2016-08-10 01:55:36 -070030
31/**
Matus Fabian694265d2016-08-10 01:55:36 -070032 * @brief IPv4 and GRE header union.
33 *
34*/
35typedef struct
36{
37 union
38 {
39 ip4_and_gre_header_t ip4_and_gre;
40 u64 as_u64[3];
41 };
42} ip4_and_gre_union_t;
43
44/**
45 * @brief Packet trace.
46 *
47*/
48typedef struct
49{
50 u32 tunnel_id; /**< Tunnel-id / index in tunnel vector */
51
52 u32 length; /**< pkt length */
53
54 ip4_address_t src; /**< tunnel src IPv4 address */
55 ip4_address_t dst; /**< tunnel dst IPv4 address */
56
57 u32 sa_id; /**< tunnel IPSec SA id */
58} ipsec_gre_tx_trace_t;
59
Filip Tehlaraee73642019-03-13 05:50:44 -070060static u8 *
Matus Fabian694265d2016-08-10 01:55:36 -070061format_ipsec_gre_tx_trace (u8 * s, va_list * args)
62{
63 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65 ipsec_gre_tx_trace_t *t = va_arg (*args, ipsec_gre_tx_trace_t *);
66
67 s = format (s, "GRE: tunnel %d len %d src %U dst %U sa-id %d",
68 t->tunnel_id, clib_net_to_host_u16 (t->length),
69 format_ip4_address, &t->src.as_u8,
70 format_ip4_address, &t->dst.as_u8, t->sa_id);
71 return s;
72}
73
74/**
75 * @brief IPSec-GRE tunnel interface tx function.
76 *
77 * Add GRE header to the packet.
78 *
79 * @param vm vlib_main_t corresponding to the current thread.
80 * @param node vlib_node_runtime_t data for this node.
81 * @param frame vlib_frame_t whose contents should be dispatched.
82 *
83 * @par Graph mechanics: buffer metadata, next index usage
84 *
85 * <em>Uses:</em>
86 * - <code>node->runtime_data</code>
87 * - Match tunnel by <code>rd->dev_instance</code> in IPSec-GRE tunnels
88 * pool.
89 *
90 * <em>Sets:</em>
91 * - <code>vnet_buffer(b)->output_features.ipsec_sad_index</code>
92 * - Set IPSec Security Association for packet encryption.
93 * - <code>vnet_buffer(b)->sw_if_index[VLIB_TX]</code>
94 * - Reset output sw_if_index.
95 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -070096 * <em>Next Index:</em>
Matus Fabian694265d2016-08-10 01:55:36 -070097 * - Dispatches the packet to the esp-encrypt node.
98*/
Filip Tehlaraee73642019-03-13 05:50:44 -070099VNET_DEVICE_CLASS_TX_FN (ipsec_gre_device_class) (vlib_main_t * vm,
100 vlib_node_runtime_t * node,
101 vlib_frame_t * frame)
Matus Fabian694265d2016-08-10 01:55:36 -0700102{
103 ipsec_gre_main_t *igm = &ipsec_gre_main;
104 u32 next_index;
105 u32 *from, *to_next, n_left_from, n_left_to_next;
106 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
107 ipsec_gre_tunnel_t *t = pool_elt_at_index (igm->tunnels, rd->dev_instance);
108
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700109 /* use an ethertype of 0x01 for l2-gre */
110 u16 l2_gre_protocol_ethertype = clib_net_to_host_u16 (0x01);
111
Matus Fabian694265d2016-08-10 01:55:36 -0700112 /* Vector of buffer / pkt indices we're supposed to process */
113 from = vlib_frame_vector_args (frame);
114
115 /* Number of buffers / pkts */
116 n_left_from = frame->n_vectors;
117
118 /* Speculatively send the first buffer to the last disposition we used */
119 next_index = node->cached_next_index;
120
121 while (n_left_from > 0)
122 {
123 /* set up to enqueue to our disposition with index = next_index */
124 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
125
126 /*
127 * As long as we have enough pkts left to process two pkts
128 * and prefetch two pkts...
129 */
130 while (n_left_from >= 4 && n_left_to_next >= 2)
131 {
132 vlib_buffer_t *b0, *b1;
133 ip4_header_t *ip0, *ip1;
134 ip4_and_gre_union_t *h0, *h1;
135 u32 bi0, next0, bi1, next1;
136 __attribute__ ((unused)) u8 error0, error1;
Matus Fabian694265d2016-08-10 01:55:36 -0700137
138 /* Prefetch the next iteration */
139 {
140 vlib_buffer_t *p2, *p3;
141
142 p2 = vlib_get_buffer (vm, from[2]);
143 p3 = vlib_get_buffer (vm, from[3]);
144
145 vlib_prefetch_buffer_header (p2, LOAD);
146 vlib_prefetch_buffer_header (p3, LOAD);
147
148 /*
149 * Prefetch packet data. We expect to overwrite
150 * the inbound L2 header with an ip header and a
151 * gre header. Might want to prefetch the last line
152 * of rewrite space as well; need profile data
153 */
154 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
155 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
156 }
157
158 /* Pick up the next two buffer indices */
159 bi0 = from[0];
160 bi1 = from[1];
161
162 /* Speculatively enqueue them where we sent the last buffer */
163 to_next[0] = bi0;
164 to_next[1] = bi1;
165 from += 2;
166 to_next += 2;
167 n_left_to_next -= 2;
168 n_left_from -= 2;
169
170 b0 = vlib_get_buffer (vm, bi0);
171 b1 = vlib_get_buffer (vm, bi1);
172
Matus Fabian694265d2016-08-10 01:55:36 -0700173 vlib_buffer_advance (b0, -sizeof (*h0));
174 vlib_buffer_advance (b1, -sizeof (*h1));
175
176 h0 = vlib_buffer_get_current (b0);
177 h1 = vlib_buffer_get_current (b1);
178 h0->as_u64[0] = 0;
179 h0->as_u64[1] = 0;
180 h0->as_u64[2] = 0;
181
182 h1->as_u64[0] = 0;
183 h1->as_u64[1] = 0;
184 h1->as_u64[2] = 0;
185
186 ip0 = &h0->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700187 h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700188 ip0->ip_version_and_header_length = 0x45;
189 ip0->ttl = 254;
190 ip0->protocol = IP_PROTOCOL_GRE;
191
192 ip1 = &h1->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700193 h1->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700194 ip1->ip_version_and_header_length = 0x45;
195 ip1->ttl = 254;
196 ip1->protocol = IP_PROTOCOL_GRE;
197
198 ip0->length =
199 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
200 ip1->length =
201 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
202 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
203 ip1->src_address.as_u32 = t->tunnel_src.as_u32;
204 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
205 ip1->dst_address.as_u32 = t->tunnel_dst.as_u32;
206 ip0->checksum = ip4_header_checksum (ip0);
207 ip1->checksum = ip4_header_checksum (ip1);
208
Matus Fabian025943b2016-10-07 03:29:09 -0700209 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
210 vnet_buffer (b0)->sw_if_index[VLIB_TX];
211 vnet_buffer (b1)->sw_if_index[VLIB_RX] =
212 vnet_buffer (b1)->sw_if_index[VLIB_TX];
213
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100214 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
215 vnet_buffer (b1)->ipsec.sad_index = t->local_sa;
Matus Fabian694265d2016-08-10 01:55:36 -0700216
217 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
218 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
219
220 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
221 next1 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
222 error0 = IPSEC_GRE_ERROR_NONE;
223 error1 = IPSEC_GRE_ERROR_NONE;
224
225 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
226 {
227 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
228 b0, sizeof (*tr));
229 tr->tunnel_id = t - igm->tunnels;
230 tr->length = ip0->length;
231 tr->src.as_u32 = ip0->src_address.as_u32;
232 tr->dst.as_u32 = ip0->dst_address.as_u32;
233 tr->sa_id = t->local_sa_id;
234 }
235
236 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
237 {
238 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
239 b1, sizeof (*tr));
240 tr->tunnel_id = t - igm->tunnels;
241 tr->length = ip1->length;
242 tr->src.as_u32 = ip1->src_address.as_u32;
243 tr->dst.as_u32 = ip1->dst_address.as_u32;
244 tr->sa_id = t->local_sa_id;
245 }
246
247 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
248 to_next, n_left_to_next,
249 bi0, bi1, next0, next1);
250 }
251
252 while (n_left_from > 0 && n_left_to_next > 0)
253 {
254 vlib_buffer_t *b0;
255 ip4_header_t *ip0;
256 ip4_and_gre_union_t *h0;
257 u32 bi0, next0;
258 __attribute__ ((unused)) u8 error0;
Matus Fabian694265d2016-08-10 01:55:36 -0700259
260 bi0 = to_next[0] = from[0];
261 from += 1;
262 n_left_from -= 1;
263 to_next += 1;
264 n_left_to_next -= 1;
265
266 b0 = vlib_get_buffer (vm, bi0);
267
Matus Fabian694265d2016-08-10 01:55:36 -0700268 vlib_buffer_advance (b0, -sizeof (*h0));
269
270 h0 = vlib_buffer_get_current (b0);
271 h0->as_u64[0] = 0;
272 h0->as_u64[1] = 0;
273 h0->as_u64[2] = 0;
274
275 ip0 = &h0->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700276 h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700277 ip0->ip_version_and_header_length = 0x45;
278 ip0->ttl = 254;
279 ip0->protocol = IP_PROTOCOL_GRE;
280 ip0->length =
281 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
282 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
283 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
284 ip0->checksum = ip4_header_checksum (ip0);
285
Matus Fabian025943b2016-10-07 03:29:09 -0700286 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
287 vnet_buffer (b0)->sw_if_index[VLIB_TX];
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100288 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
Matus Fabian694265d2016-08-10 01:55:36 -0700289 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
290
291 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
292 error0 = IPSEC_GRE_ERROR_NONE;
293
294 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
295 {
296 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
297 b0, sizeof (*tr));
298 tr->tunnel_id = t - igm->tunnels;
299 tr->length = ip0->length;
300 tr->src.as_u32 = ip0->src_address.as_u32;
301 tr->dst.as_u32 = ip0->dst_address.as_u32;
302 tr->sa_id = t->local_sa_id;
303 }
304
305 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
306 to_next, n_left_to_next,
307 bi0, next0);
308 }
309
310 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
311 }
312
313 vlib_node_increment_counter (vm, ipsec_gre_input_node.index,
314 IPSEC_GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
315
316 return frame->n_vectors;
317}
318
319static clib_error_t *
320ipsec_gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
321 u32 flags)
322{
323 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
324 vnet_hw_interface_set_flags (vnm, hw_if_index,
325 VNET_HW_INTERFACE_FLAG_LINK_UP);
326 else
327 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
328
329 return /* no error */ 0;
330}
331
332static u8 *
333format_ipsec_gre_tunnel_name (u8 * s, va_list * args)
334{
335 u32 dev_instance = va_arg (*args, u32);
336 return format (s, "ipsec-gre%d", dev_instance);
337}
338
339static u8 *
340format_ipsec_gre_device (u8 * s, va_list * args)
341{
342 u32 dev_instance = va_arg (*args, u32);
343 CLIB_UNUSED (int verbose) = va_arg (*args, int);
344
345 s = format (s, "IPSEC-GRE tunnel: id %d\n", dev_instance);
346 return s;
347}
348
349/* *INDENT-OFF* */
350VNET_DEVICE_CLASS (ipsec_gre_device_class) = {
351 .name = "IPSec GRE tunnel device",
352 .format_device_name = format_ipsec_gre_tunnel_name,
353 .format_device = format_ipsec_gre_device,
354 .format_tx_trace = format_ipsec_gre_tx_trace,
Matus Fabian694265d2016-08-10 01:55:36 -0700355 .admin_up_down_function = ipsec_gre_interface_admin_up_down,
356};
357
Matus Fabian694265d2016-08-10 01:55:36 -0700358
Filip Tehlaraee73642019-03-13 05:50:44 -0700359#ifndef CLIB_MARCH_VARIANT
Matus Fabian694265d2016-08-10 01:55:36 -0700360VNET_HW_INTERFACE_CLASS (ipsec_gre_hw_interface_class) = {
361 .name = "IPSEC-GRE",
362};
Filip Tehlaraee73642019-03-13 05:50:44 -0700363#endif /* CLIB_MARCH_VARIANT */
Matus Fabian694265d2016-08-10 01:55:36 -0700364/* *INDENT-ON* */
365
366static clib_error_t *
367ipsec_gre_init (vlib_main_t * vm)
368{
369 ipsec_gre_main_t *igm = &ipsec_gre_main;
370 clib_error_t *error;
371
Dave Barachb7b92992018-10-17 10:38:51 -0400372 clib_memset (igm, 0, sizeof (igm[0]));
Matus Fabian694265d2016-08-10 01:55:36 -0700373 igm->vlib_main = vm;
374 igm->vnet_main = vnet_get_main ();
375
376 if ((error = vlib_call_init_function (vm, ip_main_init)))
377 return error;
378
379 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
380 return error;
381
382 igm->tunnel_by_key = hash_create (0, sizeof (uword));
383
384 return vlib_call_init_function (vm, ipsec_gre_input_init);
385}
386
387VLIB_INIT_FUNCTION (ipsec_gre_init);
388
Matus Fabian694265d2016-08-10 01:55:36 -0700389/*
390* fd.io coding-style-patch-verification: ON
391*
392* Local Variables:
393* eval: (c-set-style "gnu")
394* End:
395*/