Pavel Kotucek | 56f32cc | 2016-12-22 10:03:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * ipsec_gre_api.c - ipsec_gre 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/ipsec-gre/ipsec_gre.h> |
| 26 | |
| 27 | #include <vnet/vnet_msg_enum.h> |
| 28 | |
| 29 | #define vl_typedefs /* define message structures */ |
| 30 | #include <vnet/vnet_all_api_h.h> |
| 31 | #undef vl_typedefs |
| 32 | |
| 33 | #define vl_endianfun /* define message structures */ |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_endianfun |
| 36 | |
| 37 | /* instantiate all the print functions we know about */ |
| 38 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 39 | #define vl_printfun |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_printfun |
| 42 | |
| 43 | #include <vlibapi/api_helper_macros.h> |
| 44 | |
| 45 | #define foreach_vpe_api_msg \ |
| 46 | _(IPSEC_GRE_ADD_DEL_TUNNEL, ipsec_gre_add_del_tunnel) \ |
| 47 | _(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump) |
| 48 | |
| 49 | static void |
| 50 | vl_api_ipsec_gre_add_del_tunnel_t_handler (vl_api_ipsec_gre_add_del_tunnel_t * |
| 51 | mp) |
| 52 | { |
| 53 | vl_api_ipsec_gre_add_del_tunnel_reply_t *rmp; |
| 54 | int rv = 0; |
| 55 | vnet_ipsec_gre_add_del_tunnel_args_t _a, *a = &_a; |
| 56 | u32 sw_if_index = ~0; |
| 57 | |
| 58 | /* Check src & dst are different */ |
| 59 | if (memcmp (mp->src_address, mp->dst_address, 4) == 0) |
| 60 | { |
| 61 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 62 | goto out; |
| 63 | } |
| 64 | |
| 65 | memset (a, 0, sizeof (*a)); |
| 66 | |
| 67 | /* ip addresses sent in network byte order */ |
| 68 | clib_memcpy (&(a->src), mp->src_address, 4); |
| 69 | clib_memcpy (&(a->dst), mp->dst_address, 4); |
| 70 | a->is_add = mp->is_add; |
| 71 | a->lsa = ntohl (mp->local_sa_id); |
| 72 | a->rsa = ntohl (mp->remote_sa_id); |
| 73 | |
| 74 | rv = vnet_ipsec_gre_add_del_tunnel (a, &sw_if_index); |
| 75 | |
| 76 | out: |
| 77 | /* *INDENT-OFF* */ |
| 78 | REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY, |
| 79 | ({ |
| 80 | rmp->sw_if_index = ntohl (sw_if_index); |
| 81 | })); |
| 82 | /* *INDENT-ON* */ |
| 83 | } |
| 84 | |
| 85 | static void send_ipsec_gre_tunnel_details |
| 86 | (ipsec_gre_tunnel_t * t, unix_shared_memory_queue_t * q, u32 context) |
| 87 | { |
| 88 | vl_api_ipsec_gre_tunnel_details_t *rmp; |
| 89 | |
| 90 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 91 | memset (rmp, 0, sizeof (*rmp)); |
| 92 | rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS); |
| 93 | clib_memcpy (rmp->src_address, &(t->tunnel_src), 4); |
| 94 | clib_memcpy (rmp->dst_address, &(t->tunnel_dst), 4); |
| 95 | rmp->sw_if_index = htonl (t->sw_if_index); |
| 96 | rmp->local_sa_id = htonl (t->local_sa_id); |
| 97 | rmp->remote_sa_id = htonl (t->remote_sa_id); |
| 98 | rmp->context = context; |
| 99 | |
| 100 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 101 | } |
| 102 | |
| 103 | static void vl_api_ipsec_gre_tunnel_dump_t_handler |
| 104 | (vl_api_ipsec_gre_tunnel_dump_t * mp) |
| 105 | { |
| 106 | unix_shared_memory_queue_t *q; |
| 107 | ipsec_gre_main_t *igm = &ipsec_gre_main; |
| 108 | ipsec_gre_tunnel_t *t; |
| 109 | u32 sw_if_index; |
| 110 | |
| 111 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 112 | if (q == 0) |
| 113 | { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | sw_if_index = ntohl (mp->sw_if_index); |
| 118 | |
| 119 | if (~0 == sw_if_index) |
| 120 | { |
| 121 | /* *INDENT-OFF* */ |
| 122 | pool_foreach (t, igm->tunnels, |
| 123 | ({ |
| 124 | send_ipsec_gre_tunnel_details(t, q, mp->context); |
| 125 | })); |
| 126 | /* *INDENT-ON* */ |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) || |
| 131 | (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index])) |
| 132 | { |
| 133 | return; |
| 134 | } |
| 135 | t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]]; |
| 136 | send_ipsec_gre_tunnel_details (t, q, mp->context); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * ipsec_gre_api_hookup |
| 142 | * Add vpe's API message handlers to the table. |
| 143 | * vlib has alread mapped shared memory and |
| 144 | * added the client registration handlers. |
| 145 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 146 | */ |
| 147 | #define vl_msg_name_crc_list |
| 148 | #include <vnet/vnet_all_api_h.h> |
| 149 | #undef vl_msg_name_crc_list |
| 150 | |
| 151 | static void |
| 152 | setup_message_id_table (api_main_t * am) |
| 153 | { |
| 154 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 155 | foreach_vl_msg_name_crc_ipsec_gre; |
| 156 | #undef _ |
| 157 | } |
| 158 | |
| 159 | static clib_error_t * |
| 160 | ipsec_gre_api_hookup (vlib_main_t * vm) |
| 161 | { |
| 162 | api_main_t *am = &api_main; |
| 163 | |
| 164 | #define _(N,n) \ |
| 165 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 166 | vl_api_##n##_t_handler, \ |
| 167 | vl_noop_handler, \ |
| 168 | vl_api_##n##_t_endian, \ |
| 169 | vl_api_##n##_t_print, \ |
| 170 | sizeof(vl_api_##n##_t), 1); |
| 171 | foreach_vpe_api_msg; |
| 172 | #undef _ |
| 173 | |
| 174 | /* |
| 175 | * Set up the (msg_name, crc, message-id) table |
| 176 | */ |
| 177 | setup_message_id_table (am); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | VLIB_API_INIT_FUNCTION (ipsec_gre_api_hookup); |
| 183 | |
| 184 | /* |
| 185 | * fd.io coding-style-patch-verification: ON |
| 186 | * |
| 187 | * Local Variables: |
| 188 | * eval: (c-set-style "gnu") |
| 189 | * End: |
| 190 | */ |