blob: 3a31372253f2c04f3c72dccddafdeb6565aaf81c [file] [log] [blame]
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +01001/*
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>
Jakub Grajciar3ae52622019-10-28 10:22:01 +010026#include <vnet/ip/ip_types_api.h>
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010027
28#include <vnet/vnet_msg_enum.h>
29
30#define vl_typedefs /* define message structures */
31#include <vnet/vnet_all_api_h.h>
32#undef vl_typedefs
33
34#define vl_endianfun /* define message structures */
35#include <vnet/vnet_all_api_h.h>
36#undef vl_endianfun
37
38/* instantiate all the print functions we know about */
39#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
40#define vl_printfun
41#include <vnet/vnet_all_api_h.h>
42#undef vl_printfun
43
44#include <vlibapi/api_helper_macros.h>
45
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)
52
53static void
54send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
Florin Coras6c4dae22018-01-09 06:39:23 -080055 vl_api_registration_t * reg,
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010056 l2t_session_t * s,
57 l2t_main_t * lm, u32 context)
58{
59 vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
60 u8 *if_name = NULL;
61 vnet_sw_interface_t *si = NULL;
62
63 si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
64
65 if_name = format (if_name, "%U",
66 format_vnet_sw_interface_name, lm->vnet_main, si);
67
68 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040069 clib_memset (mp, 0, sizeof (*mp));
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010070 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;
Jakub Grajciar3ae52622019-10-28 10:22:01 +010079 ip_address_encode ((ip46_address_t *) & s->client_address, IP46_TYPE_IP6,
80 &mp->client_address);
81 ip_address_encode ((ip46_address_t *) & s->our_address, IP46_TYPE_IP6,
82 &mp->our_address);
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010083 mp->l2_sublayer_present = s->l2_sublayer_present;
84 mp->context = context;
85
Florin Coras6c4dae22018-01-09 06:39:23 -080086 vl_api_send_msg (reg, (u8 *) mp);
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010087}
88
89
90static void
91vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
92 mp)
93{
94 vpe_api_main_t *am = &vpe_api_main;
95 l2t_main_t *lm = &l2t_main;
Florin Coras6c4dae22018-01-09 06:39:23 -080096 vl_api_registration_t *reg;
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +010097 l2t_session_t *session;
98
Florin Coras6c4dae22018-01-09 06:39:23 -080099 reg = vl_api_client_index_to_registration (mp->client_index);
100 if (!reg)
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100101 return;
102
103 /* *INDENT-OFF* */
104 pool_foreach (session, lm->sessions,
105 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800106 send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100107 }));
108 /* *INDENT-ON* */
109}
110
111static void vl_api_l2tpv3_create_tunnel_t_handler
112 (vl_api_l2tpv3_create_tunnel_t * mp)
113{
114 vl_api_l2tpv3_create_tunnel_reply_t *rmp;
115 l2t_main_t *lm = &l2t_main;
116 u32 sw_if_index = (u32) ~ 0;
117 int rv;
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100118 ip46_address_t client, our;
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100119
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100120 if (mp->our_address.af == ADDRESS_IP4)
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100121 {
122 rv = VNET_API_ERROR_UNIMPLEMENTED;
123 goto out;
124 }
125
126 u32 encap_fib_index;
127
128 if (mp->encap_vrf_id != ~0)
129 {
130 uword *p;
131 ip6_main_t *im = &ip6_main;
132 if (!
133 (p =
134 hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
135 {
136 rv = VNET_API_ERROR_NO_SUCH_FIB;
137 goto out;
138 }
139 encap_fib_index = p[0];
140 }
141 else
142 {
143 encap_fib_index = ~0;
144 }
145
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100146 ip_address_decode (&mp->client_address, &client);
147 ip_address_decode (&mp->our_address, &our);
148
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100149 rv = create_l2tpv3_ipv6_tunnel (lm,
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100150 &client.ip6,
151 &our.ip6,
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100152 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);
158
159out:
160 /* *INDENT-OFF* */
161 REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
162 ({
163 rmp->sw_if_index = ntohl (sw_if_index);
164 }));
165 /* *INDENT-ON* */
166}
167
168static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
169 (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
170{
171 vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
172 l2t_main_t *lm = &l2t_main;
173 int rv;
174
175 VALIDATE_SW_IF_INDEX (mp);
176
177 rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
178 clib_net_to_host_u64 (mp->new_local_cookie),
179 clib_net_to_host_u64
180 (mp->new_remote_cookie));
181
182 BAD_SW_IF_INDEX_LABEL;
183
184 REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
185}
186
187static void vl_api_l2tpv3_interface_enable_disable_t_handler
188 (vl_api_l2tpv3_interface_enable_disable_t * mp)
189{
190 int rv;
191 vnet_main_t *vnm = vnet_get_main ();
192 vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
193
194 VALIDATE_SW_IF_INDEX (mp);
195
196 rv = l2tpv3_interface_enable_disable
197 (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
198
199 BAD_SW_IF_INDEX_LABEL;
200
201 REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
202}
203
204static void vl_api_l2tpv3_set_lookup_key_t_handler
205 (vl_api_l2tpv3_set_lookup_key_t * mp)
206{
207 int rv = 0;
208 l2t_main_t *lm = &l2t_main;
209 vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
210
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100211 if (mp->key > L2T_LOOKUP_KEY_API_SESSION_ID)
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100212 {
213 rv = VNET_API_ERROR_INVALID_VALUE;
214 goto out;
215 }
216
Jakub Grajciar3ae52622019-10-28 10:22:01 +0100217 lm->lookup_type = (ip6_to_l2_lookup_t) mp->key;
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100218
219out:
220 REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
221}
222
223/*
224 * l2tp_api_hookup
225 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700226 * vlib has already mapped shared memory and
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100227 * added the client registration handlers.
228 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
229 */
230#define vl_msg_name_crc_list
231#include <vnet/vnet_all_api_h.h>
232#undef vl_msg_name_crc_list
233
234static void
235setup_message_id_table (api_main_t * am)
236{
237#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
238 foreach_vl_msg_name_crc_l2tp;
239#undef _
240}
241
242static clib_error_t *
243l2tp_api_hookup (vlib_main_t * vm)
244{
Dave Barach39d69112019-11-27 11:42:13 -0500245 api_main_t *am = vlibapi_get_main ();
Pavel Kotucekc4d1cf72016-12-20 10:01:20 +0100246
247#define _(N,n) \
248 vl_msg_api_set_handlers(VL_API_##N, #n, \
249 vl_api_##n##_t_handler, \
250 vl_noop_handler, \
251 vl_api_##n##_t_endian, \
252 vl_api_##n##_t_print, \
253 sizeof(vl_api_##n##_t), 1);
254 foreach_vpe_api_msg;
255#undef _
256
257 /*
258 * Set up the (msg_name, crc, message-id) table
259 */
260 setup_message_id_table (am);
261
262 return 0;
263}
264
265VLIB_API_INIT_FUNCTION (l2tp_api_hookup);
266
267/*
268 * fd.io coding-style-patch-verification: ON
269 *
270 * Local Variables:
271 * eval: (c-set-style "gnu")
272 * End:
273 */