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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
21 #include <wireguard/wireguard.h>
22 #include <wireguard/wireguard_send.h>
24 #define foreach_wg_output_error \
26 _(PEER, "Peer error") \
27 _(KEYPAIR, "Keypair error") \
28 _(TOO_BIG, "packet too big") \
32 #define _(sym,str) WG_OUTPUT_ERROR_##sym,
33 foreach_wg_output_error
38 static char *wg_output_error_strings[] = {
39 #define _(sym,string) string,
40 foreach_wg_output_error
47 WG_OUTPUT_NEXT_HANDOFF,
48 WG_OUTPUT_NEXT_INTERFACE_OUTPUT,
56 } wg_output_tun_trace_t;
59 format_ip4_udp_header (u8 * s, va_list * args)
61 ip4_udp_header_t *hdr = va_arg (*args, ip4_udp_header_t *);
63 s = format (s, "%U:$U",
64 format_ip4_header, &hdr->ip4, format_udp_header, &hdr->udp);
69 /* packet trace format function */
71 format_wg_output_tun_trace (u8 * s, va_list * args)
73 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
74 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
76 wg_output_tun_trace_t *t = va_arg (*args, wg_output_tun_trace_t *);
78 s = format (s, "peer: %d\n", t->peer);
79 s = format (s, " Encrypted packet: %U", format_ip4_udp_header, &t->hdr);
83 VLIB_NODE_FN (wg_output_tun_node) (vlib_main_t * vm,
84 vlib_node_runtime_t * node,
89 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
90 u16 nexts[VLIB_FRAME_SIZE], *next;
91 u32 thread_index = vm->thread_index;
93 from = vlib_frame_vector_args (frame);
94 n_left_from = frame->n_vectors;
98 vlib_get_buffers (vm, from, bufs, n_left_from);
100 wg_main_t *wmp = &wg_main;
101 wg_peer_t *peer = NULL;
103 while (n_left_from > 0)
105 ip4_udp_header_t *hdr = vlib_buffer_get_current (b[0]);
106 u8 *plain_data = (vlib_buffer_get_current (b[0]) +
107 sizeof (ip4_udp_header_t));
109 clib_net_to_host_u16 (((ip4_header_t *) plain_data)->length);
112 next[0] = WG_OUTPUT_NEXT_ERROR;
114 wg_peer_get_by_adj_index (vnet_buffer (b[0])->ip.adj_index[VLIB_TX]);
115 peer = wg_peer_get (peeri);
117 if (!peer || peer->is_dead)
119 b[0]->error = node->errors[WG_OUTPUT_ERROR_PEER];
123 if (PREDICT_FALSE (~0 == peer->output_thread_index))
125 /* this is the first packet to use this peer, claim the peer
128 clib_atomic_cmp_and_swap (&peer->output_thread_index, ~0,
129 wg_peer_assign_thread (thread_index));
132 if (PREDICT_TRUE (thread_index != peer->output_thread_index))
134 next[0] = WG_OUTPUT_NEXT_HANDOFF;
138 if (PREDICT_FALSE (!peer->remote.r_current))
140 wg_send_handshake_from_mt (peeri, false);
141 b[0]->error = node->errors[WG_OUTPUT_ERROR_KEYPAIR];
144 size_t encrypted_packet_len = message_data_len (plain_data_len);
147 * Ensure there is enough space to write the encrypted data
150 if (PREDICT_FALSE (encrypted_packet_len >= WG_DEFAULT_DATA_SIZE) ||
151 PREDICT_FALSE ((b[0]->current_data + encrypted_packet_len) >=
152 vlib_buffer_get_default_data_size (vm)))
154 b[0]->error = node->errors[WG_OUTPUT_ERROR_TOO_BIG];
158 message_data_t *encrypted_packet =
159 (message_data_t *) wmp->per_thread_data[thread_index].data;
161 enum noise_state_crypt state;
163 noise_remote_encrypt (vm,
165 &encrypted_packet->receiver_index,
166 &encrypted_packet->counter, plain_data,
168 encrypted_packet->encrypted_data);
170 if (PREDICT_FALSE (state == SC_KEEP_KEY_FRESH))
172 wg_send_handshake_from_mt (peeri, false);
174 else if (PREDICT_FALSE (state == SC_FAILED))
177 wg_send_handshake_from_mt (peeri, false);
181 /* Here we are sure that can send packet to next node */
182 next[0] = WG_OUTPUT_NEXT_INTERFACE_OUTPUT;
183 encrypted_packet->header.type = MESSAGE_DATA;
185 clib_memcpy (plain_data, (u8 *) encrypted_packet, encrypted_packet_len);
187 hdr->udp.length = clib_host_to_net_u16 (encrypted_packet_len +
188 sizeof (udp_header_t));
189 b[0]->current_length = (encrypted_packet_len +
190 sizeof (ip4_header_t) + sizeof (udp_header_t));
191 ip4_header_set_len_w_chksum
192 (&hdr->ip4, clib_host_to_net_u16 (b[0]->current_length));
194 wg_timers_any_authenticated_packet_sent (peer);
195 wg_timers_data_sent (peer);
196 wg_timers_any_authenticated_packet_traversal (peer);
199 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
200 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
202 wg_output_tun_trace_t *t =
203 vlib_add_trace (vm, node, b[0], sizeof (*t));
213 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
214 return frame->n_vectors;
218 VLIB_REGISTER_NODE (wg_output_tun_node) =
220 .name = "wg-output-tun",
221 .vector_size = sizeof (u32),
222 .format_trace = format_wg_output_tun_trace,
223 .type = VLIB_NODE_TYPE_INTERNAL,
224 .n_errors = ARRAY_LEN (wg_output_error_strings),
225 .error_strings = wg_output_error_strings,
226 .n_next_nodes = WG_OUTPUT_N_NEXT,
228 [WG_OUTPUT_NEXT_HANDOFF] = "wg-output-tun-handoff",
229 [WG_OUTPUT_NEXT_INTERFACE_OUTPUT] = "adj-midchain-tx",
230 [WG_OUTPUT_NEXT_ERROR] = "error-drop",
236 * fd.io coding-style-patch-verification: ON
239 * eval: (c-set-style "gnu")