blob: 1b6fcf3ef63513a224723a4a7b33fe7583269d50 [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
25ipsec_gre_main_t ipsec_gre_main;
26
27/**
Matus Fabian694265d2016-08-10 01:55:36 -070028 * @brief IPv4 and GRE header union.
29 *
30*/
31typedef struct
32{
33 union
34 {
35 ip4_and_gre_header_t ip4_and_gre;
36 u64 as_u64[3];
37 };
38} ip4_and_gre_union_t;
39
40/**
41 * @brief Packet trace.
42 *
43*/
44typedef struct
45{
46 u32 tunnel_id; /**< Tunnel-id / index in tunnel vector */
47
48 u32 length; /**< pkt length */
49
50 ip4_address_t src; /**< tunnel src IPv4 address */
51 ip4_address_t dst; /**< tunnel dst IPv4 address */
52
53 u32 sa_id; /**< tunnel IPSec SA id */
54} ipsec_gre_tx_trace_t;
55
56u8 *
57format_ipsec_gre_tx_trace (u8 * s, va_list * args)
58{
59 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
60 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
61 ipsec_gre_tx_trace_t *t = va_arg (*args, ipsec_gre_tx_trace_t *);
62
63 s = format (s, "GRE: tunnel %d len %d src %U dst %U sa-id %d",
64 t->tunnel_id, clib_net_to_host_u16 (t->length),
65 format_ip4_address, &t->src.as_u8,
66 format_ip4_address, &t->dst.as_u8, t->sa_id);
67 return s;
68}
69
70/**
71 * @brief IPSec-GRE tunnel interface tx function.
72 *
73 * Add GRE header to the packet.
74 *
75 * @param vm vlib_main_t corresponding to the current thread.
76 * @param node vlib_node_runtime_t data for this node.
77 * @param frame vlib_frame_t whose contents should be dispatched.
78 *
79 * @par Graph mechanics: buffer metadata, next index usage
80 *
81 * <em>Uses:</em>
82 * - <code>node->runtime_data</code>
83 * - Match tunnel by <code>rd->dev_instance</code> in IPSec-GRE tunnels
84 * pool.
85 *
86 * <em>Sets:</em>
87 * - <code>vnet_buffer(b)->output_features.ipsec_sad_index</code>
88 * - Set IPSec Security Association for packet encryption.
89 * - <code>vnet_buffer(b)->sw_if_index[VLIB_TX]</code>
90 * - Reset output sw_if_index.
91 *
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -070092 * <em>Next Index:</em>
Matus Fabian694265d2016-08-10 01:55:36 -070093 * - Dispatches the packet to the esp-encrypt node.
94*/
95static uword
96ipsec_gre_interface_tx (vlib_main_t * vm,
97 vlib_node_runtime_t * node, vlib_frame_t * frame)
98{
99 ipsec_gre_main_t *igm = &ipsec_gre_main;
100 u32 next_index;
101 u32 *from, *to_next, n_left_from, n_left_to_next;
102 vnet_interface_output_runtime_t *rd = (void *) node->runtime_data;
103 ipsec_gre_tunnel_t *t = pool_elt_at_index (igm->tunnels, rd->dev_instance);
104
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700105 /* use an ethertype of 0x01 for l2-gre */
106 u16 l2_gre_protocol_ethertype = clib_net_to_host_u16 (0x01);
107
Matus Fabian694265d2016-08-10 01:55:36 -0700108 /* Vector of buffer / pkt indices we're supposed to process */
109 from = vlib_frame_vector_args (frame);
110
111 /* Number of buffers / pkts */
112 n_left_from = frame->n_vectors;
113
114 /* Speculatively send the first buffer to the last disposition we used */
115 next_index = node->cached_next_index;
116
117 while (n_left_from > 0)
118 {
119 /* set up to enqueue to our disposition with index = next_index */
120 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
121
122 /*
123 * As long as we have enough pkts left to process two pkts
124 * and prefetch two pkts...
125 */
126 while (n_left_from >= 4 && n_left_to_next >= 2)
127 {
128 vlib_buffer_t *b0, *b1;
129 ip4_header_t *ip0, *ip1;
130 ip4_and_gre_union_t *h0, *h1;
131 u32 bi0, next0, bi1, next1;
132 __attribute__ ((unused)) u8 error0, error1;
Matus Fabian694265d2016-08-10 01:55:36 -0700133
134 /* Prefetch the next iteration */
135 {
136 vlib_buffer_t *p2, *p3;
137
138 p2 = vlib_get_buffer (vm, from[2]);
139 p3 = vlib_get_buffer (vm, from[3]);
140
141 vlib_prefetch_buffer_header (p2, LOAD);
142 vlib_prefetch_buffer_header (p3, LOAD);
143
144 /*
145 * Prefetch packet data. We expect to overwrite
146 * the inbound L2 header with an ip header and a
147 * gre header. Might want to prefetch the last line
148 * of rewrite space as well; need profile data
149 */
150 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
151 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
152 }
153
154 /* Pick up the next two buffer indices */
155 bi0 = from[0];
156 bi1 = from[1];
157
158 /* Speculatively enqueue them where we sent the last buffer */
159 to_next[0] = bi0;
160 to_next[1] = bi1;
161 from += 2;
162 to_next += 2;
163 n_left_to_next -= 2;
164 n_left_from -= 2;
165
166 b0 = vlib_get_buffer (vm, bi0);
167 b1 = vlib_get_buffer (vm, bi1);
168
Matus Fabian694265d2016-08-10 01:55:36 -0700169 vlib_buffer_advance (b0, -sizeof (*h0));
170 vlib_buffer_advance (b1, -sizeof (*h1));
171
172 h0 = vlib_buffer_get_current (b0);
173 h1 = vlib_buffer_get_current (b1);
174 h0->as_u64[0] = 0;
175 h0->as_u64[1] = 0;
176 h0->as_u64[2] = 0;
177
178 h1->as_u64[0] = 0;
179 h1->as_u64[1] = 0;
180 h1->as_u64[2] = 0;
181
182 ip0 = &h0->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700183 h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700184 ip0->ip_version_and_header_length = 0x45;
185 ip0->ttl = 254;
186 ip0->protocol = IP_PROTOCOL_GRE;
187
188 ip1 = &h1->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700189 h1->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700190 ip1->ip_version_and_header_length = 0x45;
191 ip1->ttl = 254;
192 ip1->protocol = IP_PROTOCOL_GRE;
193
194 ip0->length =
195 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
196 ip1->length =
197 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b1));
198 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
199 ip1->src_address.as_u32 = t->tunnel_src.as_u32;
200 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
201 ip1->dst_address.as_u32 = t->tunnel_dst.as_u32;
202 ip0->checksum = ip4_header_checksum (ip0);
203 ip1->checksum = ip4_header_checksum (ip1);
204
Matus Fabian025943b2016-10-07 03:29:09 -0700205 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
206 vnet_buffer (b0)->sw_if_index[VLIB_TX];
207 vnet_buffer (b1)->sw_if_index[VLIB_RX] =
208 vnet_buffer (b1)->sw_if_index[VLIB_TX];
209
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100210 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
211 vnet_buffer (b1)->ipsec.sad_index = t->local_sa;
Matus Fabian694265d2016-08-10 01:55:36 -0700212
213 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
214 vnet_buffer (b1)->sw_if_index[VLIB_TX] = (u32) ~ 0;
215
216 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
217 next1 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
218 error0 = IPSEC_GRE_ERROR_NONE;
219 error1 = IPSEC_GRE_ERROR_NONE;
220
221 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
222 {
223 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
224 b0, sizeof (*tr));
225 tr->tunnel_id = t - igm->tunnels;
226 tr->length = ip0->length;
227 tr->src.as_u32 = ip0->src_address.as_u32;
228 tr->dst.as_u32 = ip0->dst_address.as_u32;
229 tr->sa_id = t->local_sa_id;
230 }
231
232 if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
233 {
234 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
235 b1, sizeof (*tr));
236 tr->tunnel_id = t - igm->tunnels;
237 tr->length = ip1->length;
238 tr->src.as_u32 = ip1->src_address.as_u32;
239 tr->dst.as_u32 = ip1->dst_address.as_u32;
240 tr->sa_id = t->local_sa_id;
241 }
242
243 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
244 to_next, n_left_to_next,
245 bi0, bi1, next0, next1);
246 }
247
248 while (n_left_from > 0 && n_left_to_next > 0)
249 {
250 vlib_buffer_t *b0;
251 ip4_header_t *ip0;
252 ip4_and_gre_union_t *h0;
253 u32 bi0, next0;
254 __attribute__ ((unused)) u8 error0;
Matus Fabian694265d2016-08-10 01:55:36 -0700255
256 bi0 = to_next[0] = from[0];
257 from += 1;
258 n_left_from -= 1;
259 to_next += 1;
260 n_left_to_next -= 1;
261
262 b0 = vlib_get_buffer (vm, bi0);
263
Matus Fabian694265d2016-08-10 01:55:36 -0700264 vlib_buffer_advance (b0, -sizeof (*h0));
265
266 h0 = vlib_buffer_get_current (b0);
267 h0->as_u64[0] = 0;
268 h0->as_u64[1] = 0;
269 h0->as_u64[2] = 0;
270
271 ip0 = &h0->ip4_and_gre.ip4;
Paul Vinciguerra1bc56042018-10-24 16:45:50 -0700272 h0->ip4_and_gre.gre.protocol = l2_gre_protocol_ethertype;
Matus Fabian694265d2016-08-10 01:55:36 -0700273 ip0->ip_version_and_header_length = 0x45;
274 ip0->ttl = 254;
275 ip0->protocol = IP_PROTOCOL_GRE;
276 ip0->length =
277 clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0));
278 ip0->src_address.as_u32 = t->tunnel_src.as_u32;
279 ip0->dst_address.as_u32 = t->tunnel_dst.as_u32;
280 ip0->checksum = ip4_header_checksum (ip0);
281
Matus Fabian025943b2016-10-07 03:29:09 -0700282 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
283 vnet_buffer (b0)->sw_if_index[VLIB_TX];
Damjan Marion9c6ae5f2016-11-15 23:20:01 +0100284 vnet_buffer (b0)->ipsec.sad_index = t->local_sa;
Matus Fabian694265d2016-08-10 01:55:36 -0700285 vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
286
287 next0 = IPSEC_GRE_OUTPUT_NEXT_ESP_ENCRYPT;
288 error0 = IPSEC_GRE_ERROR_NONE;
289
290 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
291 {
292 ipsec_gre_tx_trace_t *tr = vlib_add_trace (vm, node,
293 b0, sizeof (*tr));
294 tr->tunnel_id = t - igm->tunnels;
295 tr->length = ip0->length;
296 tr->src.as_u32 = ip0->src_address.as_u32;
297 tr->dst.as_u32 = ip0->dst_address.as_u32;
298 tr->sa_id = t->local_sa_id;
299 }
300
301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
302 to_next, n_left_to_next,
303 bi0, next0);
304 }
305
306 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307 }
308
309 vlib_node_increment_counter (vm, ipsec_gre_input_node.index,
310 IPSEC_GRE_ERROR_PKTS_ENCAP, frame->n_vectors);
311
312 return frame->n_vectors;
313}
314
315static clib_error_t *
316ipsec_gre_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index,
317 u32 flags)
318{
319 if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
320 vnet_hw_interface_set_flags (vnm, hw_if_index,
321 VNET_HW_INTERFACE_FLAG_LINK_UP);
322 else
323 vnet_hw_interface_set_flags (vnm, hw_if_index, 0 /* down */ );
324
325 return /* no error */ 0;
326}
327
328static u8 *
329format_ipsec_gre_tunnel_name (u8 * s, va_list * args)
330{
331 u32 dev_instance = va_arg (*args, u32);
332 return format (s, "ipsec-gre%d", dev_instance);
333}
334
335static u8 *
336format_ipsec_gre_device (u8 * s, va_list * args)
337{
338 u32 dev_instance = va_arg (*args, u32);
339 CLIB_UNUSED (int verbose) = va_arg (*args, int);
340
341 s = format (s, "IPSEC-GRE tunnel: id %d\n", dev_instance);
342 return s;
343}
344
345/* *INDENT-OFF* */
346VNET_DEVICE_CLASS (ipsec_gre_device_class) = {
347 .name = "IPSec GRE tunnel device",
348 .format_device_name = format_ipsec_gre_tunnel_name,
349 .format_device = format_ipsec_gre_device,
350 .format_tx_trace = format_ipsec_gre_tx_trace,
351 .tx_function = ipsec_gre_interface_tx,
352 .admin_up_down_function = ipsec_gre_interface_admin_up_down,
353};
354
355VLIB_DEVICE_TX_FUNCTION_MULTIARCH (ipsec_gre_device_class,
356 ipsec_gre_interface_tx)
357
358
359VNET_HW_INTERFACE_CLASS (ipsec_gre_hw_interface_class) = {
360 .name = "IPSEC-GRE",
361};
362/* *INDENT-ON* */
363
364static clib_error_t *
365ipsec_gre_init (vlib_main_t * vm)
366{
367 ipsec_gre_main_t *igm = &ipsec_gre_main;
368 clib_error_t *error;
369
Dave Barachb7b92992018-10-17 10:38:51 -0400370 clib_memset (igm, 0, sizeof (igm[0]));
Matus Fabian694265d2016-08-10 01:55:36 -0700371 igm->vlib_main = vm;
372 igm->vnet_main = vnet_get_main ();
373
374 if ((error = vlib_call_init_function (vm, ip_main_init)))
375 return error;
376
377 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
378 return error;
379
380 igm->tunnel_by_key = hash_create (0, sizeof (uword));
381
382 return vlib_call_init_function (vm, ipsec_gre_input_init);
383}
384
385VLIB_INIT_FUNCTION (ipsec_gre_init);
386
Matus Fabian694265d2016-08-10 01:55:36 -0700387/*
388* fd.io coding-style-patch-verification: ON
389*
390* Local Variables:
391* eval: (c-set-style "gnu")
392* End:
393*/