2 * Copyright (c) 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/session/application_interface.h>
17 #include <vnet/session/session.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/dpo/load_balance.h>
22 VPP's application/session API bind/unbind/connect/disconnect calls
26 * TLS server cert and keys to be used for testing only
28 const char test_srv_crt_rsa[] =
29 "-----BEGIN CERTIFICATE-----\r\n"
30 "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
31 "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
32 "MTEwMjEyMTQ0NDA2WhcNMjEwMjEyMTQ0NDA2WjA0MQswCQYDVQQGEwJOTDERMA8G\r\n"
33 "A1UEChMIUG9sYXJTU0wxEjAQBgNVBAMTCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcN\r\n"
34 "AQEBBQADggEPADCCAQoCggEBAMFNo93nzR3RBNdJcriZrA545Do8Ss86ExbQWuTN\r\n"
35 "owCIp+4ea5anUrSQ7y1yej4kmvy2NKwk9XfgJmSMnLAofaHa6ozmyRyWvP7BBFKz\r\n"
36 "NtSj+uGxdtiQwWG0ZlI2oiZTqqt0Xgd9GYLbKtgfoNkNHC1JZvdbJXNG6AuKT2kM\r\n"
37 "tQCQ4dqCEGZ9rlQri2V5kaHiYcPNQEkI7mgM8YuG0ka/0LiqEQMef1aoGh5EGA8P\r\n"
38 "hYvai0Re4hjGYi/HZo36Xdh98yeJKQHFkA4/J/EwyEoO79bex8cna8cFPXrEAjya\r\n"
39 "HT4P6DSYW8tzS1KW2BGiLICIaTla0w+w3lkvEcf36hIBMJcCAwEAAaNNMEswCQYD\r\n"
40 "VR0TBAIwADAdBgNVHQ4EFgQUpQXoZLjc32APUBJNYKhkr02LQ5MwHwYDVR0jBBgw\r\n"
41 "FoAUtFrkpbPe0lL2udWmlQ/rPrzH/f8wDQYJKoZIhvcNAQEFBQADggEBAJxnXClY\r\n"
42 "oHkbp70cqBrsGXLybA74czbO5RdLEgFs7rHVS9r+c293luS/KdliLScZqAzYVylw\r\n"
43 "UfRWvKMoWhHYKp3dEIS4xTXk6/5zXxhv9Rw8SGc8qn6vITHk1S1mPevtekgasY5Y\r\n"
44 "iWQuM3h4YVlRH3HHEMAD1TnAexfXHHDFQGe+Bd1iAbz1/sH9H8l4StwX6egvTK3M\r\n"
45 "wXRwkKkvjKaEDA9ATbZx0mI8LGsxSuCqe9r9dyjmttd47J1p1Rulz3CLzaRcVIuS\r\n"
46 "RRQfaD8neM9c1S/iJ/amTVqJxA1KOdOS5780WhPfSArA+g4qAmSjelc3p4wWpha8\r\n"
47 "zhuYwjVuX6JHG0c=\r\n" "-----END CERTIFICATE-----\r\n";
48 const u32 test_srv_crt_rsa_len = sizeof (test_srv_crt_rsa);
50 const char test_srv_key_rsa[] =
51 "-----BEGIN RSA PRIVATE KEY-----\r\n"
52 "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n"
53 "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n"
54 "2JDBYbRmUjaiJlOqq3ReB30Zgtsq2B+g2Q0cLUlm91slc0boC4pPaQy1AJDh2oIQ\r\n"
55 "Zn2uVCuLZXmRoeJhw81ASQjuaAzxi4bSRr/QuKoRAx5/VqgaHkQYDw+Fi9qLRF7i\r\n"
56 "GMZiL8dmjfpd2H3zJ4kpAcWQDj8n8TDISg7v1t7H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nocJbqCm7IukhXHOBK8CgYEAwqdHB0hqyNSzIOGY7v9abzB6pUdA3BZiQvEs\r\n"
72 "3LjHVd4HPJ2x0N8CgrBIWOE0q8+0hSMmeE96WW/7jD3fPWwCR5zlXknxBQsfv0gP\r\n"
73 "3BC5PR0Qdypz+d+9zfMf625kyit4T/hzwhDveZUzHnk1Cf+IG7Q+TOEnLnWAWBED\r\n"
74 "ISOWmrUCgYAFEmRxgwAc/u+D6t0syCwAYh6POtscq9Y0i9GyWk89NzgC4NdwwbBH\r\n"
75 "4AgahOxIxXx2gxJnq3yfkJfIjwf0s2DyP0kY2y6Ua1OeomPeY9mrIS4tCuDQ6LrE\r\n"
76 "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n"
77 "-----END RSA PRIVATE KEY-----\r\n";
78 const u32 test_srv_key_rsa_len = sizeof (test_srv_key_rsa);
81 session_endpoint_is_local (session_endpoint_t * sep)
83 return (ip_is_zero (&sep->ip, sep->is_ip4)
84 || ip_is_local_host (&sep->ip, sep->is_ip4));
88 session_endpoint_is_zero (session_endpoint_t * sep)
90 return ip_is_zero (&sep->ip, sep->is_ip4);
94 session_endpoint_in_ns (session_endpoint_t * sep)
96 u8 is_zero = ip_is_zero (&sep->ip, sep->is_ip4);
97 if (!is_zero && sep->sw_if_index != ENDPOINT_INVALID_INDEX
98 && !ip_interface_has_address (sep->sw_if_index, &sep->ip, sep->is_ip4))
100 clib_warning ("sw_if_index %u not configured with ip %U",
101 sep->sw_if_index, format_ip46_address, &sep->ip,
105 return (is_zero || ip_is_local (sep->fib_index, &sep->ip, sep->is_ip4));
109 api_parse_session_handle (u64 handle, u32 * session_index, u32 * thread_index)
111 session_manager_main_t *smm = vnet_get_session_manager_main ();
112 stream_session_t *pool;
114 *thread_index = handle & 0xFFFFFFFF;
115 *session_index = handle >> 32;
117 if (*thread_index >= vec_len (smm->sessions))
118 return VNET_API_ERROR_INVALID_VALUE;
120 pool = smm->sessions[*thread_index];
122 if (pool_is_free_index (pool, *session_index))
123 return VNET_API_ERROR_INVALID_VALUE_2;
129 session_endpoint_update_for_app (session_endpoint_t * sep,
132 app_namespace_t *app_ns;
133 app_ns = app_namespace_get (app->ns_index);
136 /* Ask transport and network to bind to/connect using local interface
137 * that "supports" app's namespace. This will fix our local connection
140 sep->sw_if_index = app_ns->sw_if_index;
142 sep->is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
147 vnet_bind_i (u32 app_index, session_endpoint_t * sep, u64 * handle)
149 u64 lh, ll_handle = SESSION_INVALID_HANDLE;
154 app = application_get_if_valid (app_index);
157 SESSION_DBG ("app not attached");
158 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
161 session_endpoint_update_for_app (sep, app);
162 if (!session_endpoint_in_ns (sep))
163 return VNET_API_ERROR_INVALID_VALUE_2;
165 table_index = application_session_table (app,
166 session_endpoint_fib_proto (sep));
167 lh = session_lookup_endpoint_listener (table_index, sep, 1);
168 if (lh != SESSION_INVALID_HANDLE)
169 return VNET_API_ERROR_ADDRESS_IN_USE;
172 * Add session endpoint to local session table. Only binds to "inaddr_any"
173 * (i.e., zero address) are added to local scope table.
175 if (application_has_local_scope (app) && session_endpoint_is_zero (sep))
177 if ((rv = application_start_local_listen (app, sep, handle)))
182 if (!application_has_global_scope (app))
183 return (ll_handle == SESSION_INVALID_HANDLE ? -1 : 0);
186 * Add session endpoint to global session table
189 /* Setup listen path down to transport */
190 rv = application_start_listen (app, sep, handle);
191 if (rv && ll_handle != SESSION_INVALID_HANDLE)
192 session_lookup_del_session_endpoint (table_index, sep);
195 * Store in local table listener the index of the transport layer
196 * listener. We'll need local listeners are hit and we need to
197 * return global handle
199 if (ll_handle != SESSION_INVALID_HANDLE)
202 stream_session_t *tl;
203 ll = application_get_local_listener_w_handle (ll_handle);
204 tl = listen_session_get_from_handle (*handle);
205 ll->transport_listener_index = tl->session_index;
211 vnet_unbind_i (u32 app_index, session_handle_t handle)
216 if (!(app = application_get_if_valid (app_index)))
218 SESSION_DBG ("app (%d) not attached", app_index);
219 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
222 if (application_has_local_scope (app))
224 if ((rv = application_stop_local_listen (app, handle)))
229 * Clear the global scope table of the listener
231 if (application_has_global_scope (app))
232 return application_stop_listen (app, handle);
237 application_connect (u32 client_index, u32 api_context,
238 session_endpoint_t * sep)
240 application_t *server, *client;
241 u32 table_index, server_index, li;
242 stream_session_t *listener;
246 if (session_endpoint_is_zero (sep))
247 return VNET_API_ERROR_INVALID_VALUE;
249 client = application_get (client_index);
250 session_endpoint_update_for_app (sep, client);
253 * First check the local scope for locally attached destinations.
254 * If we have local scope, we pass *all* connects through it since we may
255 * have special policy rules even for non-local destinations, think proxy.
257 if (application_has_local_scope (client))
259 table_index = application_local_session_table (client);
260 lh = session_lookup_local_endpoint (table_index, sep);
261 if (lh == SESSION_DROP_HANDLE)
262 return VNET_API_ERROR_APP_CONNECT_FILTERED;
264 if (lh == SESSION_INVALID_HANDLE)
267 local_session_parse_handle (lh, &server_index, &li);
270 * Break loop if rule in local table points to connecting app. This
271 * can happen if client is a generic proxy. Route connect through
272 * global table instead.
274 if (server_index != client_index)
276 server = application_get (server_index);
277 ll = application_get_local_listen_session (server, li);
278 return application_local_session_connect (table_index, client,
279 server, ll, api_context);
284 * If nothing found, check the global scope for locally attached
285 * destinations. Make sure first that we're allowed to.
289 if (session_endpoint_is_local (sep))
290 return VNET_API_ERROR_SESSION_CONNECT;
292 if (!application_has_global_scope (client))
293 return VNET_API_ERROR_APP_CONNECT_SCOPE;
295 table_index = application_session_table (client,
296 session_endpoint_fib_proto (sep));
297 listener = session_lookup_listener (table_index, sep);
300 server = application_get (listener->app_index);
302 return application_local_session_connect (table_index, client, server,
304 listener, api_context);
308 * Not connecting to a local server, propagate to transport
310 if (application_open_session (client, sep, api_context))
311 return VNET_API_ERROR_SESSION_CONNECT;
316 * unformat a vnet URI
318 * transport-proto://ip46-addr:port
319 * eg. tcp://ip46-addr:port
321 * u8 ip46_address[16];
322 * u16 port_in_host_byte_order;
323 * stream_session_type_t sst;
326 * if (unformat (input, "%U", unformat_vnet_uri, &ip46_address,
327 * &sst, &port, &fifo_name))
332 unformat_vnet_uri (unformat_input_t * input, va_list * args)
334 session_endpoint_t *sep = va_arg (*args, session_endpoint_t *);
335 u32 transport_proto = 0, port;
338 (input, "%U://%U/%d", unformat_transport_proto, &transport_proto,
339 unformat_ip4_address, &sep->ip.ip4, &port))
341 sep->transport_proto = transport_proto;
342 sep->port = clib_host_to_net_u16 (port);
346 else if (unformat (input, "%U://%U/%d", unformat_transport_proto,
347 &transport_proto, unformat_ip6_address, &sep->ip.ip6,
350 sep->transport_proto = transport_proto;
351 sep->port = clib_host_to_net_u16 (port);
358 static u8 *cache_uri;
359 static session_endpoint_t *cache_sep;
362 parse_uri (char *uri, session_endpoint_t * sep)
364 unformat_input_t _input, *input = &_input;
366 if (cache_uri && !strncmp (uri, (char *) cache_uri, vec_len (cache_uri)))
373 uri = (char *) format (0, "%s%c", uri, 0);
376 unformat_init_string (input, uri, strlen (uri));
377 if (!unformat (input, "%U", unformat_vnet_uri, sep))
379 unformat_free (input);
380 return VNET_API_ERROR_INVALID_VALUE;
382 unformat_free (input);
384 vec_free (cache_uri);
385 cache_uri = (u8 *) uri;
387 clib_mem_free (cache_sep);
388 cache_sep = clib_mem_alloc (sizeof (*sep));
395 session_validate_namespace (u8 * namespace_id, u64 secret, u32 * app_ns_index)
397 app_namespace_t *app_ns;
398 if (vec_len (namespace_id) == 0)
400 /* Use default namespace */
405 *app_ns_index = app_namespace_index_from_id (namespace_id);
406 if (*app_ns_index == APP_NAMESPACE_INVALID_INDEX)
407 return VNET_API_ERROR_APP_INVALID_NS;
408 app_ns = app_namespace_get (*app_ns_index);
410 return VNET_API_ERROR_APP_INVALID_NS;
411 if (app_ns->ns_secret != secret)
412 return VNET_API_ERROR_APP_WRONG_NS_SECRET;
417 * Attach application to vpp
419 * Allocates a vpp app, i.e., a structure that keeps back pointers
420 * to external app and a segment manager for shared memory fifo based
421 * communication with the external app.
424 vnet_application_attach (vnet_app_attach_args_t * a)
426 svm_fifo_segment_private_t *fs;
427 application_t *app = 0;
428 segment_manager_t *sm;
429 u32 app_ns_index = 0;
433 app = application_lookup (a->api_client_index);
435 return clib_error_return_code (0, VNET_API_ERROR_APP_ALREADY_ATTACHED,
436 0, "app already attached");
438 secret = a->options[APP_OPTIONS_NAMESPACE_SECRET];
439 if ((rv = session_validate_namespace (a->namespace_id, secret,
441 return clib_error_return_code (0, rv, 0, "namespace validation: %d", rv);
442 a->options[APP_OPTIONS_NAMESPACE] = app_ns_index;
443 app = application_new ();
444 if ((rv = application_init (app, a->api_client_index, a->options,
446 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
448 a->app_event_queue_address = pointer_to_uword (app->event_queue);
449 sm = segment_manager_get (app->first_segment_manager);
450 fs = segment_manager_get_segment_w_lock (sm, 0);
452 if (application_is_proxy (app))
453 application_setup_proxy (app);
455 ASSERT (vec_len (fs->ssvm.name) <= 128);
456 a->segment = &fs->ssvm;
457 a->app_index = app->index;
459 segment_manager_segment_reader_unlock (sm);
465 * Detach application from vpp
468 vnet_application_detach (vnet_app_detach_args_t * a)
471 app = application_get_if_valid (a->app_index);
475 clib_warning ("app not attached");
476 return VNET_API_ERROR_APPLICATION_NOT_ATTACHED;
479 application_del (app);
484 vnet_bind_uri (vnet_bind_args_t * a)
486 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
489 rv = parse_uri (a->uri, &sep);
493 return vnet_bind_i (a->app_index, &sep, &a->handle);
497 vnet_unbind_uri (vnet_unbind_args_t * a)
499 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
500 stream_session_t *listener;
503 rv = parse_uri (a->uri, &sep);
507 /* NOTE: only default table supported for uri */
508 listener = session_lookup_listener (0, &sep);
510 return VNET_API_ERROR_ADDRESS_NOT_IN_USE;
512 return vnet_unbind_i (a->app_index, listen_session_get_handle (listener));
516 vnet_connect_uri (vnet_connect_args_t * a)
518 session_endpoint_t sep = SESSION_ENDPOINT_NULL;
522 rv = parse_uri (a->uri, &sep);
524 return clib_error_return_code (0, rv, 0, "app init: %d", rv);
526 if ((rv = application_connect (a->app_index, a->api_context, &sep)))
527 return clib_error_return_code (0, rv, 0, "connect failed");
532 vnet_disconnect_session (vnet_disconnect_args_t * a)
534 if (session_handle_is_local (a->handle))
537 ls = application_get_local_session_from_handle (a->handle);
538 if (ls->app_index != a->app_index && ls->client_index != a->app_index)
540 clib_warning ("app %u is neither client nor server for session %u",
541 a->app_index, a->app_index);
542 return VNET_API_ERROR_INVALID_VALUE;
544 return application_local_session_disconnect (a->app_index, ls);
549 s = session_get_from_handle_if_valid (a->handle);
550 if (!s || s->app_index != a->app_index)
551 return VNET_API_ERROR_INVALID_VALUE;
553 /* We're peeking into another's thread pool. Make sure */
554 ASSERT (s->session_index == session_index_from_handle (a->handle));
556 stream_session_disconnect (s);
562 vnet_bind (vnet_bind_args_t * a)
565 if ((rv = vnet_bind_i (a->app_index, &a->sep, &a->handle)))
566 return clib_error_return_code (0, rv, 0, "bind failed");
571 vnet_unbind (vnet_unbind_args_t * a)
574 if ((rv = vnet_unbind_i (a->app_index, a->handle)))
575 return clib_error_return_code (0, rv, 0, "unbind failed");
580 vnet_connect (vnet_connect_args_t * a)
582 session_endpoint_t *sep = &a->sep;
585 if ((rv = application_connect (a->app_index, a->api_context, sep)))
586 return clib_error_return_code (0, rv, 0, "connect failed");
591 * fd.io coding-style-patch-verification: ON
594 * eval: (c-set-style "gnu")