pot_api.c revision ab96454e
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
2985b528e0SShwetha/* define message IDs */
30ab96454eSOle Troan#include <ioam/lib-pot/pot.api_enum.h>
31ab96454eSOle Troan#include <ioam/lib-pot/pot.api_types.h>
3285b528e0SShwetha
33b614d083SEyal Bari#define REPLY_MSG_ID_BASE sm->msg_id_base
34b614d083SEyal Bari#include <vlibapi/api_helper_macros.h>
3585b528e0SShwetha
3685b528e0SShwethastatic void vl_api_pot_profile_add_t_handler
3785b528e0SShwetha(vl_api_pot_profile_add_t *mp)
3885b528e0SShwetha{
3985b528e0SShwetha    pot_main_t * sm = &pot_main;
4085b528e0SShwetha    int rv = 0;
4185b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
4285b528e0SShwetha    u8 id;
4385b528e0SShwetha    pot_profile *profile = NULL;
4485b528e0SShwetha    u8 *name = 0;
4585b528e0SShwetha
4685b528e0SShwetha    if (mp->list_name_len)
4785b528e0SShwetha        name = format(0, "%s", mp->list_name);
4885b528e0SShwetha
4985b528e0SShwetha    pot_profile_list_init(name);
5085b528e0SShwetha    id = mp->id;
5185b528e0SShwetha    profile = pot_profile_find(id);
5285b528e0SShwetha    if (profile) {
5385b528e0SShwetha	rv = pot_profile_create(profile,
5485b528e0SShwetha				clib_net_to_host_u64(mp->prime),
5585b528e0SShwetha				clib_net_to_host_u64(mp->polynomial_public),
5685b528e0SShwetha				clib_net_to_host_u64(mp->lpc),
5785b528e0SShwetha				clib_net_to_host_u64(mp->secret_share));
5885b528e0SShwetha	if (rv != 0)
5985b528e0SShwetha            goto ERROROUT;
6085b528e0SShwetha	if (1 == mp->validator)
6185b528e0SShwetha	  (void)pot_set_validator(profile, clib_net_to_host_u64(mp->secret_key));
6285b528e0SShwetha        (void)pot_profile_set_bit_mask(profile, mp->max_bits);
6385b528e0SShwetha    } else {
6485b528e0SShwetha        rv = -3;
6585b528e0SShwetha    }
6685b528e0SShwetha ERROROUT:
6785b528e0SShwetha    vec_free(name);
6885b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ADD_REPLY);
6985b528e0SShwetha}
7085b528e0SShwetha
71b4bc55b9SSagar Srivastavstatic void send_pot_profile_details(vl_api_pot_profile_show_config_dump_t *mp, u8 id)
72b4bc55b9SSagar Srivastav{
73b4bc55b9SSagar Srivastav    vl_api_pot_profile_show_config_details_t * rmp;
74b4bc55b9SSagar Srivastav    pot_main_t * sm = &pot_main;
75b4bc55b9SSagar Srivastav    pot_profile *profile = pot_profile_find(id);
76b4bc55b9SSagar Srivastav    int rv = 0;
77b4bc55b9SSagar Srivastav    if(profile){
78b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
79b4bc55b9SSagar Srivastav			rmp->id=id;
80b4bc55b9SSagar Srivastav			rmp->validator=profile->validator;
81b4bc55b9SSagar Srivastav			rmp->secret_key=clib_host_to_net_u64(profile->secret_key);
82b4bc55b9SSagar Srivastav			rmp->secret_share=clib_host_to_net_u64(profile->secret_share);
83b4bc55b9SSagar Srivastav			rmp->prime=clib_host_to_net_u64(profile->prime);
84b4bc55b9SSagar Srivastav			rmp->bit_mask=clib_host_to_net_u64(profile->bit_mask);
85b4bc55b9SSagar Srivastav			rmp->lpc=clib_host_to_net_u64(profile->lpc);
86b4bc55b9SSagar Srivastav			rmp->polynomial_public=clib_host_to_net_u64(profile->poly_pre_eval);
87b4bc55b9SSagar Srivastav			);
88b4bc55b9SSagar Srivastav    }
89b4bc55b9SSagar Srivastav    else{
90b4bc55b9SSagar Srivastav        REPLY_MACRO2(VL_API_POT_PROFILE_SHOW_CONFIG_DETAILS,
91b4bc55b9SSagar Srivastav			rmp->id=id;
92b4bc55b9SSagar Srivastav			rmp->validator=0;
93b4bc55b9SSagar Srivastav			rmp->secret_key=0;
94b4bc55b9SSagar Srivastav			rmp->secret_share=0;
95b4bc55b9SSagar Srivastav			rmp->prime=0;
96b4bc55b9SSagar Srivastav			rmp->bit_mask=0;
97b4bc55b9SSagar Srivastav			rmp->lpc=0;
98b4bc55b9SSagar Srivastav			rmp->polynomial_public=0;
99b4bc55b9SSagar Srivastav			);
100b4bc55b9SSagar Srivastav    }
101b4bc55b9SSagar Srivastav}
102b4bc55b9SSagar Srivastav
103b4bc55b9SSagar Srivastavstatic void vl_api_pot_profile_show_config_dump_t_handler
104b4bc55b9SSagar Srivastav(vl_api_pot_profile_show_config_dump_t *mp)
105b4bc55b9SSagar Srivastav{
106b4bc55b9SSagar Srivastav    u8 id = mp->id;
107b4bc55b9SSagar Srivastav    u8 dump_call_id = ~0;
108b4bc55b9SSagar Srivastav    if(dump_call_id==id){
109b4bc55b9SSagar Srivastav        for(id=0;id<MAX_POT_PROFILES;id++)
110b4bc55b9SSagar Srivastav	    send_pot_profile_details(mp,id);
111b4bc55b9SSagar Srivastav    }
112b4bc55b9SSagar Srivastav    else
113b4bc55b9SSagar Srivastav        send_pot_profile_details(mp,id);
114b4bc55b9SSagar Srivastav}
115b4bc55b9SSagar Srivastav
11685b528e0SShwethastatic void vl_api_pot_profile_activate_t_handler
11785b528e0SShwetha(vl_api_pot_profile_activate_t *mp)
11885b528e0SShwetha{
11985b528e0SShwetha    pot_main_t * sm = &pot_main;
12085b528e0SShwetha    int rv = 0;
12185b528e0SShwetha    vl_api_pot_profile_add_reply_t * rmp;
12285b528e0SShwetha    u8 id;
12385b528e0SShwetha    u8 *name = NULL;
12485b528e0SShwetha
12585b528e0SShwetha    if (mp->list_name_len)
12685b528e0SShwetha        name = format(0, "%s", mp->list_name);
12785b528e0SShwetha    if (!pot_profile_list_is_enabled(name)) {
12885b528e0SShwetha        rv = -1;
12985b528e0SShwetha    } else {
13085b528e0SShwetha        id = mp->id;
13185b528e0SShwetha	rv = pot_profile_set_active(id);
13285b528e0SShwetha    }
13385b528e0SShwetha
13485b528e0SShwetha    vec_free(name);
13585b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_ACTIVATE_REPLY);
13685b528e0SShwetha}
13785b528e0SShwetha
13885b528e0SShwetha
13985b528e0SShwethastatic void vl_api_pot_profile_del_t_handler
14085b528e0SShwetha(vl_api_pot_profile_del_t *mp)
14185b528e0SShwetha{
14285b528e0SShwetha    pot_main_t * sm = &pot_main;
14385b528e0SShwetha    int rv = 0;
14485b528e0SShwetha    vl_api_pot_profile_del_reply_t * rmp;
14585b528e0SShwetha
14685b528e0SShwetha    clear_pot_profiles();
14785b528e0SShwetha
14885b528e0SShwetha    REPLY_MACRO(VL_API_POT_PROFILE_DEL_REPLY);
14985b528e0SShwetha}
15085b528e0SShwetha
151ab96454eSOle Troan#include <ioam/lib-pot/pot.api.c>
15285b528e0SShwethastatic clib_error_t * pot_init (vlib_main_t * vm)
15385b528e0SShwetha{
15485b528e0SShwetha  pot_main_t * sm = &pot_main;
15585b528e0SShwetha
15685b528e0SShwetha  bzero(sm, sizeof(pot_main));
15785b528e0SShwetha  (void)pot_util_init();
158a90ba9d3SAkshayaNadahalli
159a90ba9d3SAkshayaNadahalli  sm->vlib_main = vm;
160a90ba9d3SAkshayaNadahalli  sm->vnet_main = vnet_get_main();
161a90ba9d3SAkshayaNadahalli
16285b528e0SShwetha  /* Ask for a correctly-sized block of API message decode slots */
163ab96454eSOle Troan  sm->msg_id_base = setup_message_id_table ();
16485b528e0SShwetha
165ab96454eSOle Troan  return 0;
16685b528e0SShwetha}
16785b528e0SShwetha
16885b528e0SShwethaVLIB_INIT_FUNCTION (pot_init);
169