session: first approximation implementation of tls
[vpp.git] / src / vnet / session / session_api.c
1 /*
2  * Copyright (c) 2015-2016 Cisco 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 <vlibmemory/api.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/application_interface.h>
20 #include <vnet/session/session_rules_table.h>
21 #include <vnet/session/session_table.h>
22
23 #include <vnet/vnet_msg_enum.h>
24
25 #define vl_typedefs             /* define message structures */
26 #include <vnet/vnet_all_api_h.h>
27 #undef vl_typedefs
28
29 #define vl_endianfun            /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_endianfun
32
33 /* instantiate all the print functions we know about */
34 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
35 #define vl_printfun
36 #include <vnet/vnet_all_api_h.h>
37 #undef vl_printfun
38
39 #include <vlibapi/api_helper_macros.h>
40
41 #define foreach_session_api_msg                                         \
42 _(MAP_ANOTHER_SEGMENT_REPLY, map_another_segment_reply)                 \
43 _(APPLICATION_ATTACH, application_attach)                               \
44 _(APPLICATION_DETACH, application_detach)                               \
45 _(BIND_URI, bind_uri)                                                   \
46 _(UNBIND_URI, unbind_uri)                                               \
47 _(CONNECT_URI, connect_uri)                                             \
48 _(DISCONNECT_SESSION, disconnect_session)                               \
49 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)                   \
50 _(ACCEPT_SESSION_REPLY, accept_session_reply)                           \
51 _(RESET_SESSION_REPLY, reset_session_reply)                             \
52 _(BIND_SOCK, bind_sock)                                                 \
53 _(UNBIND_SOCK, unbind_sock)                                             \
54 _(CONNECT_SOCK, connect_sock)                                           \
55 _(SESSION_ENABLE_DISABLE, session_enable_disable)                       \
56 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)                         \
57 _(SESSION_RULE_ADD_DEL, session_rule_add_del)                           \
58 _(SESSION_RULES_DUMP, session_rules_dump)                               \
59 _(APPLICATION_TLS_CERT_ADD, application_tls_cert_add)                   \
60 _(APPLICATION_TLS_KEY_ADD, application_tls_key_add)                     \
61
62 static int
63 session_send_memfd_fd (vl_api_registration_t * reg, const ssvm_private_t * sp)
64 {
65   clib_error_t *error;
66   if (vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
67     {
68       clib_warning ("can't send memfd fd");
69       return -1;
70     }
71   error = vl_api_send_fd_msg (reg, sp->fd);
72   if (error)
73     {
74       clib_error_report (error);
75       return -1;
76     }
77   return 0;
78 }
79
80 static int
81 send_add_segment_callback (u32 api_client_index, const ssvm_private_t * sp)
82 {
83   vl_api_map_another_segment_t *mp;
84   vl_api_registration_t *reg;
85
86   reg = vl_mem_api_client_index_to_registration (api_client_index);
87   if (!reg)
88     {
89       clib_warning ("no registration: %u", api_client_index);
90       return -1;
91     }
92
93   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD
94       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
95     {
96       clib_warning ("can't send memfd fd");
97       return -1;
98     }
99
100   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
101   memset (mp, 0, sizeof (*mp));
102   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_MAP_ANOTHER_SEGMENT);
103   mp->segment_size = sp->ssvm_size;
104   strncpy ((char *) mp->segment_name, (char *) sp->name,
105            sizeof (mp->segment_name) - 1);
106
107   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
108
109   if (ssvm_type (sp) == SSVM_SEGMENT_MEMFD)
110     return session_send_memfd_fd (reg, sp);
111
112   return 0;
113 }
114
115 static int
116 send_del_segment_callback (u32 api_client_index, const ssvm_private_t * fs)
117 {
118   vl_api_unmap_segment_t *mp;
119   vl_api_registration_t *reg;
120
121   reg = vl_mem_api_client_index_to_registration (api_client_index);
122   if (!reg)
123     {
124       clib_warning ("no registration: %u", api_client_index);
125       return -1;
126     }
127
128   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD
129       && vl_api_registration_file_index (reg) == VL_API_INVALID_FI)
130     {
131       clib_warning ("can't send memfd fd");
132       return -1;
133     }
134
135   mp = vl_msg_api_alloc_as_if_client (sizeof (*mp));
136   memset (mp, 0, sizeof (*mp));
137   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_UNMAP_SEGMENT);
138   strncpy ((char *) mp->segment_name, (char *) fs->name,
139            sizeof (mp->segment_name) - 1);
140
141   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
142
143   if (ssvm_type (fs) == SSVM_SEGMENT_MEMFD)
144     return session_send_memfd_fd (reg, fs);
145
146   return 0;
147 }
148
149 static int
150 send_session_accept_callback (stream_session_t * s)
151 {
152   application_t *server = application_get (s->app_index);
153   transport_proto_vft_t *tp_vft;
154   vl_api_accept_session_t *mp;
155   vl_api_registration_t *reg;
156   transport_connection_t *tc;
157   stream_session_t *listener;
158   svm_queue_t *vpp_queue;
159
160   reg = vl_mem_api_client_index_to_registration (server->api_client_index);
161   if (!reg)
162     {
163       clib_warning ("no registration: %u", server->api_client_index);
164       return -1;
165     }
166
167   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
168   memset (mp, 0, sizeof (*mp));
169
170   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION);
171   mp->context = server->index;
172   mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
173   mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
174
175   if (session_has_transport (s))
176     {
177       listener = listen_session_get (s->session_type, s->listener_index);
178       mp->listener_handle = listen_session_get_handle (listener);
179       if (application_is_proxy (server))
180         {
181           listener =
182             application_first_listener (server, session_get_fib_proto (s),
183                                         session_get_transport_proto (s));
184           if (listener)
185             mp->listener_handle = listen_session_get_handle (listener);
186         }
187       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
188       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
189       mp->handle = session_handle (s);
190       tp_vft = transport_protocol_get_vft (session_get_transport_proto (s));
191       tc = tp_vft->get_connection (s->connection_index, s->thread_index);
192       mp->port = tc->rmt_port;
193       mp->is_ip4 = tc->is_ip4;
194       clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip));
195     }
196   else
197     {
198       local_session_t *ls = (local_session_t *) s;
199       local_session_t *ll;
200       if (application_local_session_listener_has_transport (ls))
201         {
202           listener = listen_session_get (ls->listener_session_type,
203                                          ls->listener_index);
204           mp->listener_handle = listen_session_get_handle (listener);
205           mp->is_ip4 = session_type_is_ip4 (listener->session_type);
206         }
207       else
208         {
209           ll = application_get_local_listen_session (server,
210                                                      ls->listener_index);
211           if (ll->transport_listener_index != ~0)
212             {
213               listener = listen_session_get (ll->listener_session_type,
214                                              ll->transport_listener_index);
215               mp->listener_handle = listen_session_get_handle (listener);
216             }
217           else
218             {
219               mp->listener_handle = application_local_session_handle (ll);
220             }
221           mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type);
222         }
223       mp->handle = application_local_session_handle (ls);
224       mp->port = ls->port;
225       mp->vpp_event_queue_address = ls->client_evt_q;
226       mp->server_event_queue_address = ls->server_evt_q;
227     }
228   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
229
230   return 0;
231 }
232
233 void
234 send_local_session_disconnect_callback (u32 app_index, local_session_t * ls)
235 {
236   application_t *app = application_get (app_index);
237   vl_api_disconnect_session_t *mp;
238   vl_api_registration_t *reg;
239
240   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
241   if (!reg)
242     {
243       clib_warning ("no registration: %u", app->api_client_index);
244       return;
245     }
246
247   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
248   memset (mp, 0, sizeof (*mp));
249   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
250   mp->handle = application_local_session_handle (ls);
251   mp->context = app->api_client_index;
252   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
253 }
254
255 static void
256 send_session_disconnect_callback (stream_session_t * s)
257 {
258   application_t *app = application_get (s->app_index);
259   vl_api_disconnect_session_t *mp;
260   vl_api_registration_t *reg;
261
262   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
263   if (!reg)
264     {
265       clib_warning ("no registration: %u", app->api_client_index);
266       return;
267     }
268
269   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
270   memset (mp, 0, sizeof (*mp));
271   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION);
272   mp->handle = session_handle (s);
273   mp->context = app->api_client_index;
274   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
275 }
276
277 static void
278 send_session_reset_callback (stream_session_t * s)
279 {
280   application_t *app = application_get (s->app_index);
281   vl_api_registration_t *reg;
282   vl_api_reset_session_t *mp;
283
284   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
285   if (!reg)
286     {
287       clib_warning ("no registration: %u", app->api_client_index);
288       return;
289     }
290
291   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
292   memset (mp, 0, sizeof (*mp));
293   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION);
294   mp->handle = session_handle (s);
295   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
296 }
297
298 int
299 send_session_connected_callback (u32 app_index, u32 api_context,
300                                  stream_session_t * s, u8 is_fail)
301 {
302   vl_api_connect_session_reply_t *mp;
303   transport_connection_t *tc;
304   vl_api_registration_t *reg;
305   svm_queue_t *vpp_queue;
306   application_t *app;
307
308   app = application_get (app_index);
309   reg = vl_mem_api_client_index_to_registration (app->api_client_index);
310   if (!reg)
311     {
312       clib_warning ("no registration: %u", app->api_client_index);
313       return -1;
314     }
315
316   mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp));
317   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY);
318   mp->context = api_context;
319
320   if (is_fail)
321     goto done;
322
323   if (session_has_transport (s))
324     {
325       tc = session_get_transport (s);
326       if (!tc)
327         {
328           is_fail = 1;
329           goto done;
330         }
331
332       vpp_queue = session_manager_get_vpp_event_queue (s->thread_index);
333       mp->handle = session_handle (s);
334       mp->vpp_event_queue_address = pointer_to_uword (vpp_queue);
335       clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
336       mp->is_ip4 = tc->is_ip4;
337       mp->lcl_port = tc->lcl_port;
338       mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo);
339       mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo);
340     }
341   else
342     {
343       local_session_t *ls = (local_session_t *) s;
344       mp->handle = application_local_session_handle (ls);
345       mp->lcl_port = ls->port;
346       mp->vpp_event_queue_address = ls->server_evt_q;
347       mp->client_event_queue_address = ls->client_evt_q;
348       mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo);
349       mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo);
350     }
351
352 done:
353   mp->retval = is_fail ?
354     clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0;
355   vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp);
356   return 0;
357 }
358
359 static session_cb_vft_t session_cb_vft = {
360   .session_accept_callback = send_session_accept_callback,
361   .session_disconnect_callback = send_session_disconnect_callback,
362   .session_connected_callback = send_session_connected_callback,
363   .session_reset_callback = send_session_reset_callback,
364   .add_segment_callback = send_add_segment_callback,
365   .del_segment_callback = send_del_segment_callback,
366 };
367
368 static void
369 vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp)
370 {
371   vl_api_session_enable_disable_reply_t *rmp;
372   vlib_main_t *vm = vlib_get_main ();
373   int rv = 0;
374
375   vnet_session_enable_disable (vm, mp->is_enable);
376   REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY);
377 }
378
379 static void
380 vl_api_application_attach_t_handler (vl_api_application_attach_t * mp)
381 {
382   vl_api_application_attach_reply_t *rmp;
383   ssvm_private_t *segp, *evt_q_segment;
384   vnet_app_attach_args_t _a, *a = &_a;
385   vl_api_registration_t *reg;
386   clib_error_t *error = 0;
387   int rv = 0;
388
389   reg = vl_api_client_index_to_registration (mp->client_index);
390   if (!reg)
391     return;
392
393   if (session_manager_is_enabled () == 0)
394     {
395       rv = VNET_API_ERROR_FEATURE_DISABLED;
396       goto done;
397     }
398
399   STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <=
400                  sizeof (mp->options),
401                  "Out of options, fix api message definition");
402
403   memset (a, 0, sizeof (*a));
404   a->api_client_index = mp->client_index;
405   a->options = mp->options;
406   a->session_cb_vft = &session_cb_vft;
407
408   if (mp->namespace_id_len > 64)
409     {
410       rv = VNET_API_ERROR_INVALID_VALUE;
411       goto done;
412     }
413
414   if (mp->namespace_id_len)
415     {
416       vec_validate (a->namespace_id, mp->namespace_id_len - 1);
417       clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len);
418     }
419
420   if ((error = vnet_application_attach (a)))
421     {
422       rv = clib_error_get_code (error);
423       clib_error_report (error);
424     }
425   vec_free (a->namespace_id);
426
427 done:
428
429   /* *INDENT-OFF* */
430   REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({
431     if (!rv)
432       {
433         segp = a->segment;
434         rmp->segment_name_length = 0;
435         rmp->segment_size = segp->ssvm_size;
436         if (vec_len (segp->name))
437           {
438             memcpy (rmp->segment_name, segp->name, vec_len (segp->name));
439             rmp->segment_name_length = vec_len (segp->name);
440           }
441         rmp->app_event_queue_address = a->app_event_queue_address;
442       }
443   }));
444   /* *INDENT-ON* */
445
446   if (rv)
447     return;
448
449   /* Send fifo segment fd if needed */
450   if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD)
451     session_send_memfd_fd (reg, a->segment);
452   /* Send event queues segment */
453   if ((evt_q_segment = session_manager_get_evt_q_segment ()))
454     session_send_memfd_fd (reg, evt_q_segment);
455 }
456
457 static void
458 vl_api_application_detach_t_handler (vl_api_application_detach_t * mp)
459 {
460   vl_api_application_detach_reply_t *rmp;
461   int rv = VNET_API_ERROR_INVALID_VALUE_2;
462   vnet_app_detach_args_t _a, *a = &_a;
463   application_t *app;
464
465   if (session_manager_is_enabled () == 0)
466     {
467       rv = VNET_API_ERROR_FEATURE_DISABLED;
468       goto done;
469     }
470
471   app = application_lookup (mp->client_index);
472   if (app)
473     {
474       a->app_index = app->index;
475       rv = vnet_application_detach (a);
476     }
477
478 done:
479   REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY);
480 }
481
482 static void
483 vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp)
484 {
485   vl_api_bind_uri_reply_t *rmp;
486   vnet_bind_args_t _a, *a = &_a;
487   application_t *app;
488   int rv;
489
490   if (session_manager_is_enabled () == 0)
491     {
492       rv = VNET_API_ERROR_FEATURE_DISABLED;
493       goto done;
494     }
495
496   app = application_lookup (mp->client_index);
497   if (app)
498     {
499       memset (a, 0, sizeof (*a));
500       a->uri = (char *) mp->uri;
501       a->app_index = app->index;
502       rv = vnet_bind_uri (a);
503     }
504   else
505     {
506       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
507     }
508
509 done:
510   REPLY_MACRO (VL_API_BIND_URI_REPLY);
511 }
512
513 static void
514 vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp)
515 {
516   vl_api_unbind_uri_reply_t *rmp;
517   application_t *app;
518   vnet_unbind_args_t _a, *a = &_a;
519   int rv;
520
521   if (session_manager_is_enabled () == 0)
522     {
523       rv = VNET_API_ERROR_FEATURE_DISABLED;
524       goto done;
525     }
526
527   app = application_lookup (mp->client_index);
528   if (app)
529     {
530       a->uri = (char *) mp->uri;
531       a->app_index = app->index;
532       rv = vnet_unbind_uri (a);
533     }
534   else
535     {
536       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
537     }
538
539 done:
540   REPLY_MACRO (VL_API_UNBIND_URI_REPLY);
541 }
542
543 static void
544 vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp)
545 {
546   vl_api_connect_session_reply_t *rmp;
547   vnet_connect_args_t _a, *a = &_a;
548   application_t *app;
549   clib_error_t *error = 0;
550   int rv = 0;
551
552   if (session_manager_is_enabled () == 0)
553     {
554       rv = VNET_API_ERROR_FEATURE_DISABLED;
555       goto done;
556     }
557
558   app = application_lookup (mp->client_index);
559   if (app)
560     {
561       a->uri = (char *) mp->uri;
562       a->api_context = mp->context;
563       a->app_index = app->index;
564       a->mp = mp;
565       if ((error = vnet_connect_uri (a)))
566         {
567           rv = clib_error_get_code (error);
568           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
569             clib_error_report (error);
570         }
571     }
572   else
573     {
574       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
575     }
576
577   /*
578    * Don't reply to stream (tcp) connects. The reply will come once
579    * the connection is established. In case of the redirects, the reply
580    * will come from the server app.
581    */
582   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
583     return;
584
585 done:
586   /* *INDENT-OFF* */
587   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
588   /* *INDENT-ON* */
589 }
590
591 static void
592 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
593 {
594   vl_api_disconnect_session_reply_t *rmp;
595   vnet_disconnect_args_t _a, *a = &_a;
596   application_t *app;
597   int rv = 0;
598
599   if (session_manager_is_enabled () == 0)
600     {
601       rv = VNET_API_ERROR_FEATURE_DISABLED;
602       goto done;
603     }
604
605   app = application_lookup (mp->client_index);
606   if (app)
607     {
608       a->handle = mp->handle;
609       a->app_index = app->index;
610       rv = vnet_disconnect_session (a);
611     }
612   else
613     {
614       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
615     }
616
617 done:
618   REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle);
619 }
620
621 static void
622 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
623                                            mp)
624 {
625   vnet_disconnect_args_t _a, *a = &_a;
626   application_t *app;
627
628   /* Client objected to disconnecting the session, log and continue */
629   if (mp->retval)
630     {
631       clib_warning ("client retval %d", mp->retval);
632       return;
633     }
634
635   /* Disconnect has been confirmed. Confirm close to transport */
636   app = application_lookup (mp->context);
637   if (app)
638     {
639       a->handle = mp->handle;
640       a->app_index = app->index;
641       vnet_disconnect_session (a);
642     }
643 }
644
645 static void
646 vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp)
647 {
648   application_t *app;
649   stream_session_t *s;
650   u32 index, thread_index;
651
652   app = application_lookup (mp->client_index);
653   if (!app)
654     return;
655
656   session_parse_handle (mp->handle, &index, &thread_index);
657   s = session_get_if_valid (index, thread_index);
658   if (s == 0 || app->index != s->app_index)
659     {
660       clib_warning ("Invalid session!");
661       return;
662     }
663
664   /* Client objected to resetting the session, log and continue */
665   if (mp->retval)
666     {
667       clib_warning ("client retval %d", mp->retval);
668       return;
669     }
670
671   /* This comes as a response to a reset, transport only waiting for
672    * confirmation to remove connection state, no need to disconnect */
673   stream_session_cleanup (s);
674 }
675
676 static void
677 vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp)
678 {
679   vnet_disconnect_args_t _a = { 0 }, *a = &_a;
680   local_session_t *ls;
681   stream_session_t *s;
682
683   /* Server isn't interested, kill the session */
684   if (mp->retval)
685     {
686       a->app_index = mp->context;
687       a->handle = mp->handle;
688       vnet_disconnect_session (a);
689       return;
690     }
691
692   if (session_handle_is_local (mp->handle))
693     {
694       ls = application_get_local_session_from_handle (mp->handle);
695       if (!ls || ls->app_index != mp->context)
696         {
697           clib_warning ("server %u doesn't own local handle %llu",
698                         mp->context, mp->handle);
699           return;
700         }
701       if (application_local_session_connect_notify (ls))
702         return;
703       ls->session_state = SESSION_STATE_READY;
704     }
705   else
706     {
707       s = session_get_from_handle_if_valid (mp->handle);
708       if (!s)
709         {
710           clib_warning ("session doesn't exist");
711           return;
712         }
713       if (s->app_index != mp->context)
714         {
715           clib_warning ("app doesn't own session");
716           return;
717         }
718       s->session_state = SESSION_STATE_READY;
719     }
720 }
721
722 static void
723 vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t
724                                             * mp)
725 {
726   clib_warning ("not implemented");
727 }
728
729 static void
730 vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp)
731 {
732   vl_api_bind_sock_reply_t *rmp;
733   vnet_bind_args_t _a, *a = &_a;
734   int rv = 0;
735   clib_error_t *error;
736   application_t *app = 0;
737   stream_session_t *s;
738   transport_connection_t *tc = 0;
739   ip46_address_t *ip46;
740
741   if (session_manager_is_enabled () == 0)
742     {
743       rv = VNET_API_ERROR_FEATURE_DISABLED;
744       goto done;
745     }
746
747   app = application_lookup (mp->client_index);
748   if (!app)
749     {
750       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
751       goto done;
752     }
753
754   ip46 = (ip46_address_t *) mp->ip;
755   memset (a, 0, sizeof (*a));
756   a->sep.is_ip4 = mp->is_ip4;
757   a->sep.ip = *ip46;
758   a->sep.port = mp->port;
759   a->sep.fib_index = mp->vrf;
760   a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
761   a->sep.transport_proto = mp->proto;
762   a->app_index = app->index;
763
764   if ((error = vnet_bind (a)))
765     {
766       rv = clib_error_get_code (error);
767       clib_error_report (error);
768     }
769
770 done:
771   /* *INDENT-OFF* */
772   REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({
773     if (!rv)
774       {
775         rmp->handle = a->handle;
776         rmp->lcl_port = mp->port;
777         if (app && application_has_global_scope (app))
778           {
779             s = listen_session_get_from_handle (a->handle);
780             tc = listen_session_get_transport (s);
781             rmp->lcl_is_ip4 = tc->is_ip4;
782             clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip));
783           }
784       }
785   }));
786   /* *INDENT-ON* */
787 }
788
789 static void
790 vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp)
791 {
792   vl_api_unbind_sock_reply_t *rmp;
793   vnet_unbind_args_t _a, *a = &_a;
794   application_t *app;
795   clib_error_t *error;
796   int rv = 0;
797
798   if (session_manager_is_enabled () == 0)
799     {
800       rv = VNET_API_ERROR_FEATURE_DISABLED;
801       goto done;
802     }
803
804   app = application_lookup (mp->client_index);
805   if (app)
806     {
807       a->app_index = app->index;
808       a->handle = mp->handle;
809       if ((error = vnet_unbind (a)))
810         {
811           rv = clib_error_get_code (error);
812           clib_error_report (error);
813         }
814     }
815
816 done:
817   REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY);
818 }
819
820 static void
821 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
822 {
823   vl_api_connect_session_reply_t *rmp;
824   vnet_connect_args_t _a, *a = &_a;
825   application_t *app;
826   clib_error_t *error = 0;
827   int rv = 0;
828
829   if (session_manager_is_enabled () == 0)
830     {
831       rv = VNET_API_ERROR_FEATURE_DISABLED;
832       goto done;
833     }
834
835   app = application_lookup (mp->client_index);
836   if (app)
837     {
838       svm_queue_t *client_q;
839       ip46_address_t *ip46 = (ip46_address_t *) mp->ip;
840
841       client_q = vl_api_client_index_to_input_queue (mp->client_index);
842       mp->client_queue_address = pointer_to_uword (client_q);
843       a->sep.is_ip4 = mp->is_ip4;
844       a->sep.ip = *ip46;
845       a->sep.port = mp->port;
846       a->sep.transport_proto = mp->proto;
847       a->sep.fib_index = mp->vrf;
848       a->sep.sw_if_index = ENDPOINT_INVALID_INDEX;
849       a->api_context = mp->context;
850       a->app_index = app->index;
851       a->mp = mp;
852       if ((error = vnet_connect (a)))
853         {
854           rv = clib_error_get_code (error);
855           if (rv != VNET_API_ERROR_SESSION_REDIRECT)
856             clib_error_report (error);
857         }
858     }
859   else
860     {
861       rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
862     }
863
864   if (rv == 0 || rv == VNET_API_ERROR_SESSION_REDIRECT)
865     return;
866
867   /* Got some error, relay it */
868
869 done:
870   REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY);
871 }
872
873 static void
874 vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp)
875 {
876   vl_api_app_namespace_add_del_reply_t *rmp;
877   clib_error_t *error = 0;
878   u32 appns_index = 0;
879   u8 *ns_id = 0;
880   int rv = 0;
881   if (!session_manager_is_enabled ())
882     {
883       rv = VNET_API_ERROR_FEATURE_DISABLED;
884       goto done;
885     }
886
887   if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id))
888     {
889       rv = VNET_API_ERROR_INVALID_VALUE;
890       goto done;
891     }
892
893   vec_validate (ns_id, mp->namespace_id_len - 1);
894   clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len);
895   vnet_app_namespace_add_del_args_t args = {
896     .ns_id = ns_id,
897     .secret = clib_net_to_host_u64 (mp->secret),
898     .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index),
899     .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id),
900     .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id),
901     .is_add = 1
902   };
903   error = vnet_app_namespace_add_del (&args);
904   if (error)
905     {
906       rv = clib_error_get_code (error);
907       clib_error_report (error);
908     }
909   else
910     {
911       appns_index = app_namespace_index_from_id (ns_id);
912       if (appns_index == APP_NAMESPACE_INVALID_INDEX)
913         {
914           clib_warning ("app ns lookup failed");
915           rv = VNET_API_ERROR_UNSPECIFIED;
916         }
917     }
918   vec_free (ns_id);
919
920   /* *INDENT-OFF* */
921 done:
922   REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({
923     if (!rv)
924       rmp->appns_index = clib_host_to_net_u32 (appns_index);
925   }));
926   /* *INDENT-ON* */
927 }
928
929 static void
930 vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
931 {
932   vl_api_session_rule_add_del_reply_t *rmp;
933   session_rule_add_del_args_t args;
934   session_rule_table_add_del_args_t *table_args = &args.table_args;
935   clib_error_t *error;
936   u8 fib_proto;
937   int rv = 0;
938
939   memset (&args, 0, sizeof (args));
940   fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
941
942   table_args->lcl.fp_len = mp->lcl_plen;
943   table_args->lcl.fp_proto = fib_proto;
944   table_args->rmt.fp_len = mp->rmt_plen;
945   table_args->rmt.fp_proto = fib_proto;
946   table_args->lcl_port = mp->lcl_port;
947   table_args->rmt_port = mp->rmt_port;
948   table_args->action_index = clib_net_to_host_u32 (mp->action_index);
949   table_args->is_add = mp->is_add;
950   mp->tag[sizeof (mp->tag) - 1] = 0;
951   table_args->tag = format (0, "%s", mp->tag);
952   args.appns_index = clib_net_to_host_u32 (mp->appns_index);
953   args.scope = mp->scope;
954   args.transport_proto = mp->transport_proto;
955
956   memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
957   memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
958   ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
959   ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
960   error = vnet_session_rule_add_del (&args);
961   if (error)
962     {
963       rv = clib_error_get_code (error);
964       clib_error_report (error);
965     }
966   vec_free (table_args->tag);
967   REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
968 }
969
970 static void
971 send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local,
972                             u8 transport_proto, u32 appns_index, u8 * tag,
973                             vl_api_registration_t * reg, u32 context)
974 {
975   vl_api_session_rules_details_t *rmp = 0;
976   session_mask_or_match_4_t *match =
977     (session_mask_or_match_4_t *) & rule->match;
978   session_mask_or_match_4_t *mask =
979     (session_mask_or_match_4_t *) & rule->mask;
980
981   rmp = vl_msg_api_alloc (sizeof (*rmp));
982   memset (rmp, 0, sizeof (*rmp));
983   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
984   rmp->context = context;
985
986   rmp->is_ip4 = 1;
987   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
988   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
989   rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip);
990   rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip);
991   rmp->lcl_port = match->lcl_port;
992   rmp->rmt_port = match->rmt_port;
993   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
994   rmp->scope =
995     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
996   rmp->transport_proto = transport_proto;
997   rmp->appns_index = clib_host_to_net_u32 (appns_index);
998   if (tag)
999     {
1000       clib_memcpy (rmp->tag, tag, vec_len (tag));
1001       rmp->tag[vec_len (tag)] = 0;
1002     }
1003
1004   vl_api_send_msg (reg, (u8 *) rmp);
1005 }
1006
1007 static void
1008 send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local,
1009                             u8 transport_proto, u32 appns_index, u8 * tag,
1010                             vl_api_registration_t * reg, u32 context)
1011 {
1012   vl_api_session_rules_details_t *rmp = 0;
1013   session_mask_or_match_6_t *match =
1014     (session_mask_or_match_6_t *) & rule->match;
1015   session_mask_or_match_6_t *mask =
1016     (session_mask_or_match_6_t *) & rule->mask;
1017
1018   rmp = vl_msg_api_alloc (sizeof (*rmp));
1019   memset (rmp, 0, sizeof (*rmp));
1020   rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS);
1021   rmp->context = context;
1022
1023   rmp->is_ip4 = 0;
1024   clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip));
1025   clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip));
1026   rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip);
1027   rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip);
1028   rmp->lcl_port = match->lcl_port;
1029   rmp->rmt_port = match->rmt_port;
1030   rmp->action_index = clib_host_to_net_u32 (rule->action_index);
1031   rmp->scope =
1032     is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL;
1033   rmp->transport_proto = transport_proto;
1034   rmp->appns_index = clib_host_to_net_u32 (appns_index);
1035   if (tag)
1036     {
1037       clib_memcpy (rmp->tag, tag, vec_len (tag));
1038       rmp->tag[vec_len (tag)] = 0;
1039     }
1040
1041   vl_api_send_msg (reg, (u8 *) rmp);
1042 }
1043
1044 static void
1045 send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto,
1046                                   u8 tp, u8 is_local, u32 appns_index,
1047                                   vl_api_registration_t * reg, u32 context)
1048 {
1049   mma_rule_16_t *rule16;
1050   mma_rule_40_t *rule40;
1051   mma_rules_table_16_t *srt16;
1052   mma_rules_table_40_t *srt40;
1053   u32 ri;
1054
1055   if (is_local || fib_proto == FIB_PROTOCOL_IP4)
1056     {
1057       u8 *tag = 0;
1058       /* *INDENT-OFF* */
1059       srt16 = &srt->session_rules_tables_16;
1060       pool_foreach (rule16, srt16->rules, ({
1061         ri = mma_rules_table_rule_index_16 (srt16, rule16);
1062         tag = session_rules_table_rule_tag (srt, ri, 1);
1063         send_session_rule_details4 (rule16, is_local, tp, appns_index, tag,
1064                                     reg, context);
1065       }));
1066       /* *INDENT-ON* */
1067     }
1068   if (is_local || fib_proto == FIB_PROTOCOL_IP6)
1069     {
1070       u8 *tag = 0;
1071       /* *INDENT-OFF* */
1072       srt40 = &srt->session_rules_tables_40;
1073       pool_foreach (rule40, srt40->rules, ({
1074         ri = mma_rules_table_rule_index_40 (srt40, rule40);
1075         tag = session_rules_table_rule_tag (srt, ri, 1);
1076         send_session_rule_details6 (rule40, is_local, tp, appns_index, tag,
1077                                     reg, context);
1078       }));
1079       /* *INDENT-ON* */
1080     }
1081 }
1082
1083 static void
1084 vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp)
1085 {
1086   vl_api_registration_t *reg;
1087   session_table_t *st;
1088   u8 tp;
1089
1090   reg = vl_api_client_index_to_registration (mp->client_index);
1091   if (!reg)
1092     return;
1093
1094   /* *INDENT-OFF* */
1095   session_table_foreach (st, ({
1096     for (tp = 0; tp < TRANSPORT_N_PROTO; tp++)
1097       {
1098         send_session_rules_table_details (&st->session_rules[tp],
1099                                           st->active_fib_proto, tp,
1100                                           st->is_local, st->appns_index, reg,
1101                                           mp->context);
1102       }
1103   }));
1104   /* *INDENT-ON* */
1105 }
1106
1107 static void
1108 vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t *
1109                                            mp)
1110 {
1111   vl_api_app_namespace_add_del_reply_t *rmp;
1112   vnet_app_add_tls_cert_args_t _a, *a = &_a;
1113   clib_error_t *error;
1114   u32 cert_len;
1115   int rv = 0;
1116   if (!session_manager_is_enabled ())
1117     {
1118       rv = VNET_API_ERROR_FEATURE_DISABLED;
1119       goto done;
1120     }
1121   memset (a, 0, sizeof (*a));
1122   a->app_index = clib_net_to_host_u32 (mp->app_index);
1123   cert_len = clib_net_to_host_u16 (mp->cert_len);
1124   vec_validate (a->cert, cert_len);
1125   clib_memcpy (a->cert, mp->cert, cert_len);
1126   if ((error = vnet_app_add_tls_cert (a)))
1127     {
1128       rv = clib_error_get_code (error);
1129       clib_error_report (error);
1130     }
1131   vec_free (a->cert);
1132 done:
1133   REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY);
1134 }
1135
1136 static void
1137 vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t *
1138                                           mp)
1139 {
1140   vl_api_app_namespace_add_del_reply_t *rmp;
1141   vnet_app_add_tls_key_args_t _a, *a = &_a;
1142   clib_error_t *error;
1143   u32 key_len;
1144   int rv = 0;
1145   if (!session_manager_is_enabled ())
1146     {
1147       rv = VNET_API_ERROR_FEATURE_DISABLED;
1148       goto done;
1149     }
1150   memset (a, 0, sizeof (*a));
1151   a->app_index = clib_net_to_host_u32 (mp->app_index);
1152   key_len = clib_net_to_host_u16 (mp->key_len);
1153   vec_validate (a->key, key_len);
1154   clib_memcpy (a->key, mp->key, key_len);
1155   if ((error = vnet_app_add_tls_key (a)))
1156     {
1157       rv = clib_error_get_code (error);
1158       clib_error_report (error);
1159     }
1160   vec_free (a->key);
1161 done:
1162   REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY);
1163 }
1164
1165 static clib_error_t *
1166 application_reaper_cb (u32 client_index)
1167 {
1168   application_t *app = application_lookup (client_index);
1169   vnet_app_detach_args_t _a, *a = &_a;
1170   if (app)
1171     {
1172       a->app_index = app->index;
1173       vnet_application_detach (a);
1174     }
1175   return 0;
1176 }
1177
1178 VL_MSG_API_REAPER_FUNCTION (application_reaper_cb);
1179
1180 #define vl_msg_name_crc_list
1181 #include <vnet/vnet_all_api_h.h>
1182 #undef vl_msg_name_crc_list
1183
1184 static void
1185 setup_message_id_table (api_main_t * am)
1186 {
1187 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1188   foreach_vl_msg_name_crc_session;
1189 #undef _
1190 }
1191
1192 /*
1193  * session_api_hookup
1194  * Add uri's API message handlers to the table.
1195  * vlib has alread mapped shared memory and
1196  * added the client registration handlers.
1197  * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process()
1198  */
1199 static clib_error_t *
1200 session_api_hookup (vlib_main_t * vm)
1201 {
1202   api_main_t *am = &api_main;
1203
1204 #define _(N,n)                                                  \
1205     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1206                            vl_api_##n##_t_handler,              \
1207                            vl_noop_handler,                     \
1208                            vl_api_##n##_t_endian,               \
1209                            vl_api_##n##_t_print,                \
1210                            sizeof(vl_api_##n##_t), 1);
1211   foreach_session_api_msg;
1212 #undef _
1213
1214   /*
1215    * Messages which bounce off the data-plane to
1216    * an API client. Simply tells the message handling infra not
1217    * to free the message.
1218    *
1219    * Bounced message handlers MUST NOT block the data plane
1220    */
1221   am->message_bounce[VL_API_CONNECT_URI] = 1;
1222   am->message_bounce[VL_API_CONNECT_SOCK] = 1;
1223
1224   /*
1225    * Set up the (msg_name, crc, message-id) table
1226    */
1227   setup_message_id_table (am);
1228
1229   return 0;
1230 }
1231
1232 VLIB_API_INIT_FUNCTION (session_api_hookup);
1233
1234 /*
1235  * fd.io coding-style-patch-verification: ON
1236  *
1237  * Local Variables:
1238  * eval: (c-set-style "gnu")
1239  * End:
1240  */