crypto: use fixed crypto frame pool
[vpp.git] / src / plugins / wireguard / wireguard_input.c
index ef60d50..cf8a597 100644 (file)
@@ -25,6 +25,7 @@
 #define foreach_wg_input_error                                                \
   _ (NONE, "No error")                                                        \
   _ (HANDSHAKE_MAC, "Invalid MAC handshake")                                  \
+  _ (HANDSHAKE_RATELIMITED, "Handshake ratelimited")                          \
   _ (PEER, "Peer error")                                                      \
   _ (INTERFACE, "Interface error")                                            \
   _ (DECRYPTION, "Failed during decryption")                                  \
@@ -32,7 +33,8 @@
   _ (HANDSHAKE_SEND, "Failed while sending Handshake")                        \
   _ (HANDSHAKE_RECEIVE, "Failed while receiving Handshake")                   \
   _ (COOKIE_DECRYPTION, "Failed during Cookie decryption")                    \
-  _ (TOO_BIG, "Packet too big")                                               \
+  _ (COOKIE_SEND, "Failed during sending Cookie")                             \
+  _ (NO_BUFFERS, "No buffers")                                                \
   _ (UNDEFINED, "Undefined error")                                            \
   _ (CRYPTO_ENGINE_ERROR, "crypto engine error (packet dropped)")
 
@@ -123,16 +125,6 @@ typedef enum
   WG_INPUT_N_NEXT,
 } wg_input_next_t;
 
-/* static void */
-/* set_peer_address (wg_peer_t * peer, ip4_address_t ip4, u16 udp_port) */
-/* { */
-/*   if (peer) */
-/*     { */
-/*       ip46_address_set_ip4 (&peer->dst.addr, &ip4); */
-/*       peer->dst.port = udp_port; */
-/*     } */
-/* } */
-
 static u8
 is_ip4_header (u8 *data)
 {
@@ -169,11 +161,10 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
     }
 
   udp_header_t *uhd = current_b_data - sizeof (udp_header_t);
-  u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);;
-  u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);;
+  u16 udp_src_port = clib_host_to_net_u16 (uhd->src_port);
+  u16 udp_dst_port = clib_host_to_net_u16 (uhd->dst_port);
 
   message_header_t *header = current_b_data;
-  under_load = false;
 
   if (PREDICT_FALSE (header->type == MESSAGE_HANDSHAKE_COOKIE))
     {
@@ -211,11 +202,13 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
       if (NULL == wg_if)
        continue;
 
+      under_load = wg_if_is_under_load (vm, wg_if);
       mac_state = cookie_checker_validate_macs (
        vm, &wg_if->cookie_checker, macs, current_b_data, len, under_load,
        &src_ip, udp_src_port);
       if (mac_state == INVALID_MAC)
        {
+         wg_if_dec_handshake_num (wg_if);
          wg_if = NULL;
          continue;
        }
@@ -230,6 +223,8 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
     packet_needs_cookie = false;
   else if (under_load && mac_state == VALID_MAC_BUT_NO_COOKIE)
     packet_needs_cookie = true;
+  else if (mac_state == VALID_MAC_WITH_COOKIE_BUT_RATELIMITED)
+    return WG_INPUT_ERROR_HANDSHAKE_RATELIMITED;
   else
     return WG_INPUT_ERROR_HANDSHAKE_MAC;
 
@@ -241,8 +236,16 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
 
        if (packet_needs_cookie)
          {
-           // TODO: Add processing
+
+           if (!wg_send_handshake_cookie (vm, message->sender_index,
+                                          &wg_if->cookie_checker, macs,
+                                          &ip_addr_46 (&wg_if->src_ip),
+                                          wg_if->port, &src_ip, udp_src_port))
+             return WG_INPUT_ERROR_COOKIE_SEND;
+
+           return WG_INPUT_ERROR_NONE;
          }
+
        noise_remote_t *rp;
        if (noise_consume_initiation
            (vm, noise_local_get (wg_if->local_idx), &rp,
@@ -256,21 +259,30 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
            return WG_INPUT_ERROR_PEER;
          }
 
-       // set_peer_address (peer, ip4_src, udp_src_port);
+       wg_peer_update_endpoint (rp->r_peer_idx, &src_ip, udp_src_port);
+
        if (PREDICT_FALSE (!wg_send_handshake_response (vm, peer)))
          {
            vlib_node_increment_counter (vm, node_idx,
                                         WG_INPUT_ERROR_HANDSHAKE_SEND, 1);
          }
-       else
-         {
-           wg_peer_update_flags (rp->r_peer_idx, WG_PEER_ESTABLISHED, true);
-         }
        break;
       }
     case MESSAGE_HANDSHAKE_RESPONSE:
       {
        message_handshake_response_t *resp = current_b_data;
+
+       if (packet_needs_cookie)
+         {
+           if (!wg_send_handshake_cookie (vm, resp->sender_index,
+                                          &wg_if->cookie_checker, macs,
+                                          &ip_addr_46 (&wg_if->src_ip),
+                                          wg_if->port, &src_ip, udp_src_port))
+             return WG_INPUT_ERROR_COOKIE_SEND;
+
+           return WG_INPUT_ERROR_NONE;
+         }
+
        index_t peeri = INDEX_INVALID;
        u32 *entry =
          wg_index_table_lookup (&wmp->index_table, resp->receiver_index);
@@ -292,12 +304,9 @@ wg_handshake_process (vlib_main_t *vm, wg_main_t *wmp, vlib_buffer_t *b,
          {
            return WG_INPUT_ERROR_PEER;
          }
-       if (packet_needs_cookie)
-         {
-           // TODO: Add processing
-         }
 
-       // set_peer_address (peer, ip4_src, udp_src_port);
+       wg_peer_update_endpoint (peeri, &src_ip, udp_src_port);
+
        if (noise_remote_begin_session (vm, &peer->remote))
          {
 
@@ -330,27 +339,35 @@ wg_input_post_process (vlib_main_t *vm, vlib_buffer_t *b, u16 *next,
                       bool *is_keepalive)
 {
   next[0] = WG_INPUT_NEXT_PUNT;
+  noise_keypair_t *kp;
+  vlib_buffer_t *lb;
 
-  noise_keypair_t *kp =
-    wg_get_active_keypair (&peer->remote, data->receiver_index);
+  if ((kp = wg_get_active_keypair (&peer->remote, data->receiver_index)) ==
+      NULL)
+    return -1;
 
   if (!noise_counter_recv (&kp->kp_ctr, data->counter))
     {
       return -1;
     }
 
-  u16 encr_len = b->current_length - sizeof (message_data_t);
+  lb = b;
+  /* Find last buffer in the chain */
+  while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
+    lb = vlib_get_buffer (vm, lb->next_buffer);
+
+  u16 encr_len = vlib_buffer_length_in_chain (vm, b) - sizeof (message_data_t);
   u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
 
   vlib_buffer_advance (b, sizeof (message_data_t));
-  b->current_length = decr_len;
+  vlib_buffer_chain_increase_length (b, lb, -NOISE_AUTHTAG_LEN);
   vnet_buffer_offload_flags_clear (b, VNET_BUFFER_OFFLOAD_F_UDP_CKSUM);
 
   /* Keepalive packet has zero length */
   if (decr_len == 0)
     {
       *is_keepalive = true;
-      return -1;
+      return 0;
     }
 
   wg_timers_data_received (peer);
@@ -422,9 +439,75 @@ wg_input_process_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
     }
 }
 
+static_always_inline void
+wg_input_process_chained_ops (vlib_main_t *vm, vlib_node_runtime_t *node,
+                             vnet_crypto_op_t *ops, vlib_buffer_t *b[],
+                             u16 *nexts, vnet_crypto_op_chunk_t *chunks,
+                             u16 drop_next)
+{
+  u32 n_fail, n_ops = vec_len (ops);
+  vnet_crypto_op_t *op = ops;
+
+  if (n_ops == 0)
+    return;
+
+  n_fail = n_ops - vnet_crypto_process_chained_ops (vm, op, chunks, n_ops);
+
+  while (n_fail)
+    {
+      ASSERT (op - ops < n_ops);
+
+      if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
+       {
+         u32 bi = op->user_data;
+         b[bi]->error = node->errors[WG_INPUT_ERROR_DECRYPTION];
+         nexts[bi] = drop_next;
+         n_fail--;
+       }
+      op++;
+    }
+}
+
+static_always_inline void
+wg_input_chain_crypto (vlib_main_t *vm, wg_per_thread_data_t *ptd,
+                      vlib_buffer_t *b, vlib_buffer_t *lb, u8 *start,
+                      u32 start_len, u16 *n_ch)
+{
+  vnet_crypto_op_chunk_t *ch;
+  vlib_buffer_t *cb = b;
+  u32 n_chunks = 1;
+
+  vec_add2 (ptd->chunks, ch, 1);
+  ch->len = start_len;
+  ch->src = ch->dst = start;
+  cb = vlib_get_buffer (vm, cb->next_buffer);
+
+  while (1)
+    {
+      vec_add2 (ptd->chunks, ch, 1);
+      n_chunks += 1;
+      if (lb == cb)
+       ch->len = cb->current_length - NOISE_AUTHTAG_LEN;
+      else
+       ch->len = cb->current_length;
+
+      ch->src = ch->dst = vlib_buffer_get_current (cb);
+
+      if (!(cb->flags & VLIB_BUFFER_NEXT_PRESENT))
+       break;
+
+      cb = vlib_get_buffer (vm, cb->next_buffer);
+    }
+
+  if (n_ch)
+    *n_ch = n_chunks;
+}
+
 always_inline void
-wg_prepare_sync_dec_op (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
-                       u8 *src, u32 src_len, u8 *dst, u8 *aad, u32 aad_len,
+wg_prepare_sync_dec_op (vlib_main_t *vm, wg_per_thread_data_t *ptd,
+                       vlib_buffer_t *b, vlib_buffer_t *lb,
+                       vnet_crypto_op_t **crypto_ops, u8 *src, u32 src_len,
+                       u8 *dst, u8 *aad, u32 aad_len,
                        vnet_crypto_key_index_t key_index, u32 bi, u8 *iv)
 {
   vnet_crypto_op_t _op, *op = &_op;
@@ -434,16 +517,28 @@ wg_prepare_sync_dec_op (vlib_main_t *vm, vnet_crypto_op_t **crypto_ops,
   vnet_crypto_op_init (op, VNET_CRYPTO_OP_CHACHA20_POLY1305_DEC);
 
   op->tag_len = NOISE_AUTHTAG_LEN;
-  op->tag = src + src_len;
-  op->src = !src ? src_ : src;
-  op->len = src_len;
-  op->dst = dst;
+  op->tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
   op->key_index = key_index;
   op->aad = aad;
   op->aad_len = aad_len;
   op->iv = iv;
   op->user_data = bi;
   op->flags |= VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
+
+  if (b != lb)
+    {
+      /* Chained buffers */
+      op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
+      op->chunk_index = vec_len (ptd->chunks);
+      wg_input_chain_crypto (vm, ptd, b, lb, src, src_len + NOISE_AUTHTAG_LEN,
+                            &op->n_chunks);
+    }
+  else
+    {
+      op->src = !src ? src_ : src;
+      op->len = src_len;
+      op->dst = dst;
+    }
 }
 
 static_always_inline void
@@ -474,10 +569,10 @@ static_always_inline enum noise_state_crypt
 wg_input_process (vlib_main_t *vm, wg_per_thread_data_t *ptd,
                  vnet_crypto_op_t **crypto_ops,
                  vnet_crypto_async_frame_t **async_frame, vlib_buffer_t *b,
-                 u32 buf_idx, noise_remote_t *r, uint32_t r_idx,
-                 uint64_t nonce, uint8_t *src, size_t srclen, uint8_t *dst,
-                 u32 from_idx, u8 *iv, f64 time, u8 is_async,
-                 u16 async_next_node)
+                 vlib_buffer_t *lb, u32 buf_idx, noise_remote_t *r,
+                 uint32_t r_idx, uint64_t nonce, uint8_t *src, size_t srclen,
+                 size_t srclen_total, uint8_t *dst, u32 from_idx, u8 *iv,
+                 f64 time, u8 is_async, u16 async_next_node)
 {
   noise_keypair_t *kp;
   enum noise_state_crypt ret = SC_FAILED;
@@ -505,23 +600,31 @@ wg_input_process (vlib_main_t *vm, wg_per_thread_data_t *ptd,
 
   if (is_async)
     {
+      u8 flags = VNET_CRYPTO_OP_FLAG_HMAC_CHECK;
+      u8 *tag = vlib_buffer_get_tail (lb) - NOISE_AUTHTAG_LEN;
+
+      if (b != lb)
+       flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
+
       if (NULL == *async_frame ||
          vnet_crypto_async_frame_is_full (*async_frame))
        {
          *async_frame = vnet_crypto_async_get_frame (
            vm, VNET_CRYPTO_OP_CHACHA20_POLY1305_TAG16_AAD0_DEC);
+         if (PREDICT_FALSE (NULL == *async_frame))
+           goto error;
          /* Save the frame to the list we'll submit at the end */
          vec_add1 (ptd->async_frames, *async_frame);
        }
 
-      wg_input_add_to_frame (vm, *async_frame, kp->kp_recv_index, srclen,
-                            src - b->data, buf_idx, async_next_node, iv,
-                            src + srclen, VNET_CRYPTO_OP_FLAG_HMAC_CHECK);
+      wg_input_add_to_frame (vm, *async_frame, kp->kp_recv_index, srclen_total,
+                            src - b->data, buf_idx, async_next_node, iv, tag,
+                            flags);
     }
   else
     {
-      wg_prepare_sync_dec_op (vm, crypto_ops, src, srclen, dst, NULL, 0,
-                             kp->kp_recv_index, from_idx, iv);
+      wg_prepare_sync_dec_op (vm, ptd, b, lb, crypto_ops, src, srclen, dst,
+                             NULL, 0, kp->kp_recv_index, from_idx, iv);
     }
 
   /* If we've received the handshake confirming data packet then move the
@@ -561,6 +664,26 @@ error:
   return ret;
 }
 
+static_always_inline void
+wg_find_outer_addr_port (vlib_buffer_t *b, ip46_address_t *addr, u16 *port,
+                        u8 is_ip4)
+{
+  if (is_ip4)
+    {
+      ip4_udp_header_t *ip4_udp_hdr =
+       vlib_buffer_get_current (b) - sizeof (ip4_udp_header_t);
+      ip46_address_set_ip4 (addr, &ip4_udp_hdr->ip4.src_address);
+      *port = clib_net_to_host_u16 (ip4_udp_hdr->udp.src_port);
+    }
+  else
+    {
+      ip6_udp_header_t *ip6_udp_hdr =
+       vlib_buffer_get_current (b) - sizeof (ip6_udp_header_t);
+      ip46_address_set_ip6 (addr, &ip6_udp_hdr->ip6.src_address);
+      *port = clib_net_to_host_u16 (ip6_udp_hdr->udp.src_port);
+    }
+}
+
 always_inline uword
 wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
                 vlib_frame_t *frame, u8 is_ip4, u16 async_next_node)
@@ -574,8 +697,9 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
   u32 n_left_from = frame->n_vectors;
 
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
+  vlib_buffer_t *lb;
   u32 thread_index = vm->thread_index;
-  vnet_crypto_op_t **crypto_ops = &ptd->crypto_ops;
+  vnet_crypto_op_t **crypto_ops;
   const u16 drop_next = WG_INPUT_NEXT_PUNT;
   message_type_t header_type;
   vlib_buffer_t *data_bufs[VLIB_FRAME_SIZE];
@@ -589,6 +713,8 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 
   vlib_get_buffers (vm, from, bufs, n_left_from);
   vec_reset_length (ptd->crypto_ops);
+  vec_reset_length (ptd->chained_crypto_ops);
+  vec_reset_length (ptd->chunks);
   vec_reset_length (ptd->async_frames);
 
   f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
@@ -599,6 +725,7 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 
   bool is_keepalive = false;
   u32 *peer_idx = NULL;
+  index_t peeri = INDEX_INVALID;
 
   while (n_left_from > 0)
     {
@@ -623,6 +750,7 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
          message_data_t *data = vlib_buffer_get_current (b[0]);
          u8 *iv_data = b[0]->pre_data;
          u32 buf_idx = from[b - bufs];
+         u32 n_bufs;
          peer_idx = wg_index_table_lookup (&wmp->index_table,
                                            data->receiver_index);
 
@@ -632,9 +760,15 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
                                                data->receiver_index);
              if (PREDICT_TRUE (peer_idx != NULL))
                {
-                 peer = wg_peer_get (*peer_idx);
+                 peeri = *peer_idx;
+                 peer = wg_peer_get (peeri);
+                 last_rec_idx = data->receiver_index;
+               }
+             else
+               {
+                 peer = NULL;
+                 last_rec_idx = ~0;
                }
-             last_rec_idx = data->receiver_index;
            }
 
          if (PREDICT_FALSE (!peer_idx))
@@ -663,21 +797,63 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
              goto next;
            }
 
-         u16 encr_len = b[0]->current_length - sizeof (message_data_t);
-         u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
-         if (PREDICT_FALSE (decr_len >= WG_DEFAULT_DATA_SIZE))
+         lb = b[0];
+         n_bufs = vlib_buffer_chain_linearize (vm, b[0]);
+         if (n_bufs == 0)
            {
-             b[0]->error = node->errors[WG_INPUT_ERROR_TOO_BIG];
+             other_next[n_other] = WG_INPUT_NEXT_ERROR;
+             b[0]->error = node->errors[WG_INPUT_ERROR_NO_BUFFERS];
              other_bi[n_other] = buf_idx;
              n_other += 1;
              goto out;
            }
 
-         enum noise_state_crypt state_cr = wg_input_process (
-           vm, ptd, crypto_ops, &async_frame, b[0], buf_idx, &peer->remote,
-           data->receiver_index, data->counter, data->encrypted_data,
-           decr_len, data->encrypted_data, n_data, iv_data, time, is_async,
-           async_next_node);
+         if (n_bufs > 1)
+           {
+             vlib_buffer_t *before_last = b[0];
+
+             /* Find last and before last buffer in the chain */
+             while (lb->flags & VLIB_BUFFER_NEXT_PRESENT)
+               {
+                 before_last = lb;
+                 lb = vlib_get_buffer (vm, lb->next_buffer);
+               }
+
+             /* Ensure auth tag is contiguous and not splitted into two last
+              * buffers */
+             if (PREDICT_FALSE (lb->current_length < NOISE_AUTHTAG_LEN))
+               {
+                 u32 len_diff = NOISE_AUTHTAG_LEN - lb->current_length;
+
+                 before_last->current_length -= len_diff;
+                 if (before_last == b[0])
+                   before_last->flags &= ~VLIB_BUFFER_TOTAL_LENGTH_VALID;
+
+                 vlib_buffer_advance (lb, (signed) -len_diff);
+
+                 clib_memcpy_fast (vlib_buffer_get_current (lb),
+                                   vlib_buffer_get_tail (before_last),
+                                   len_diff);
+               }
+           }
+
+         u16 encr_len = b[0]->current_length - sizeof (message_data_t);
+         u16 decr_len = encr_len - NOISE_AUTHTAG_LEN;
+         u16 encr_len_total =
+           vlib_buffer_length_in_chain (vm, b[0]) - sizeof (message_data_t);
+         u16 decr_len_total = encr_len_total - NOISE_AUTHTAG_LEN;
+
+         if (lb != b[0])
+           crypto_ops = &ptd->chained_crypto_ops;
+         else
+           crypto_ops = &ptd->crypto_ops;
+
+         enum noise_state_crypt state_cr =
+           wg_input_process (vm, ptd, crypto_ops, &async_frame, b[0], lb,
+                             buf_idx, &peer->remote, data->receiver_index,
+                             data->counter, data->encrypted_data, decr_len,
+                             decr_len_total, data->encrypted_data, n_data,
+                             iv_data, time, is_async, async_next_node);
 
          if (PREDICT_FALSE (state_cr == SC_FAILED))
            {
@@ -706,7 +882,7 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
            }
          else if (PREDICT_FALSE (state_cr == SC_KEEP_KEY_FRESH))
            {
-             wg_send_handshake_from_mt (*peer_idx, false);
+             wg_send_handshake_from_mt (peeri, false);
              goto next;
            }
          else if (PREDICT_TRUE (state_cr == SC_OK))
@@ -714,8 +890,6 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
        }
       else
        {
-         peer_idx = NULL;
-
          /* Handshake packets should be processed in main thread */
          if (thread_index != 0)
            {
@@ -749,7 +923,7 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
          t->type = header_type;
          t->current_length = b[0]->current_length;
          t->is_keepalive = is_keepalive;
-         t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
+         t->peer = peer_idx ? peeri : INDEX_INVALID;
        }
 
     next:
@@ -760,6 +934,8 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
   /* decrypt packets */
   wg_input_process_ops (vm, node, ptd->crypto_ops, data_bufs, data_nexts,
                        drop_next);
+  wg_input_process_chained_ops (vm, node, ptd->chained_crypto_ops, data_bufs,
+                               data_nexts, ptd->chunks, drop_next);
 
   /* process after decryption */
   b = data_bufs;
@@ -787,23 +963,50 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
        }
 
       message_data_t *data = vlib_buffer_get_current (b[0]);
+      ip46_address_t out_src_ip;
+      u16 out_udp_src_port;
+
+      wg_find_outer_addr_port (b[0], &out_src_ip, &out_udp_src_port, is_ip4);
 
       if (data->receiver_index != last_rec_idx)
        {
          peer_idx =
            wg_index_table_lookup (&wmp->index_table, data->receiver_index);
-         peer = wg_peer_get (*peer_idx);
-         last_rec_idx = data->receiver_index;
+         if (PREDICT_TRUE (peer_idx != NULL))
+           {
+             peeri = *peer_idx;
+             peer = wg_peer_get (peeri);
+             last_rec_idx = data->receiver_index;
+           }
+         else
+           {
+             peer = NULL;
+             last_rec_idx = ~0;
+           }
        }
 
-      if (PREDICT_FALSE (wg_input_post_process (vm, b[0], data_next, peer,
-                                               data, &is_keepalive) < 0))
-       goto trace;
+      if (PREDICT_TRUE (peer != NULL))
+       {
+         if (PREDICT_FALSE (wg_input_post_process (vm, b[0], data_next, peer,
+                                                   data, &is_keepalive) < 0))
+           goto trace;
+       }
+      else
+       {
+         data_next[0] = WG_INPUT_NEXT_PUNT;
+         goto trace;
+       }
 
       if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
        {
+         if (PREDICT_FALSE (
+               !ip46_address_is_equal (&peer->dst.addr, &out_src_ip) ||
+               peer->dst.port != out_udp_src_port))
+           wg_peer_update_endpoint_from_mt (peeri, &out_src_ip,
+                                            out_udp_src_port);
          wg_timers_any_authenticated_packet_received_opt (peer, time);
          wg_timers_any_authenticated_packet_traversal (peer);
+         wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, true);
          last_peer_time_idx = peer_idx;
        }
 
@@ -820,7 +1023,7 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
          t->type = header_type;
          t->current_length = b[0]->current_length;
          t->is_keepalive = is_keepalive;
-         t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
+         t->peer = peer_idx ? peeri : INDEX_INVALID;
        }
 
       b += 1;
@@ -869,7 +1072,8 @@ wg_input_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
 }
 
 always_inline uword
-wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
+wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame,
+              u8 is_ip4)
 {
   vnet_main_t *vnm = vnet_get_main ();
   vnet_interface_main_t *im = &vnm->interface_main;
@@ -881,6 +1085,7 @@ wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
   wg_peer_t *peer = NULL;
   u32 *peer_idx = NULL;
   u32 *last_peer_time_idx = NULL;
+  index_t peeri = INDEX_INVALID;
   u32 last_rec_idx = ~0;
   f64 time = clib_time_now (&vm->clib_time) + vm->time_offset;
 
@@ -904,14 +1109,27 @@ wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
 
       bool is_keepalive = false;
       message_data_t *data = vlib_buffer_get_current (b[0]);
+      ip46_address_t out_src_ip;
+      u16 out_udp_src_port;
+
+      wg_find_outer_addr_port (b[0], &out_src_ip, &out_udp_src_port, is_ip4);
 
       if (data->receiver_index != last_rec_idx)
        {
          peer_idx =
            wg_index_table_lookup (&wmp->index_table, data->receiver_index);
 
-         peer = wg_peer_get (*peer_idx);
-         last_rec_idx = data->receiver_index;
+         if (PREDICT_TRUE (peer_idx != NULL))
+           {
+             peeri = *peer_idx;
+             peer = wg_peer_get (peeri);
+             last_rec_idx = data->receiver_index;
+           }
+         else
+           {
+             peer = NULL;
+             last_rec_idx = ~0;
+           }
        }
 
       if (PREDICT_TRUE (peer != NULL))
@@ -928,8 +1146,14 @@ wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
 
       if (PREDICT_FALSE (peer_idx && (last_peer_time_idx != peer_idx)))
        {
+         if (PREDICT_FALSE (
+               !ip46_address_is_equal (&peer->dst.addr, &out_src_ip) ||
+               peer->dst.port != out_udp_src_port))
+           wg_peer_update_endpoint_from_mt (peeri, &out_src_ip,
+                                            out_udp_src_port);
          wg_timers_any_authenticated_packet_received_opt (peer, time);
          wg_timers_any_authenticated_packet_traversal (peer);
+         wg_peer_update_flags (*peer_idx, WG_PEER_ESTABLISHED, true);
          last_peer_time_idx = peer_idx;
        }
 
@@ -945,7 +1169,7 @@ wg_input_post (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
          wg_input_post_trace_t *t =
            vlib_add_trace (vm, node, b[0], sizeof (*t));
          t->next = next[0];
-         t->peer = peer_idx ? *peer_idx : INDEX_INVALID;
+         t->peer = peer_idx ? peeri : INDEX_INVALID;
        }
 
       b += 1;
@@ -974,13 +1198,13 @@ VLIB_NODE_FN (wg6_input_node)
 VLIB_NODE_FN (wg4_input_post_node)
 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
 {
-  return wg_input_post (vm, node, from_frame);
+  return wg_input_post (vm, node, from_frame, /* is_ip4 */ 1);
 }
 
 VLIB_NODE_FN (wg6_input_post_node)
 (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
 {
-  return wg_input_post (vm, node, from_frame);
+  return wg_input_post (vm, node, from_frame, /* is_ip4 */ 0);
 }
 
 /* *INDENT-OFF* */