wireguard: initial implementation of wireguard protocol
[vpp.git] / src / plugins / wireguard / wireguard_if.h
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 #ifndef __WG_ITF_H__
17 #define __WG_ITF_H__
18
19 #include <wireguard/wireguard_index_table.h>
20 #include <wireguard/wireguard_messages.h>
21
22 typedef struct wg_if_t_
23 {
24   int user_instance;
25   u32 sw_if_index;
26
27   // Interface params
28   noise_local_t local;
29   cookie_checker_t cookie_checker;
30   u16 port;
31
32   wg_index_table_t index_table;
33
34   /* Source IP address for originated packets */
35   ip_address_t src_ip;
36
37   /* hash table of peers on this link */
38   uword *peers;
39 } wg_if_t;
40
41
42 int wg_if_create (u32 user_instance,
43                   const u8 private_key_64[NOISE_PUBLIC_KEY_LEN],
44                   u16 port, const ip_address_t * src_ip, u32 * sw_if_indexp);
45 int wg_if_delete (u32 sw_if_index);
46 index_t wg_if_find_by_sw_if_index (u32 sw_if_index);
47
48 u8 *format_wg_if (u8 * s, va_list * va);
49
50 typedef walk_rc_t (*wg_if_walk_cb_t) (index_t wgi, void *data);
51 void wg_if_walk (wg_if_walk_cb_t fn, void *data);
52
53 typedef walk_rc_t (*wg_if_peer_walk_cb_t) (wg_if_t * wgi, index_t peeri,
54                                            void *data);
55 void wg_if_peer_walk (wg_if_t * wgi, wg_if_peer_walk_cb_t fn, void *data);
56
57 void wg_if_peer_add (wg_if_t * wgi, index_t peeri);
58 void wg_if_peer_remove (wg_if_t * wgi, index_t peeri);
59
60 /**
61  * Data-plane exposed functions
62  */
63 extern wg_if_t *wg_if_pool;
64
65 static_always_inline wg_if_t *
66 wg_if_get (index_t wgii)
67 {
68   if (INDEX_INVALID == wgii)
69     return (NULL);
70   return (pool_elt_at_index (wg_if_pool, wgii));
71 }
72
73 extern index_t *wg_if_index_by_port;
74
75 static_always_inline wg_if_t *
76 wg_if_get_by_port (u16 port)
77 {
78   if (vec_len (wg_if_index_by_port) < port)
79     return (NULL);
80   if (INDEX_INVALID == wg_if_index_by_port[port])
81     return (NULL);
82   return (wg_if_get (wg_if_index_by_port[port]));
83 }
84
85
86 #endif
87
88 /*
89  * fd.io coding-style-patch-verification: ON
90  *
91  * Local Variables:
92  * eval: (c-set-style "gnu")
93  * End:
94  */