wireguard: fix ipv6 payload_length computation
[vpp.git] / src / plugins / wireguard / wireguard_output_tun.c
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2020 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20
21 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
23
24 #define foreach_wg_output_error                                               \
25   _ (NONE, "No error")                                                        \
26   _ (PEER, "Peer error")                                                      \
27   _ (KEYPAIR, "Keypair error")                                                \
28   _ (TOO_BIG, "packet too big")                                               \
29   _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
30
31 typedef enum
32 {
33 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
34   foreach_wg_output_error
35 #undef _
36     WG_OUTPUT_N_ERROR,
37 } wg_output_error_t;
38
39 static char *wg_output_error_strings[] = {
40 #define _(sym,string) string,
41   foreach_wg_output_error
42 #undef _
43 };
44
45 typedef enum
46 {
47   WG_OUTPUT_NEXT_ERROR,
48   WG_OUTPUT_NEXT_HANDOFF,
49   WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
50   WG_OUTPUT_N_NEXT,
51 } wg_output_next_t;
52
53 typedef struct
54 {
55   index_t peer;
56   u8 header[sizeof (ip6_udp_header_t)];
57   u8 is_ip4;
58 } wg_output_tun_trace_t;
59
60 typedef struct
61 {
62   index_t peer;
63   u32 next_index;
64 } wg_output_tun_post_trace_t;
65
66 u8 *
67 format_ip4_udp_header (u8 * s, va_list * args)
68 {
69   ip4_udp_header_t *hdr4 = va_arg (*args, ip4_udp_header_t *);
70
71   s = format (s, "%U:$U", format_ip4_header, &hdr4->ip4, format_udp_header,
72               &hdr4->udp);
73   return (s);
74 }
75
76 u8 *
77 format_ip6_udp_header (u8 *s, va_list *args)
78 {
79   ip6_udp_header_t *hdr6 = va_arg (*args, ip6_udp_header_t *);
80
81   s = format (s, "%U:$U", format_ip6_header, &hdr6->ip6, format_udp_header,
82               &hdr6->udp);
83   return (s);
84 }
85
86 /* packet trace format function */
87 static u8 *
88 format_wg_output_tun_trace (u8 * s, va_list * args)
89 {
90   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
91   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
92
93   wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
94
95   s = format (s, "peer: %d\n", t->peer);
96   s = format (s, "  Encrypted packet: ");
97
98   s = t->is_ip4 ? format (s, "%U", format_ip4_udp_header, t->header) :
99                   format (s, "%U", format_ip6_udp_header, t->header);
100   return s;
101 }
102
103 /* post node - packet trace format function */
104 static u8 *
105 format_wg_output_tun_post_trace (u8 *s, va_list *args)
106 {
107   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
108   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
109
110   wg_output_tun_post_trace_t *t = va_arg (*args, wg_output_tun_post_trace_t *);
111
112   s = format (s, "peer: %d\n", t->peer);
113   s = format (s, "  wg-post: next node index %u", t->next_index);
114   return s;
115 }
116
117 static_always_inline void
118 wg_prepare_sync_enc_op (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
119                         u8 *src, u32 src_len, u8 *dst, u8 *aad, u32 aad_len,
120                         u64 nonce, vnet_crypto_key_index_t key_index, u32 bi,
121                         u8 *iv)
122 {
123   vnet_crypto_op_t _op, *op = &_op;
124   u8 src_[] = {};
125
126   clib_memset (iv, 0, 4);
127   clib_memcpy (iv + 4, &nonce, sizeof (nonce));
128
129   vec_add2_aligned (crypto_ops[0], op, 1, CLIB_CACHE_LINE_BYTES);
130   vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_ENC);
131
132   op->tag_len = NOISE_AUTHTAG_LEN;
133   op->tag = dst + src_len;
134   op->src = !src ? src_ : src;
135   op->len = src_len;
136   op->dst = dst;
137   op->key_index = key_index;
138   op->aad = aad;
139   op->aad_len = aad_len;
140   op->iv = iv;
141   op->user_data = bi;
142 }
143
144 static_always_inline void
145 wg_output_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
146                        vnet_crypto_op_t *ops, vlib_buffer_t *b[], u16 *nexts,
147                        u16 drop_next)
148 {
149   u32 n_fail, n_ops = vec_len (ops);
150   vnet_crypto_op_t *op = ops;
151
152   if (n_ops == 0)
153     return;
154
155   n_fail = n_ops - vnet_crypto_process_ops (vm, op, n_ops);
156
157   while (n_fail)
158     {
159       ASSERT (op - ops < n_ops);
160
161       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
162         {
163           u32 bi = op->user_data;
164           b[bi]->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
165           nexts[bi] = drop_next;
166           n_fail--;
167         }
168       op++;
169     }
170 }
171
172 static_always_inline void
173 wg_output_tun_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
174                             u32 key_index, u32 crypto_len,
175                             i16 crypto_start_offset, u32 buffer_index,
176                             u16 next_node, u8 *iv, u8 *tag, u8 flags)
177 {
178   vnet_crypto_async_frame_elt_t *fe;
179   u16 index;
180
181   ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
182
183   index = f->n_elts;
184   fe = &f->elts[index];
185   f->n_elts++;
186   fe->key_index = key_index;
187   fe->crypto_total_length = crypto_len;
188   fe->crypto_start_offset = crypto_start_offset;
189   fe->iv = iv;
190   fe->tag = tag;
191   fe->flags = flags;
192   f->buffer_indices[index] = buffer_index;
193   f->next_node_index[index] = next_node;
194 }
195
196 static_always_inline enum noise_state_crypt
197 wq_output_tun_process (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
198                        noise_remote_t *r, uint32_t *r_idx, uint64_t *nonce,
199                        uint8_t *src, size_t srclen, uint8_t *dst, u32 bi,
200                        u8 *iv, f64 time)
201 {
202   noise_keypair_t *kp;
203   enum noise_state_crypt ret = SC_FAILED;
204
205   if ((kp = r->r_current) == NULL)
206     goto error;
207
208   /* We confirm that our values are within our tolerances. We want:
209    *  - a valid keypair
210    *  - our keypair to be less than REJECT_AFTER_TIME seconds old
211    *  - our receive counter to be less than REJECT_AFTER_MESSAGES
212    *  - our send counter to be less than REJECT_AFTER_MESSAGES
213    */
214   if (!kp->kp_valid ||
215       wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
216                                     time) ||
217       kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
218       ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
219     goto error;
220
221   /* We encrypt into the same buffer, so the caller must ensure that buf
222    * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
223    * are passed back out to the caller through the provided data pointer. */
224   *r_idx = kp->kp_remote_index;
225
226   wg_prepare_sync_enc_op (vm, crypto_ops, src, srclen, dst, NULL, 0, *nonce,
227                           kp->kp_send_index, bi, iv);
228
229   /* If our values are still within tolerances, but we are approaching
230    * the tolerances, we notify the caller with ESTALE that they should
231    * establish a new keypair. The current keypair can continue to be used
232    * until the tolerances are hit. We notify if:
233    *  - our send counter is valid and not less than REKEY_AFTER_MESSAGES
234    *  - we're the initiator and our keypair is older than
235    *    REKEY_AFTER_TIME seconds */
236   ret = SC_KEEP_KEY_FRESH;
237   if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
238       (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
239                                 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
240     goto error;
241
242   ret = SC_OK;
243 error:
244   return ret;
245 }
246
247 static_always_inline enum noise_state_crypt
248 wg_add_to_async_frame (vlib_main_t *vm, wg_per_thread_data_t *ptd,
249                        vnet_crypto_async_frame_t *async_frame,
250                        vlib_buffer_t *b, u8 *payload, u32 payload_len, u32 bi,
251                        u16 next, u16 async_next, noise_remote_t *r,
252                        uint32_t *r_idx, uint64_t *nonce, u8 *iv, f64 time)
253 {
254   wg_post_data_t *post = wg_post_data (b);
255   u8 flag = 0;
256   noise_keypair_t *kp;
257
258   post->next_index = next;
259
260   /* crypto */
261   enum noise_state_crypt ret = SC_FAILED;
262
263   if ((kp = r->r_current) == NULL)
264     goto error;
265
266   /* We confirm that our values are within our tolerances. We want:
267    *  - a valid keypair
268    *  - our keypair to be less than REJECT_AFTER_TIME seconds old
269    *  - our receive counter to be less than REJECT_AFTER_MESSAGES
270    *  - our send counter to be less than REJECT_AFTER_MESSAGES
271    */
272   if (!kp->kp_valid ||
273       wg_birthdate_has_expired_opt (kp->kp_birthdate, REJECT_AFTER_TIME,
274                                     time) ||
275       kp->kp_ctr.c_recv >= REJECT_AFTER_MESSAGES ||
276       ((*nonce = noise_counter_send (&kp->kp_ctr)) > REJECT_AFTER_MESSAGES))
277     goto error;
278
279   /* We encrypt into the same buffer, so the caller must ensure that buf
280    * has NOISE_AUTHTAG_LEN bytes to store the MAC. The nonce and index
281    * are passed back out to the caller through the provided data pointer. */
282   *r_idx = kp->kp_remote_index;
283
284   clib_memset (iv, 0, 4);
285   clib_memcpy (iv + 4, nonce, sizeof (*nonce));
286
287   /* this always succeeds because we know the frame is not full */
288   wg_output_tun_add_to_frame (vm, async_frame, kp->kp_send_index, payload_len,
289                               payload - b->data, bi, async_next, iv,
290                               payload + payload_len, flag);
291
292   /* If our values are still within tolerances, but we are approaching
293    * the tolerances, we notify the caller with ESTALE that they should
294    * establish a new keypair. The current keypair can continue to be used
295    * until the tolerances are hit. We notify if:
296    *  - our send counter is valid and not less than REKEY_AFTER_MESSAGES
297    *  - we're the initiator and our keypair is older than
298    *    REKEY_AFTER_TIME seconds */
299   ret = SC_KEEP_KEY_FRESH;
300   if ((kp->kp_valid && *nonce >= REKEY_AFTER_MESSAGES) ||
301       (kp->kp_is_initiator && wg_birthdate_has_expired_opt (
302                                 kp->kp_birthdate, REKEY_AFTER_TIME, time)))
303     goto error;
304
305   ret = SC_OK;
306 error:
307   return ret;
308 }
309
310 /* is_ip4 - inner header flag */
311 always_inline uword
312 wg_output_tun_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
313                       vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
314 {
315   wg_main_t *wmp = &wg_main;
316   wg_per_thread_data_t *ptd =
317     vec_elt_at_index (wmp->per_thread_data, vm->thread_index);
318   u32 *from = vlib_frame_vector_args (frame);
319   u32 n_left_from = frame->n_vectors;
320   ip4_udp_wg_header_t *hdr4_out = NULL;
321   ip6_udp_wg_header_t *hdr6_out = NULL;
322   message_data_t *message_data_wg = NULL;
323   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
324   vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
325   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
326   vlib_buffer_t *sync_bufs[VLIB_FRAME_SIZE];
327   u32 thread_index = vm->thread_index;
328   u16 n_sync = 0;
329   const u16 drop_next = WG_OUTPUT_NEXT_ERROR;
330   const u8 is_async = wg_op_mode_is_set_ASYNC ();
331   vnet_crypto_async_frame_t *async_frame = NULL;
332   u16 n_async = 0;
333   u16 noop_nexts[VLIB_FRAME_SIZE], *noop_next = noop_nexts, n_noop = 0;
334   u16 err = !0;
335   u32 sync_bi[VLIB_FRAME_SIZE];
336   u32 noop_bi[VLIB_FRAME_SIZE];
337
338   vlib_get_buffers (vm, from, bufs, n_left_from);
339   vec_reset_length (ptd->crypto_ops);
340   vec_reset_length (ptd->async_frames);
341
342   wg_peer_t *peer = NULL;
343   u32 adj_index = 0;
344   u32 last_adj_index = ~0;
345   index_t peeri = INDEX_INVALID;
346
347   f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
348
349   while (n_left_from > 0)
350     {
351       u8 iph_offset = 0;
352       u8 is_ip4_out = 1;
353       u8 *plain_data;
354       u16 plain_data_len;
355
356       if (n_left_from > 2)
357         {
358           u8 *p;
359           vlib_prefetch_buffer_header (b[2], LOAD);
360           p = vlib_buffer_get_current (b[1]);
361           CLIB_PREFETCH (p, CLIB_CACHE_LINE_BYTES, LOAD);
362           CLIB_PREFETCH (vlib_buffer_get_tail (b[1]), CLIB_CACHE_LINE_BYTES,
363                          LOAD);
364         }
365
366       noop_next[0] = WG_OUTPUT_NEXT_ERROR;
367       err = WG_OUTPUT_NEXT_ERROR;
368
369       adj_index = vnet_buffer (b[0])->ip.adj_index[VLIB_TX];
370
371       if (PREDICT_FALSE (last_adj_index != adj_index))
372         {
373           peeri = wg_peer_get_by_adj_index (adj_index);
374           if (peeri == INDEX_INVALID)
375             {
376               b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
377               goto out;
378             }
379           peer = wg_peer_get (peeri);
380         }
381
382       if (!peer || wg_peer_is_dead (peer))
383         {
384           b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
385           goto out;
386         }
387       if (PREDICT_FALSE (~0 == peer->output_thread_index))
388         {
389           /* this is the first packet to use this peer, claim the peer
390            * for this thread.
391            */
392           clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
393                                     wg_peer_assign_thread (thread_index));
394         }
395
396       if (PREDICT_FALSE (thread_index != peer->output_thread_index))
397         {
398           noop_next[0] = WG_OUTPUT_NEXT_HANDOFF;
399           err = WG_OUTPUT_NEXT_HANDOFF;
400           goto next;
401         }
402
403       if (PREDICT_FALSE (!peer->remote.r_current))
404         {
405           wg_send_handshake_from_mt (peeri, false);
406           b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
407           goto out;
408         }
409
410       is_ip4_out = ip46_address_is_ip4 (&peer->src.addr);
411       if (is_ip4_out)
412         {
413           hdr4_out = vlib_buffer_get_current (b[0]);
414           message_data_wg = &hdr4_out->wg;
415         }
416       else
417         {
418           hdr6_out = vlib_buffer_get_current (b[0]);
419           message_data_wg = &hdr6_out->wg;
420         }
421
422       iph_offset = vnet_buffer (b[0])->ip.save_rewrite_length;
423       plain_data = vlib_buffer_get_current (b[0]) + iph_offset;
424       plain_data_len = vlib_buffer_length_in_chain (vm, b[0]) - iph_offset;
425       u8 *iv_data = b[0]->pre_data;
426
427       size_t encrypted_packet_len = message_data_len (plain_data_len);
428
429       /*
430        * Ensure there is enough space to write the encrypted data
431        * into the packet
432        */
433       if (PREDICT_FALSE (encrypted_packet_len >= WG_DEFAULT_DATA_SIZE) ||
434           PREDICT_FALSE ((b[0]->current_data + encrypted_packet_len) >=
435                          vlib_buffer_get_default_data_size (vm)))
436         {
437           b[0]->error = node->errors[WG_OUTPUT_ERROR_TOO_BIG];
438           goto out;
439         }
440
441       if (PREDICT_FALSE (last_adj_index != adj_index))
442         {
443           wg_timers_any_authenticated_packet_sent_opt (peer, time);
444           wg_timers_data_sent_opt (peer, time);
445           wg_timers_any_authenticated_packet_traversal (peer);
446           last_adj_index = adj_index;
447         }
448
449       /* Here we are sure that can send packet to next node */
450       next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
451
452       enum noise_state_crypt state;
453
454       if (is_async)
455         {
456           /* get a frame for this op if we don't yet have one or it's full  */
457           if (NULL == async_frame ||
458               vnet_crypto_async_frame_is_full (async_frame))
459             {
460               async_frame = vnet_crypto_async_get_frame (
461                 vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_ENC);
462               /* Save the frame to the list we'll submit at the end */
463               vec_add1 (ptd->async_frames, async_frame);
464             }
465           state = wg_add_to_async_frame (
466             vm, ptd, async_frame, b[0], plain_data, plain_data_len,
467             from[b - bufs], next[0], async_next_node, &peer->remote,
468             &message_data_wg->receiver_index, &message_data_wg->counter,
469             iv_data, time);
470         }
471       else
472         {
473           state = wq_output_tun_process (
474             vm, crypto_ops, &peer->remote, &message_data_wg->receiver_index,
475             &message_data_wg->counter, plain_data, plain_data_len, plain_data,
476             n_sync, iv_data, time);
477         }
478
479       if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
480         {
481           wg_send_handshake_from_mt (peeri, false);
482         }
483       else if (PREDICT_FALSE (state == SC_FAILED))
484         {
485           // TODO: Maybe wrong
486           wg_send_handshake_from_mt (peeri, false);
487           wg_peer_update_flags (peeri, WG_PEER_ESTABLISHED, false);
488           noop_next[0] = WG_OUTPUT_NEXT_ERROR;
489           goto out;
490         }
491
492       err = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
493
494       if (is_ip4_out)
495         {
496           hdr4_out->wg.header.type = MESSAGE_DATA;
497           hdr4_out->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
498                                                        sizeof (udp_header_t));
499           b[0]->current_length =
500             (encrypted_packet_len + sizeof (ip4_udp_header_t));
501           ip4_header_set_len_w_chksum (
502             &hdr4_out->ip4, clib_host_to_net_u16 (b[0]->current_length));
503         }
504       else
505         {
506           hdr6_out->wg.header.type = MESSAGE_DATA;
507           hdr6_out->ip6.payload_length = hdr6_out->udp.length =
508             clib_host_to_net_u16 (encrypted_packet_len +
509                                   sizeof (udp_header_t));
510           b[0]->current_length =
511             (encrypted_packet_len + sizeof (ip6_udp_header_t));
512         }
513
514     out:
515       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
516                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
517         {
518           wg_output_tun_trace_t *t =
519             vlib_add_trace (vm, node, b[0], sizeof (*t));
520
521           t->peer = peeri;
522           t->is_ip4 = is_ip4_out;
523           if (hdr4_out)
524             clib_memcpy (t->header, hdr4_out, sizeof (ip4_udp_header_t));
525           else if (hdr6_out)
526             clib_memcpy (t->header, hdr6_out, sizeof (ip6_udp_header_t));
527         }
528
529     next:
530       if (PREDICT_FALSE (err != WG_OUTPUT_NEXT_INTERFACE_OUTPUT))
531         {
532           noop_bi[n_noop] = from[b - bufs];
533           n_noop++;
534           noop_next++;
535           goto next_left;
536         }
537       if (!is_async)
538         {
539           sync_bi[n_sync] = from[b - bufs];
540           sync_bufs[n_sync] = b[0];
541           n_sync += 1;
542           next += 1;
543         }
544       else
545         {
546           n_async++;
547         }
548     next_left:
549       n_left_from -= 1;
550       b += 1;
551     }
552
553   if (n_sync)
554     {
555       /* wg-output-process-ops */
556       wg_output_process_ops (vm, node, ptd->crypto_ops, sync_bufs, nexts,
557                              drop_next);
558       vlib_buffer_enqueue_to_next (vm, node, sync_bi, nexts, n_sync);
559     }
560   if (n_async)
561     {
562       /* submit all of the open frames */
563       vnet_crypto_async_frame_t **async_frame;
564
565       vec_foreach (async_frame, ptd->async_frames)
566         {
567           if (PREDICT_FALSE (
568                 vnet_crypto_async_submit_open_frame (vm, *async_frame) < 0))
569             {
570               u32 n_drop = (*async_frame)->n_elts;
571               u32 *bi = (*async_frame)->buffer_indices;
572               u16 index = n_noop;
573               while (n_drop--)
574                 {
575                   noop_bi[index] = bi[0];
576                   vlib_buffer_t *b = vlib_get_buffer (vm, bi[0]);
577                   noop_nexts[index] = drop_next;
578                   b->error = node->errors[WG_OUTPUT_ERROR_CRYPTO_ENGINE_ERROR];
579                   bi++;
580                   index++;
581                 }
582               n_noop += (*async_frame)->n_elts;
583
584               vnet_crypto_async_reset_frame (*async_frame);
585               vnet_crypto_async_free_frame (vm, *async_frame);
586             }
587         }
588     }
589   if (n_noop)
590     {
591       vlib_buffer_enqueue_to_next (vm, node, noop_bi, noop_nexts, n_noop);
592     }
593
594   return frame->n_vectors;
595 }
596
597 always_inline uword
598 wg_output_tun_post (vlib_main_t *vm, vlib_node_runtime_t *node,
599                     vlib_frame_t *frame)
600 {
601   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
602   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
603   u32 *from = vlib_frame_vector_args (frame);
604   u32 n_left = frame->n_vectors;
605
606   index_t peeri = ~0;
607
608   vlib_get_buffers (vm, from, b, n_left);
609
610   if (n_left >= 4)
611     {
612       vlib_prefetch_buffer_header (b[0], LOAD);
613       vlib_prefetch_buffer_header (b[1], LOAD);
614       vlib_prefetch_buffer_header (b[2], LOAD);
615       vlib_prefetch_buffer_header (b[3], LOAD);
616     }
617
618   while (n_left > 8)
619     {
620       vlib_prefetch_buffer_header (b[4], LOAD);
621       vlib_prefetch_buffer_header (b[5], LOAD);
622       vlib_prefetch_buffer_header (b[6], LOAD);
623       vlib_prefetch_buffer_header (b[7], LOAD);
624
625       next[0] = (wg_post_data (b[0]))->next_index;
626       next[1] = (wg_post_data (b[1]))->next_index;
627       next[2] = (wg_post_data (b[2]))->next_index;
628       next[3] = (wg_post_data (b[3]))->next_index;
629
630       if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
631         {
632           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
633             {
634               wg_output_tun_post_trace_t *tr =
635                 vlib_add_trace (vm, node, b[0], sizeof (*tr));
636               peeri = wg_peer_get_by_adj_index (
637                 vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
638               tr->peer = peeri;
639               tr->next_index = next[0];
640             }
641           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
642             {
643               wg_output_tun_post_trace_t *tr =
644                 vlib_add_trace (vm, node, b[1], sizeof (*tr));
645               peeri = wg_peer_get_by_adj_index (
646                 vnet_buffer (b[1])->ip.adj_index[VLIB_TX]);
647               tr->next_index = next[1];
648             }
649           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
650             {
651               wg_output_tun_post_trace_t *tr =
652                 vlib_add_trace (vm, node, b[2], sizeof (*tr));
653               peeri = wg_peer_get_by_adj_index (
654                 vnet_buffer (b[2])->ip.adj_index[VLIB_TX]);
655               tr->next_index = next[2];
656             }
657           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
658             {
659               wg_output_tun_post_trace_t *tr =
660                 vlib_add_trace (vm, node, b[3], sizeof (*tr));
661               peeri = wg_peer_get_by_adj_index (
662                 vnet_buffer (b[3])->ip.adj_index[VLIB_TX]);
663               tr->next_index = next[3];
664             }
665         }
666
667       b += 4;
668       next += 4;
669       n_left -= 4;
670     }
671
672   while (n_left > 0)
673     {
674       next[0] = (wg_post_data (b[0]))->next_index;
675       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
676                          (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
677         {
678           wg_output_tun_post_trace_t *tr =
679             vlib_add_trace (vm, node, b[0], sizeof (*tr));
680           peeri = wg_peer_get_by_adj_index (
681             vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
682           tr->next_index = next[0];
683         }
684
685       b += 1;
686       next += 1;
687       n_left -= 1;
688     }
689
690   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
691   return frame->n_vectors;
692 }
693
694 VLIB_REGISTER_NODE (wg4_output_tun_post_node) = {
695   .name = "wg4-output-tun-post-node",
696   .vector_size = sizeof (u32),
697   .format_trace = format_wg_output_tun_post_trace,
698   .type = VLIB_NODE_TYPE_INTERNAL,
699   .sibling_of = "wg4-output-tun",
700   .n_errors = ARRAY_LEN (wg_output_error_strings),
701   .error_strings = wg_output_error_strings,
702 };
703
704 VLIB_REGISTER_NODE (wg6_output_tun_post_node) = {
705   .name = "wg6-output-tun-post-node",
706   .vector_size = sizeof (u32),
707   .format_trace = format_wg_output_tun_post_trace,
708   .type = VLIB_NODE_TYPE_INTERNAL,
709   .sibling_of = "wg6-output-tun",
710   .n_errors = ARRAY_LEN (wg_output_error_strings),
711   .error_strings = wg_output_error_strings,
712 };
713
714 VLIB_NODE_FN (wg4_output_tun_post_node)
715 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
716 {
717   return wg_output_tun_post (vm, node, from_frame);
718 }
719
720 VLIB_NODE_FN (wg6_output_tun_post_node)
721 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
722 {
723   return wg_output_tun_post (vm, node, from_frame);
724 }
725
726 VLIB_NODE_FN (wg4_output_tun_node)
727 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
728 {
729   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 1,
730                                wg_encrypt_async_next.wg4_post_next);
731 }
732
733 VLIB_NODE_FN (wg6_output_tun_node)
734 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
735 {
736   return wg_output_tun_inline (vm, node, frame, /* is_ip4 */ 0,
737                                wg_encrypt_async_next.wg6_post_next);
738 }
739
740 /* *INDENT-OFF* */
741 VLIB_REGISTER_NODE (wg4_output_tun_node) =
742 {
743   .name = "wg4-output-tun",
744   .vector_size = sizeof (u32),
745   .format_trace = format_wg_output_tun_trace,
746   .type = VLIB_NODE_TYPE_INTERNAL,
747   .n_errors = ARRAY_LEN (wg_output_error_strings),
748   .error_strings = wg_output_error_strings,
749   .n_next_nodes = WG_OUTPUT_N_NEXT,
750   .next_nodes = {
751         [WG_OUTPUT_NEXT_HANDOFF] = "wg4-output-tun-handoff",
752         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
753         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
754   },
755 };
756
757 VLIB_REGISTER_NODE (wg6_output_tun_node) =
758 {
759   .name = "wg6-output-tun",
760   .vector_size = sizeof (u32),
761   .format_trace = format_wg_output_tun_trace,
762   .type = VLIB_NODE_TYPE_INTERNAL,
763   .n_errors = ARRAY_LEN (wg_output_error_strings),
764   .error_strings = wg_output_error_strings,
765   .n_next_nodes = WG_OUTPUT_N_NEXT,
766   .next_nodes = {
767         [WG_OUTPUT_NEXT_HANDOFF] = "wg6-output-tun-handoff",
768         [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
769         [WG_OUTPUT_NEXT_ERROR] = "error-drop",
770   },
771 };
772 /* *INDENT-ON* */
773
774 /*
775  * fd.io coding-style-patch-verification: ON
776  *
777  * Local Variables:
778  * eval: (c-set-style "gnu")
779  * End:
780  */