Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * l2tp_api.c - l2tpv3 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/l2tp/l2tp.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 | _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel) \ |
| 47 | _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies) \ |
| 48 | _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable) \ |
| 49 | _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key) \ |
| 50 | _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump) |
| 51 | |
| 52 | static void |
| 53 | send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am, |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 54 | vl_api_registration_t * reg, |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 55 | l2t_session_t * s, |
| 56 | l2t_main_t * lm, u32 context) |
| 57 | { |
| 58 | vl_api_sw_if_l2tpv3_tunnel_details_t *mp; |
| 59 | u8 *if_name = NULL; |
| 60 | vnet_sw_interface_t *si = NULL; |
| 61 | |
| 62 | si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index); |
| 63 | |
| 64 | if_name = format (if_name, "%U", |
| 65 | format_vnet_sw_interface_name, lm->vnet_main, si); |
| 66 | |
| 67 | mp = vl_msg_api_alloc (sizeof (*mp)); |
| 68 | memset (mp, 0, sizeof (*mp)); |
| 69 | mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS); |
| 70 | strncpy ((char *) mp->interface_name, |
| 71 | (char *) if_name, ARRAY_LEN (mp->interface_name) - 1); |
| 72 | mp->sw_if_index = ntohl (si->sw_if_index); |
| 73 | mp->local_session_id = s->local_session_id; |
| 74 | mp->remote_session_id = s->remote_session_id; |
| 75 | mp->local_cookie[0] = s->local_cookie[0]; |
| 76 | mp->local_cookie[1] = s->local_cookie[1]; |
| 77 | mp->remote_cookie = s->remote_cookie; |
| 78 | clib_memcpy (mp->client_address, &s->client_address, |
| 79 | sizeof (s->client_address)); |
| 80 | clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address)); |
| 81 | mp->l2_sublayer_present = s->l2_sublayer_present; |
| 82 | mp->context = context; |
| 83 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 84 | vl_api_send_msg (reg, (u8 *) mp); |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | static void |
| 89 | vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t * |
| 90 | mp) |
| 91 | { |
| 92 | vpe_api_main_t *am = &vpe_api_main; |
| 93 | l2t_main_t *lm = &l2t_main; |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 94 | vl_api_registration_t *reg; |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 95 | l2t_session_t *session; |
| 96 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 97 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 98 | if (!reg) |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 99 | return; |
| 100 | |
| 101 | /* *INDENT-OFF* */ |
| 102 | pool_foreach (session, lm->sessions, |
| 103 | ({ |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 104 | send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context); |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 105 | })); |
| 106 | /* *INDENT-ON* */ |
| 107 | } |
| 108 | |
| 109 | static void vl_api_l2tpv3_create_tunnel_t_handler |
| 110 | (vl_api_l2tpv3_create_tunnel_t * mp) |
| 111 | { |
| 112 | vl_api_l2tpv3_create_tunnel_reply_t *rmp; |
| 113 | l2t_main_t *lm = &l2t_main; |
| 114 | u32 sw_if_index = (u32) ~ 0; |
| 115 | int rv; |
| 116 | |
| 117 | if (mp->is_ipv6 != 1) |
| 118 | { |
| 119 | rv = VNET_API_ERROR_UNIMPLEMENTED; |
| 120 | goto out; |
| 121 | } |
| 122 | |
| 123 | u32 encap_fib_index; |
| 124 | |
| 125 | if (mp->encap_vrf_id != ~0) |
| 126 | { |
| 127 | uword *p; |
| 128 | ip6_main_t *im = &ip6_main; |
| 129 | if (! |
| 130 | (p = |
| 131 | hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id)))) |
| 132 | { |
| 133 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 134 | goto out; |
| 135 | } |
| 136 | encap_fib_index = p[0]; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | encap_fib_index = ~0; |
| 141 | } |
| 142 | |
| 143 | rv = create_l2tpv3_ipv6_tunnel (lm, |
| 144 | (ip6_address_t *) mp->client_address, |
| 145 | (ip6_address_t *) mp->our_address, |
| 146 | ntohl (mp->local_session_id), |
| 147 | ntohl (mp->remote_session_id), |
| 148 | clib_net_to_host_u64 (mp->local_cookie), |
| 149 | clib_net_to_host_u64 (mp->remote_cookie), |
| 150 | mp->l2_sublayer_present, |
| 151 | encap_fib_index, &sw_if_index); |
| 152 | |
| 153 | out: |
| 154 | /* *INDENT-OFF* */ |
| 155 | REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY, |
| 156 | ({ |
| 157 | rmp->sw_if_index = ntohl (sw_if_index); |
| 158 | })); |
| 159 | /* *INDENT-ON* */ |
| 160 | } |
| 161 | |
| 162 | static void vl_api_l2tpv3_set_tunnel_cookies_t_handler |
| 163 | (vl_api_l2tpv3_set_tunnel_cookies_t * mp) |
| 164 | { |
| 165 | vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp; |
| 166 | l2t_main_t *lm = &l2t_main; |
| 167 | int rv; |
| 168 | |
| 169 | VALIDATE_SW_IF_INDEX (mp); |
| 170 | |
| 171 | rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index), |
| 172 | clib_net_to_host_u64 (mp->new_local_cookie), |
| 173 | clib_net_to_host_u64 |
| 174 | (mp->new_remote_cookie)); |
| 175 | |
| 176 | BAD_SW_IF_INDEX_LABEL; |
| 177 | |
| 178 | REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY); |
| 179 | } |
| 180 | |
| 181 | static void vl_api_l2tpv3_interface_enable_disable_t_handler |
| 182 | (vl_api_l2tpv3_interface_enable_disable_t * mp) |
| 183 | { |
| 184 | int rv; |
| 185 | vnet_main_t *vnm = vnet_get_main (); |
| 186 | vl_api_l2tpv3_interface_enable_disable_reply_t *rmp; |
| 187 | |
| 188 | VALIDATE_SW_IF_INDEX (mp); |
| 189 | |
| 190 | rv = l2tpv3_interface_enable_disable |
| 191 | (vnm, ntohl (mp->sw_if_index), mp->enable_disable); |
| 192 | |
| 193 | BAD_SW_IF_INDEX_LABEL; |
| 194 | |
| 195 | REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY); |
| 196 | } |
| 197 | |
| 198 | static void vl_api_l2tpv3_set_lookup_key_t_handler |
| 199 | (vl_api_l2tpv3_set_lookup_key_t * mp) |
| 200 | { |
| 201 | int rv = 0; |
| 202 | l2t_main_t *lm = &l2t_main; |
| 203 | vl_api_l2tpv3_set_lookup_key_reply_t *rmp; |
| 204 | |
| 205 | if (mp->key > L2T_LOOKUP_SESSION_ID) |
| 206 | { |
| 207 | rv = VNET_API_ERROR_INVALID_VALUE; |
| 208 | goto out; |
| 209 | } |
| 210 | |
| 211 | lm->lookup_type = mp->key; |
| 212 | |
| 213 | out: |
| 214 | REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * l2tp_api_hookup |
| 219 | * Add vpe's API message handlers to the table. |
Paul Vinciguerra | bdc0e6b | 2018-09-22 05:32:50 -0700 | [diff] [blame^] | 220 | * vlib has already mapped shared memory and |
Pavel Kotucek | c4d1cf7 | 2016-12-20 10:01:20 +0100 | [diff] [blame] | 221 | * added the client registration handlers. |
| 222 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 223 | */ |
| 224 | #define vl_msg_name_crc_list |
| 225 | #include <vnet/vnet_all_api_h.h> |
| 226 | #undef vl_msg_name_crc_list |
| 227 | |
| 228 | static void |
| 229 | setup_message_id_table (api_main_t * am) |
| 230 | { |
| 231 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 232 | foreach_vl_msg_name_crc_l2tp; |
| 233 | #undef _ |
| 234 | } |
| 235 | |
| 236 | static clib_error_t * |
| 237 | l2tp_api_hookup (vlib_main_t * vm) |
| 238 | { |
| 239 | api_main_t *am = &api_main; |
| 240 | |
| 241 | #define _(N,n) \ |
| 242 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 243 | vl_api_##n##_t_handler, \ |
| 244 | vl_noop_handler, \ |
| 245 | vl_api_##n##_t_endian, \ |
| 246 | vl_api_##n##_t_print, \ |
| 247 | sizeof(vl_api_##n##_t), 1); |
| 248 | foreach_vpe_api_msg; |
| 249 | #undef _ |
| 250 | |
| 251 | /* |
| 252 | * Set up the (msg_name, crc, message-id) table |
| 253 | */ |
| 254 | setup_message_id_table (am); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | VLIB_API_INIT_FUNCTION (l2tp_api_hookup); |
| 260 | |
| 261 | /* |
| 262 | * fd.io coding-style-patch-verification: ON |
| 263 | * |
| 264 | * Local Variables: |
| 265 | * eval: (c-set-style "gnu") |
| 266 | * End: |
| 267 | */ |