wireguard: add processing of received cookie messages
[vpp.git] / src / plugins / wireguard / wireguard.c
1 /*
2  * Copyright (c) 2020 Doc.ai 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
16 #include <vnet/vnet.h>
17 #include <vnet/plugin/plugin.h>
18 #include <vpp/app/version.h>
19 #include <vnet/crypto/crypto.h>
20
21 #include <wireguard/wireguard_send.h>
22 #include <wireguard/wireguard_key.h>
23 #include <wireguard/wireguard_if.h>
24 #include <wireguard/wireguard.h>
25
26 wg_main_t wg_main;
27 wg_async_post_next_t wg_encrypt_async_next;
28 wg_async_post_next_t wg_decrypt_async_next;
29
30 void
31 wg_set_async_mode (u32 is_enabled)
32 {
33   vnet_crypto_request_async_mode (is_enabled);
34
35   if (is_enabled)
36     wg_op_mode_set_ASYNC ();
37   else
38     wg_op_mode_unset_ASYNC ();
39 }
40
41 static void
42 wireguard_register_post_node (vlib_main_t *vm)
43
44 {
45   wg_async_post_next_t *eit;
46   wg_async_post_next_t *dit;
47
48   eit = &wg_encrypt_async_next;
49   dit = &wg_decrypt_async_next;
50
51   eit->wg4_post_next =
52     vnet_crypto_register_post_node (vm, "wg4-output-tun-post-node");
53   eit->wg6_post_next =
54     vnet_crypto_register_post_node (vm, "wg6-output-tun-post-node");
55
56   dit->wg4_post_next =
57     vnet_crypto_register_post_node (vm, "wg4-input-post-node");
58   dit->wg6_post_next =
59     vnet_crypto_register_post_node (vm, "wg6-input-post-node");
60 }
61
62 void
63 wg_secure_zero_memory (void *v, size_t n)
64 {
65   static void *(*const volatile memset_v) (void *, int, size_t) = &memset;
66   memset_v (v, 0, n);
67 }
68
69 static clib_error_t *
70 wg_init (vlib_main_t * vm)
71 {
72   wg_main_t *wmp = &wg_main;
73
74   wmp->vlib_main = vm;
75
76   wmp->in4_fq_index = vlib_frame_queue_main_init (wg4_input_node.index, 0);
77   wmp->in6_fq_index = vlib_frame_queue_main_init (wg6_input_node.index, 0);
78   wmp->out4_fq_index =
79     vlib_frame_queue_main_init (wg4_output_tun_node.index, 0);
80   wmp->out6_fq_index =
81     vlib_frame_queue_main_init (wg6_output_tun_node.index, 0);
82
83   vlib_thread_main_t *tm = vlib_get_thread_main ();
84
85   vec_validate_aligned (wmp->per_thread_data, tm->n_vlib_mains,
86                         CLIB_CACHE_LINE_BYTES);
87
88   wg_timer_wheel_init ();
89   wireguard_register_post_node (vm);
90   wmp->op_mode_flags = 0;
91
92   return (NULL);
93 }
94
95 VLIB_INIT_FUNCTION (wg_init);
96
97 /* *INDENT-OFF* */
98
99 VNET_FEATURE_INIT (wg4_output_tun, static) = {
100   .arc_name = "ip4-output",
101   .node_name = "wg4-output-tun",
102   .runs_after = VNET_FEATURES ("gso-ip4"),
103 };
104
105 VNET_FEATURE_INIT (wg6_output_tun, static) = {
106   .arc_name = "ip6-output",
107   .node_name = "wg6-output-tun",
108   .runs_after = VNET_FEATURES ("gso-ip6"),
109 };
110
111 VLIB_PLUGIN_REGISTER () =
112 {
113   .version = VPP_BUILD_VER,
114   .description = "Wireguard Protocol",
115 };
116 /* *INDENT-ON* */
117
118 /*
119  * fd.io coding-style-patch-verification: ON
120  *
121  * Local Variables:
122  * eval: (c-set-style "gnu")
123  * End:
124  */