pot_api.c revision b614d083
185b528e0SShwetha/*
285b528e0SShwetha * Copyright (c) 2016 Cisco and/or its affiliates.
385b528e0SShwetha * Licensed under the Apache License, Version 2.0 (the "License");
485b528e0SShwetha * you may not use this file except in compliance with the License.
585b528e0SShwetha * You may obtain a copy of the License at:
685b528e0SShwetha *
785b528e0SShwetha *     http://www.apache.org/licenses/LICENSE-2.0
885b528e0SShwetha *
985b528e0SShwetha * Unless required by applicable law or agreed to in writing, software
1085b528e0SShwetha * distributed under the License is distributed on an "AS IS" BASIS,
1185b528e0SShwetha * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1285b528e0SShwetha * See the License for the specific language governing permissions and
1385b528e0SShwetha * limitations under the License.
1485b528e0SShwetha */
1585b528e0SShwetha/*
1685b528e0SShwetha *------------------------------------------------------------------
1785b528e0SShwetha * pot_api.c - Proof of Transit related APIs to create
1885b528e0SShwetha *             and maintain profiles
1985b528e0SShwetha *------------------------------------------------------------------
2085b528e0SShwetha */
2185b528e0SShwetha
2285b528e0SShwetha#include <vnet/vnet.h>
2385b528e0SShwetha#include <vnet/plugin/plugin.h>
24ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_util.h>
2585b528e0SShwetha
2685b528e0SShwetha#include <vlibapi/api.h>
2785b528e0SShwetha#include <vlibmemory/api.h>
2885b528e0SShwetha#include <vlibsocket/api.h>
2985b528e0SShwetha
3085b528e0SShwetha/* define message IDs */
31ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_msg_enum.h>
3285b528e0SShwetha
3385b528e0SShwetha/* define message structures */
3485b528e0SShwetha#define vl_typedefs
35ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
3685b528e0SShwetha#undef vl_typedefs
3785b528e0SShwetha
3885b528e0SShwetha/* define generated endian-swappers */
3985b528e0SShwetha#define vl_endianfun
40ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
4185b528e0SShwetha#undef vl_endianfun
4285b528e0SShwetha
4385b528e0SShwetha/* instantiate all the print functions we know about */
4485b528e0SShwetha#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
4585b528e0SShwetha#define vl_printfun
46ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
4785b528e0SShwetha#undef vl_printfun
4885b528e0SShwetha
4985b528e0SShwetha/* Get the API version number */
5085b528e0SShwetha#define vl_api_version(n,v) static u32 api_version=(v);
51ea3e1fc8SOle Troan#include <ioam/lib-pot/pot_all_api_h.h>
5285b528e0SShwetha#undef vl_api_version
5385b528e0SShwetha
54b614d083SEyal Bari#define REPLY_MSG_ID_BASE sm->msg_id_base
55b614d083SEyal Bari#include <vlibapi/api_helper_macros.h>
5685b528e0SShwetha
5785b528e0SShwetha/* List of message types that this plugin understands */
5885b528e0SShwetha#define foreach_pot_plugin_api_msg                                      \
5985b528e0SShwetha_(POT_PROFILE_ADD, pot_profile_add)                                     \
6085b528e0SShwetha_(POT_PROFILE_ACTIVATE, pot_profile_activate)                           \
6185b528e0SShwetha_(POT_PROFILE_DEL, pot_profile_del)                                     \
62b4bc55b9SSagar Srivastav_(POT_PROFILE_SHOW_CONFIG_DUMP, pot_profile_show_config_dump)                                     \
6385b528e0SShwetha
6485b528e0SShwethastatic void vl_api_pot_profile_add_t_handler
6585b528e0SShwetha(vl_api_pot_profile_add_t *mp)
6685b528e0SShwetha{
6785b528e0SShwetha    pot_main_t * sm = &pot_main;
6885b528e0SShwetha    int rv = 0;
6985b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
7085b528e0SShwetha    u8 id;
7185b528e0SShwetha    pot_profile *profile = NULL;
7285b528e0SShwetha    u8 *name = 0;
7385b528e0SShwetha
7485b528e0SShwetha    if (mp->list_name_len)
7585b528e0SShwetha        name = format(0, "%s", mp->list_name);
7685b528e0SShwetha
7785b528e0SShwetha    pot_profile_list_init(name);
7885b528e0SShwetha    id = mp->id;
7985b528e0SShwetha    profile = pot_profile_find(id);
8085b528e0SShwetha    if (profile) {
8185b528e0SShwetha	rv = pot_profile_create(profile,
8285b528e0SShwetha				clib_net_to_host_u64(mp->prime),
8385b528e0SShwetha				clib_net_to_host_u64(mp->polynomial_public),
8485b528e0SShwetha				clib_net_to_host_u64(mp->lpc),
8585b528e0SShwetha				clib_net_to_host_u64(mp->secret_share));
8685b528e0SShwetha	if (rv != 0)
8785b528e0SShwetha            goto ERROROUT;
8885b528e0SShwetha	if (1 == mp->validator)
8985b528e0SShwetha	  (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
9085b528e0SShwetha        (void)pot_profile_set_bit_mask(profile, mp->max_bits);
9185b528e0SShwetha    } else {
9285b528e0SShwetha        rv = -3;
9385b528e0SShwetha    }
9485b528e0SShwetha ERROROUT:
9585b528e0SShwetha    vec_free(name);
9685b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
9785b528e0SShwetha}
9885b528e0SShwetha
99b4bc55b9SSagar Srivastavstatic void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
100b4bc55b9SSagar Srivastav{
101b4bc55b9SSagar Srivastav    vl_api_pot_profile_show_config_details_t * rmp;
102b4bc55b9SSagar Srivastav    pot_main_t * sm = &pot_main;
103b4bc55b9SSagar Srivastav    pot_profile *profile = pot_profile_find(id);
104b4bc55b9SSagar Srivastav    int rv = 0;
105b4bc55b9SSagar Srivastav    if(profile){
106b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
107b4bc55b9SSagar Srivastav			rmp->id=id;
108b4bc55b9SSagar Srivastav			rmp->validator=profile->validator;
109b4bc55b9SSagar Srivastav			rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
110b4bc55b9SSagar Srivastav			rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
111b4bc55b9SSagar Srivastav			rmp->prime=clib_host_to_net_u64(profile->prime);
112b4bc55b9SSagar Srivastav			rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
113b4bc55b9SSagar Srivastav			rmp->lpc=clib_host_to_net_u64(profile->lpc);
114b4bc55b9SSagar Srivastav			rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
115b4bc55b9SSagar Srivastav			);
116b4bc55b9SSagar Srivastav    }
117b4bc55b9SSagar Srivastav    else{
118b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
119b4bc55b9SSagar Srivastav			rmp->id=id;
120b4bc55b9SSagar Srivastav			rmp->validator=0;
121b4bc55b9SSagar Srivastav			rmp->secret_key=0;
122b4bc55b9SSagar Srivastav			rmp->secret_share=0;
123b4bc55b9SSagar Srivastav			rmp->prime=0;
124b4bc55b9SSagar Srivastav			rmp->bit_mask=0;
125b4bc55b9SSagar Srivastav			rmp->lpc=0;
126b4bc55b9SSagar Srivastav			rmp->polynomial_public=0;
127b4bc55b9SSagar Srivastav			);
128b4bc55b9SSagar Srivastav    }
129b4bc55b9SSagar Srivastav}
130b4bc55b9SSagar Srivastav
131b4bc55b9SSagar Srivastavstatic void vl_api_pot_profile_show_config_dump_t_handler
132b4bc55b9SSagar Srivastav(vl_api_pot_profile_show_config_dump_t *mp)
133b4bc55b9SSagar Srivastav{
134b4bc55b9SSagar Srivastav    u8 id = mp->id;
135b4bc55b9SSagar Srivastav    u8 dump_call_id = ~0;
136b4bc55b9SSagar Srivastav    if(dump_call_id==id){
137b4bc55b9SSagar Srivastav        for(id=0;id<MAX_POT_PROFILES;id++)
138b4bc55b9SSagar Srivastav	    send_pot_profile_details(mp,id);
139b4bc55b9SSagar Srivastav    }
140b4bc55b9SSagar Srivastav    else
141b4bc55b9SSagar Srivastav        send_pot_profile_details(mp,id);
142b4bc55b9SSagar Srivastav}
143b4bc55b9SSagar Srivastav
14485b528e0SShwethastatic void vl_api_pot_profile_activate_t_handler
14585b528e0SShwetha(vl_api_pot_profile_activate_t *mp)
14685b528e0SShwetha{
14785b528e0SShwetha    pot_main_t * sm = &pot_main;
14885b528e0SShwetha    int rv = 0;
14985b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
15085b528e0SShwetha    u8 id;
15185b528e0SShwetha    u8 *name = NULL;
15285b528e0SShwetha
15385b528e0SShwetha    if (mp->list_name_len)
15485b528e0SShwetha        name = format(0, "%s", mp->list_name);
15585b528e0SShwetha    if (!pot_profile_list_is_enabled(name)) {
15685b528e0SShwetha        rv = -1;
15785b528e0SShwetha    } else {
15885b528e0SShwetha        id = mp->id;
15985b528e0SShwetha	rv = pot_profile_set_active(id);
16085b528e0SShwetha    }
16185b528e0SShwetha
16285b528e0SShwetha    vec_free(name);
16385b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
16485b528e0SShwetha}
16585b528e0SShwetha
16685b528e0SShwetha
16785b528e0SShwethastatic void vl_api_pot_profile_del_t_handler
16885b528e0SShwetha(vl_api_pot_profile_del_t *mp)
16985b528e0SShwetha{
17085b528e0SShwetha    pot_main_t * sm = &pot_main;
17185b528e0SShwetha    int rv = 0;
17285b528e0SShwetha    vl_api_pot_profile_del_reply_t * rmp;
17385b528e0SShwetha
17485b528e0SShwetha    clear_pot_profiles();
17585b528e0SShwetha
17685b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
17785b528e0SShwetha}
17885b528e0SShwetha
17985b528e0SShwetha/* Set up the API message handling tables */
18085b528e0SShwethastatic clib_error_t *
18185b528e0SShwethapot_plugin_api_hookup (vlib_main_t *vm)
18285b528e0SShwetha{
18385b528e0SShwetha  pot_main_t * sm = &pot_main;
18485b528e0SShwetha#define _(N,n)                                                  \
18585b528e0SShwetha    vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
18685b528e0SShwetha                           #n,					\
18785b528e0SShwetha                           vl_api_##n##_t_handler,              \
18885b528e0SShwetha                           vl_noop_handler,                     \
18985b528e0SShwetha                           vl_api_##n##_t_endian,               \
19085b528e0SShwetha                           vl_api_##n##_t_print,                \
19185b528e0SShwetha                           sizeof(vl_api_##n##_t), 1);
19285b528e0SShwetha    foreach_pot_plugin_api_msg;
19385b528e0SShwetha#undef _
19485b528e0SShwetha
19585b528e0SShwetha    return 0;
19685b528e0SShwetha}
19785b528e0SShwetha
198557d128bSDave Barach#define vl_msg_name_crc_list
199557d128bSDave Barach#include <ioam/lib-pot/pot_all_api_h.h>
200557d128bSDave Barach#undef vl_msg_name_crc_list
201557d128bSDave Barach
202557d128bSDave Barachstatic void
203557d128bSDave Barachsetup_message_id_table (pot_main_t * sm, api_main_t * am)
204557d128bSDave Barach{
205557d128bSDave Barach#define _(id,n,crc) \
206557d128bSDave Barach  vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
207557d128bSDave Barach  foreach_vl_msg_name_crc_pot;
208557d128bSDave Barach#undef _
209557d128bSDave Barach}
210557d128bSDave Barach
21185b528e0SShwethastatic clib_error_t * pot_init (vlib_main_t * vm)
21285b528e0SShwetha{
21385b528e0SShwetha  pot_main_t * sm = &pot_main;
21485b528e0SShwetha  clib_error_t * error = 0;
21585b528e0SShwetha  u8 * name;
21685b528e0SShwetha
21785b528e0SShwetha  bzero(sm, sizeof(pot_main));
21885b528e0SShwetha  (void)pot_util_init();
219a90ba9d3SAkshayaNadahalli
220a90ba9d3SAkshayaNadahalli  sm->vlib_main = vm;
221a90ba9d3SAkshayaNadahalli  sm->vnet_main = vnet_get_main();
222a90ba9d3SAkshayaNadahalli
2235c3bfab7SSagar Srivastav  name = format (0, "ioam_pot_%08x%c", api_version, 0);
22485b528e0SShwetha
22585b528e0SShwetha  /* Ask for a correctly-sized block of API message decode slots */
22685b528e0SShwetha  sm->msg_id_base = vl_msg_api_get_msg_ids
22785b528e0SShwetha      ((char *) name, VL_MSG_FIRST_AVAILABLE);
22885b528e0SShwetha
22985b528e0SShwetha  error = pot_plugin_api_hookup (vm);
23085b528e0SShwetha
231557d128bSDave Barach  /* Add our API messages to the global name_crc hash table */
232557d128bSDave Barach  setup_message_id_table (sm, &api_main);
233557d128bSDave Barach
23485b528e0SShwetha  vec_free(name);
23585b528e0SShwetha
23685b528e0SShwetha  return error;
23785b528e0SShwetha}
23885b528e0SShwetha
23985b528e0SShwethaVLIB_INIT_FUNCTION (pot_init);
240