2 *------------------------------------------------------------------
3 * l2tp_api.c - l2tpv3 api
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/l2tp/l2tp.h>
26 #include <vnet/ip/ip_types_api.h>
28 #include <vnet/vnet_msg_enum.h>
30 #define vl_typedefs /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
34 #define vl_endianfun /* define message structures */
35 #include <vnet/vnet_all_api_h.h>
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #include <vnet/vnet_all_api_h.h>
44 #include <vlibapi/api_helper_macros.h>
46 #define foreach_vpe_api_msg \
47 _(L2TPV3_CREATE_TUNNEL, l2tpv3_create_tunnel) \
48 _(L2TPV3_SET_TUNNEL_COOKIES, l2tpv3_set_tunnel_cookies) \
49 _(L2TPV3_INTERFACE_ENABLE_DISABLE, l2tpv3_interface_enable_disable) \
50 _(L2TPV3_SET_LOOKUP_KEY, l2tpv3_set_lookup_key) \
51 _(SW_IF_L2TPV3_TUNNEL_DUMP, sw_if_l2tpv3_tunnel_dump)
54 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
55 vl_api_registration_t * reg,
57 l2t_main_t * lm, u32 context)
59 vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
61 vnet_sw_interface_t *si = NULL;
63 si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
65 if_name = format (if_name, "%U",
66 format_vnet_sw_interface_name, lm->vnet_main, si);
68 mp = vl_msg_api_alloc (sizeof (*mp));
69 clib_memset (mp, 0, sizeof (*mp));
70 mp->_vl_msg_id = ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS);
71 strncpy ((char *) mp->interface_name,
72 (char *) if_name, ARRAY_LEN (mp->interface_name) - 1);
73 mp->sw_if_index = ntohl (si->sw_if_index);
74 mp->local_session_id = s->local_session_id;
75 mp->remote_session_id = s->remote_session_id;
76 mp->local_cookie[0] = s->local_cookie[0];
77 mp->local_cookie[1] = s->local_cookie[1];
78 mp->remote_cookie = s->remote_cookie;
79 ip_address_encode ((ip46_address_t *) & s->client_address, IP46_TYPE_IP6,
81 ip_address_encode ((ip46_address_t *) & s->our_address, IP46_TYPE_IP6,
83 mp->l2_sublayer_present = s->l2_sublayer_present;
84 mp->context = context;
86 vl_api_send_msg (reg, (u8 *) mp);
91 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
94 vpe_api_main_t *am = &vpe_api_main;
95 l2t_main_t *lm = &l2t_main;
96 vl_api_registration_t *reg;
97 l2t_session_t *session;
99 reg = vl_api_client_index_to_registration (mp->client_index);
104 pool_foreach (session, lm->sessions,
106 send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
111 static void vl_api_l2tpv3_create_tunnel_t_handler
112 (vl_api_l2tpv3_create_tunnel_t * mp)
114 vl_api_l2tpv3_create_tunnel_reply_t *rmp;
115 l2t_main_t *lm = &l2t_main;
116 u32 sw_if_index = (u32) ~ 0;
118 ip46_address_t client, our;
120 if (mp->our_address.af == ADDRESS_IP4)
122 rv = VNET_API_ERROR_UNIMPLEMENTED;
128 if (mp->encap_vrf_id != ~0)
131 ip6_main_t *im = &ip6_main;
134 hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
136 rv = VNET_API_ERROR_NO_SUCH_FIB;
139 encap_fib_index = p[0];
143 encap_fib_index = ~0;
146 ip_address_decode (&mp->client_address, &client);
147 ip_address_decode (&mp->our_address, &our);
149 rv = create_l2tpv3_ipv6_tunnel (lm,
152 ntohl (mp->local_session_id),
153 ntohl (mp->remote_session_id),
154 clib_net_to_host_u64 (mp->local_cookie),
155 clib_net_to_host_u64 (mp->remote_cookie),
156 mp->l2_sublayer_present,
157 encap_fib_index, &sw_if_index);
161 REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
163 rmp->sw_if_index = ntohl (sw_if_index);
168 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
169 (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
171 vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
172 l2t_main_t *lm = &l2t_main;
175 VALIDATE_SW_IF_INDEX (mp);
177 rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
178 clib_net_to_host_u64 (mp->new_local_cookie),
180 (mp->new_remote_cookie));
182 BAD_SW_IF_INDEX_LABEL;
184 REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
187 static void vl_api_l2tpv3_interface_enable_disable_t_handler
188 (vl_api_l2tpv3_interface_enable_disable_t * mp)
191 vnet_main_t *vnm = vnet_get_main ();
192 vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
194 VALIDATE_SW_IF_INDEX (mp);
196 rv = l2tpv3_interface_enable_disable
197 (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
199 BAD_SW_IF_INDEX_LABEL;
201 REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
204 static void vl_api_l2tpv3_set_lookup_key_t_handler
205 (vl_api_l2tpv3_set_lookup_key_t * mp)
208 l2t_main_t *lm = &l2t_main;
209 vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
211 if (mp->key > L2T_LOOKUP_KEY_API_SESSION_ID)
213 rv = VNET_API_ERROR_INVALID_VALUE;
217 lm->lookup_type = (ip6_to_l2_lookup_t) mp->key;
220 REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
225 * Add vpe's API message handlers to the table.
226 * vlib has already mapped shared memory and
227 * added the client registration handlers.
228 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
230 #define vl_msg_name_crc_list
231 #include <vnet/vnet_all_api_h.h>
232 #undef vl_msg_name_crc_list
235 setup_message_id_table (api_main_t * am)
237 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
238 foreach_vl_msg_name_crc_l2tp;
242 static clib_error_t *
243 l2tp_api_hookup (vlib_main_t * vm)
245 api_main_t *am = vlibapi_get_main ();
248 vl_msg_api_set_handlers(VL_API_##N, #n, \
249 vl_api_##n##_t_handler, \
251 vl_api_##n##_t_endian, \
252 vl_api_##n##_t_print, \
253 sizeof(vl_api_##n##_t), 1);
258 * Set up the (msg_name, crc, message-id) table
260 setup_message_id_table (am);
265 VLIB_API_INIT_FUNCTION (l2tp_api_hookup);
268 * fd.io coding-style-patch-verification: ON
271 * eval: (c-set-style "gnu")