BFD: SHA1 authentication
[vpp.git] / src / vnet / bfd / bfd_api.c
1 /*
2  *------------------------------------------------------------------
3  * bfd_api.c - bfd api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/bfd/bfd_main.h>
26 #include <vnet/bfd/bfd_api.h>
27
28 #include <vnet/vnet_msg_enum.h>
29
30 #define vl_typedefs             /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_typedefs
33
34 #define vl_endianfun            /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40 #define vl_printfun
41 #include <vnet/vnet_all_api_h.h>
42 #undef vl_printfun
43
44 #include <vlibapi/api_helper_macros.h>
45
46 #define foreach_vpe_api_msg                                \
47   _ (BFD_UDP_ADD, bfd_udp_add)                             \
48   _ (BFD_UDP_DEL, bfd_udp_del)                             \
49   _ (BFD_UDP_SESSION_DUMP, bfd_udp_session_dump)           \
50   _ (BFD_UDP_SESSION_SET_FLAGS, bfd_udp_session_set_flags) \
51   _ (WANT_BFD_EVENTS, want_bfd_events)                     \
52   _ (BFD_AUTH_SET_KEY, bfd_auth_set_key)                   \
53   _ (BFD_AUTH_DEL_KEY, bfd_auth_del_key)                   \
54   _ (BFD_AUTH_KEYS_DUMP, bfd_auth_keys_dump)               \
55   _ (BFD_UDP_AUTH_ACTIVATE, bfd_udp_auth_activate)         \
56   _ (BFD_UDP_AUTH_DEACTIVATE, bfd_udp_auth_deactivate)
57
58 pub_sub_handler (bfd_events, BFD_EVENTS);
59
60 #define BFD_UDP_API_PARAM_COMMON_CODE                                         \
61   ip46_address_t local_addr;                                                  \
62   memset (&local_addr, 0, sizeof (local_addr));                               \
63   ip46_address_t peer_addr;                                                   \
64   memset (&peer_addr, 0, sizeof (peer_addr));                                 \
65   if (mp->is_ipv6)                                                            \
66     {                                                                         \
67       clib_memcpy (&local_addr.ip6, mp->local_addr, sizeof (local_addr.ip6)); \
68       clib_memcpy (&peer_addr.ip6, mp->peer_addr, sizeof (peer_addr.ip6));    \
69     }                                                                         \
70   else                                                                        \
71     {                                                                         \
72       clib_memcpy (&local_addr.ip4, mp->local_addr, sizeof (local_addr.ip4)); \
73       clib_memcpy (&peer_addr.ip4, mp->peer_addr, sizeof (peer_addr.ip4));    \
74     }
75
76 #define BFD_UDP_API_PARAM_FROM_MP(mp) \
77   clib_net_to_host_u32 (mp->sw_if_index), &local_addr, &peer_addr
78
79 static void
80 vl_api_bfd_udp_add_t_handler (vl_api_bfd_udp_add_t * mp)
81 {
82   vl_api_bfd_udp_add_reply_t *rmp;
83   int rv;
84
85   VALIDATE_SW_IF_INDEX (mp);
86
87   BFD_UDP_API_PARAM_COMMON_CODE;
88
89   rv = bfd_udp_add_session (BFD_UDP_API_PARAM_FROM_MP (mp),
90                             clib_net_to_host_u32 (mp->desired_min_tx),
91                             clib_net_to_host_u32 (mp->required_min_rx),
92                             mp->detect_mult, mp->is_authenticated,
93                             clib_net_to_host_u32 (mp->conf_key_id),
94                             mp->bfd_key_id);
95
96   BAD_SW_IF_INDEX_LABEL;
97   REPLY_MACRO (VL_API_BFD_UDP_ADD_REPLY);
98 }
99
100 static void
101 vl_api_bfd_udp_del_t_handler (vl_api_bfd_udp_del_t * mp)
102 {
103   vl_api_bfd_udp_del_reply_t *rmp;
104   int rv;
105
106   VALIDATE_SW_IF_INDEX (mp);
107
108   BFD_UDP_API_PARAM_COMMON_CODE;
109
110   rv = bfd_udp_del_session (BFD_UDP_API_PARAM_FROM_MP (mp));
111
112   BAD_SW_IF_INDEX_LABEL;
113   REPLY_MACRO (VL_API_BFD_UDP_DEL_REPLY);
114 }
115
116 void
117 send_bfd_udp_session_details (unix_shared_memory_queue_t * q, u32 context,
118                               bfd_session_t * bs)
119 {
120   if (bs->transport != BFD_TRANSPORT_UDP4 &&
121       bs->transport != BFD_TRANSPORT_UDP6)
122     {
123       return;
124     }
125
126   vl_api_bfd_udp_session_details_t *mp = vl_msg_api_alloc (sizeof (*mp));
127   memset (mp, 0, sizeof (*mp));
128   mp->_vl_msg_id = ntohs (VL_API_BFD_UDP_SESSION_DETAILS);
129   mp->context = context;
130   mp->state = bs->local_state;
131   bfd_udp_session_t *bus = &bs->udp;
132   bfd_udp_key_t *key = &bus->key;
133   mp->sw_if_index = clib_host_to_net_u32 (key->sw_if_index);
134   mp->is_ipv6 = !(ip46_address_is_ip4 (&key->local_addr));
135   if (mp->is_ipv6)
136     {
137       clib_memcpy (mp->local_addr, &key->local_addr,
138                    sizeof (key->local_addr));
139       clib_memcpy (mp->peer_addr, &key->peer_addr, sizeof (key->peer_addr));
140     }
141   else
142     {
143       clib_memcpy (mp->local_addr, key->local_addr.ip4.data,
144                    sizeof (key->local_addr.ip4.data));
145       clib_memcpy (mp->peer_addr, key->peer_addr.ip4.data,
146                    sizeof (key->peer_addr.ip4.data));
147     }
148
149   vl_msg_api_send_shmem (q, (u8 *) & mp);
150 }
151
152 void
153 bfd_event (bfd_main_t * bm, bfd_session_t * bs)
154 {
155   vpe_api_main_t *vam = &vpe_api_main;
156   vpe_client_registration_t *reg;
157   unix_shared_memory_queue_t *q;
158   /* *INDENT-OFF* */
159   pool_foreach (reg, vam->bfd_events_registrations, ({
160                   q = vl_api_client_index_to_input_queue (reg->client_index);
161                   if (q)
162                     {
163                       switch (bs->transport)
164                         {
165                         case BFD_TRANSPORT_UDP4:
166                         /* fallthrough */
167                         case BFD_TRANSPORT_UDP6:
168                           send_bfd_udp_session_details (q, 0, bs);
169                         }
170                     }
171                 }));
172   /* *INDENT-ON* */
173 }
174
175 static void
176 vl_api_bfd_udp_session_dump_t_handler (vl_api_bfd_udp_session_dump_t * mp)
177 {
178   unix_shared_memory_queue_t *q;
179
180   q = vl_api_client_index_to_input_queue (mp->client_index);
181
182   if (q == 0)
183     return;
184
185   bfd_session_t *bs = NULL;
186   /* *INDENT-OFF* */
187   pool_foreach (bs, bfd_main.sessions, ({
188                   if (bs->transport == BFD_TRANSPORT_UDP4 ||
189                       bs->transport == BFD_TRANSPORT_UDP6)
190                     send_bfd_udp_session_details (q, mp->context, bs);
191                 }));
192   /* *INDENT-ON* */
193 }
194
195 static void
196 vl_api_bfd_udp_session_set_flags_t_handler (vl_api_bfd_udp_session_set_flags_t
197                                             * mp)
198 {
199   vl_api_bfd_udp_session_set_flags_reply_t *rmp;
200   int rv;
201
202   BFD_UDP_API_PARAM_COMMON_CODE;
203
204   rv = bfd_udp_session_set_flags (BFD_UDP_API_PARAM_FROM_MP (mp),
205                                   mp->admin_up_down);
206
207   REPLY_MACRO (VL_API_BFD_UDP_SESSION_SET_FLAGS_REPLY);
208 }
209
210 static void
211 vl_api_bfd_auth_set_key_t_handler (vl_api_bfd_auth_set_key_t * mp)
212 {
213   vl_api_bfd_auth_set_key_reply_t *rmp;
214   int rv = bfd_auth_set_key (clib_net_to_host_u32 (mp->conf_key_id),
215                              mp->auth_type, mp->key_len, mp->key);
216
217   REPLY_MACRO (VL_API_BFD_AUTH_SET_KEY_REPLY);
218 }
219
220 static void
221 vl_api_bfd_auth_del_key_t_handler (vl_api_bfd_auth_del_key_t * mp)
222 {
223   vl_api_bfd_auth_del_key_reply_t *rmp;
224   int rv = bfd_auth_del_key (clib_net_to_host_u32 (mp->conf_key_id));
225
226   REPLY_MACRO (VL_API_BFD_AUTH_DEL_KEY_REPLY);
227 }
228
229 static void
230 vl_api_bfd_auth_keys_dump_t_handler (vl_api_bfd_auth_keys_dump_t * mp)
231 {
232   unix_shared_memory_queue_t *q;
233
234   q = vl_api_client_index_to_input_queue (mp->client_index);
235
236   if (q == 0)
237     return;
238
239   bfd_auth_key_t *key = NULL;
240   vl_api_bfd_auth_keys_details_t *rmp = NULL;
241
242   /* *INDENT-OFF* */
243   pool_foreach (key, bfd_main.auth_keys, ({
244                   rmp = vl_msg_api_alloc (sizeof (*rmp));
245                   memset (rmp, 0, sizeof (*rmp));
246                   rmp->_vl_msg_id = ntohs (VL_API_BFD_AUTH_KEYS_DETAILS);
247                   rmp->context = mp->context;
248                   rmp->conf_key_id = clib_host_to_net_u32 (key->conf_key_id);
249                   rmp->auth_type = key->auth_type;
250                   rmp->use_count = clib_host_to_net_u32 (key->use_count);
251                   vl_msg_api_send_shmem (q, (u8 *)&rmp);
252                 }));
253   /* *INDENT-ON* */
254 }
255
256 static void
257 vl_api_bfd_udp_auth_activate_t_handler (vl_api_bfd_udp_auth_activate_t * mp)
258 {
259   vl_api_bfd_udp_auth_activate_reply_t *rmp;
260   int rv;
261
262   VALIDATE_SW_IF_INDEX (mp);
263
264   BFD_UDP_API_PARAM_COMMON_CODE;
265
266   rv =
267     bfd_udp_auth_activate (BFD_UDP_API_PARAM_FROM_MP (mp),
268                            clib_net_to_host_u32 (mp->conf_key_id),
269                            mp->bfd_key_id, mp->is_delayed);
270
271   BAD_SW_IF_INDEX_LABEL;
272   REPLY_MACRO (VL_API_BFD_UDP_AUTH_ACTIVATE_REPLY);
273 }
274
275 static void
276 vl_api_bfd_udp_auth_deactivate_t_handler (vl_api_bfd_udp_auth_deactivate_t *
277                                           mp)
278 {
279   vl_api_bfd_udp_auth_deactivate_reply_t *rmp;
280   int rv;
281
282   VALIDATE_SW_IF_INDEX (mp);
283
284   BFD_UDP_API_PARAM_COMMON_CODE;
285
286   rv =
287     bfd_udp_auth_deactivate (BFD_UDP_API_PARAM_FROM_MP (mp), mp->is_delayed);
288
289   BAD_SW_IF_INDEX_LABEL;
290   REPLY_MACRO (VL_API_BFD_UDP_AUTH_DEACTIVATE_REPLY);
291 }
292
293 /*
294  * bfd_api_hookup
295  * Add vpe's API message handlers to the table.
296  * vlib has alread mapped shared memory and
297  * added the client registration handlers.
298  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
299  */
300 #define vl_msg_name_crc_list
301 #include <vnet/vnet_all_api_h.h>
302 #undef vl_msg_name_crc_list
303
304 static void
305 setup_message_id_table (api_main_t * am)
306 {
307 #define _(id, n, crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
308   foreach_vl_msg_name_crc_bfd;
309 #undef _
310 }
311
312 static clib_error_t *
313 bfd_api_hookup (vlib_main_t * vm)
314 {
315   api_main_t *am = &api_main;
316
317 #define _(N, n)                                                    \
318   vl_msg_api_set_handlers (VL_API_##N, #n, vl_api_##n##_t_handler, \
319                            vl_noop_handler, vl_api_##n##_t_endian, \
320                            vl_api_##n##_t_print, sizeof (vl_api_##n##_t), 1);
321   foreach_vpe_api_msg;
322 #undef _
323
324   /*
325    * Set up the (msg_name, crc, message-id) table
326    */
327   setup_message_id_table (am);
328
329   return 0;
330 }
331
332 VLIB_API_INIT_FUNCTION (bfd_api_hookup);
333
334 /*
335  * fd.io coding-style-patch-verification: ON
336  *
337  * Local Variables:
338  * eval: (c-set-style "gnu")
339  * End:
340  */