hs-test: more debug output in http3 test
[vpp.git] / src / plugins / l2tp / l2tp_api.c
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 <l2tp/l2tp.h>
26 #include <vnet/ip/ip_types_api.h>
27
28 /* define message IDs */
29 #include <vnet/format_fns.h>
30 #include <l2tp/l2tp.api_enum.h>
31 #include <l2tp/l2tp.api_types.h>
32
33 /**
34  * Base message ID fot the plugin
35  */
36 static u32 l2tp_base_msg_id;
37 #define REPLY_MSG_ID_BASE l2tp_base_msg_id
38
39 #include <vlibapi/api_helper_macros.h>
40
41 static void
42 send_sw_if_l2tpv3_tunnel_details (vpe_api_main_t * am,
43                                   vl_api_registration_t * reg,
44                                   l2t_session_t * s,
45                                   l2t_main_t * lm, u32 context)
46 {
47   vl_api_sw_if_l2tpv3_tunnel_details_t *mp;
48   u8 *if_name = NULL;
49   vnet_sw_interface_t *si = NULL;
50
51   si = vnet_get_hw_sw_interface (lm->vnet_main, s->hw_if_index);
52
53   if_name = format (if_name, "%U",
54                     format_vnet_sw_interface_name, lm->vnet_main, si);
55
56   mp = vl_msg_api_alloc (sizeof (*mp));
57   clib_memset (mp, 0, sizeof (*mp));
58   mp->_vl_msg_id =
59     ntohs (VL_API_SW_IF_L2TPV3_TUNNEL_DETAILS + REPLY_MSG_ID_BASE);
60   strncpy ((char *) mp->interface_name, (char *) if_name,
61            ARRAY_LEN (mp->interface_name) - 1);
62   mp->sw_if_index = ntohl (si->sw_if_index);
63   mp->local_session_id = s->local_session_id;
64   mp->remote_session_id = s->remote_session_id;
65   mp->local_cookie[0] = s->local_cookie[0];
66   mp->local_cookie[1] = s->local_cookie[1];
67   mp->remote_cookie = s->remote_cookie;
68   ip_address_encode ((ip46_address_t *) & s->client_address, IP46_TYPE_IP6,
69                      &mp->client_address);
70   ip_address_encode ((ip46_address_t *) & s->our_address, IP46_TYPE_IP6,
71                      &mp->our_address);
72   mp->l2_sublayer_present = s->l2_sublayer_present;
73   mp->context = context;
74
75   vl_api_send_msg (reg, (u8 *) mp);
76 }
77
78
79 static void
80 vl_api_sw_if_l2tpv3_tunnel_dump_t_handler (vl_api_sw_if_l2tpv3_tunnel_dump_t *
81                                            mp)
82 {
83   vpe_api_main_t *am = &vpe_api_main;
84   l2t_main_t *lm = &l2t_main;
85   vl_api_registration_t *reg;
86   l2t_session_t *session;
87
88   reg = vl_api_client_index_to_registration (mp->client_index);
89   if (!reg)
90     return;
91
92   pool_foreach (session, lm->sessions)
93    {
94     send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
95   }
96 }
97
98 static void vl_api_l2tpv3_create_tunnel_t_handler
99   (vl_api_l2tpv3_create_tunnel_t * mp)
100 {
101   vl_api_l2tpv3_create_tunnel_reply_t *rmp;
102   l2t_main_t *lm = &l2t_main;
103   u32 sw_if_index = (u32) ~ 0;
104   int rv;
105   ip46_address_t client, our;
106
107   if (mp->our_address.af == ADDRESS_IP4)
108     {
109       rv = VNET_API_ERROR_UNIMPLEMENTED;
110       goto out;
111     }
112
113   u32 encap_fib_index;
114
115   if (mp->encap_vrf_id != ~0)
116     {
117       uword *p;
118       ip6_main_t *im = &ip6_main;
119       if (!
120           (p =
121            hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
122         {
123           rv = VNET_API_ERROR_NO_SUCH_FIB;
124           goto out;
125         }
126       encap_fib_index = p[0];
127     }
128   else
129     {
130       encap_fib_index = ~0;
131     }
132
133   ip_address_decode (&mp->client_address, &client);
134   ip_address_decode (&mp->our_address, &our);
135
136   rv = create_l2tpv3_ipv6_tunnel (lm,
137                                   &client.ip6,
138                                   &our.ip6,
139                                   ntohl (mp->local_session_id),
140                                   ntohl (mp->remote_session_id),
141                                   clib_net_to_host_u64 (mp->local_cookie),
142                                   clib_net_to_host_u64 (mp->remote_cookie),
143                                   mp->l2_sublayer_present,
144                                   encap_fib_index, &sw_if_index);
145
146 out:
147   REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
148   ({
149     rmp->sw_if_index = ntohl (sw_if_index);
150   }));
151 }
152
153 static void vl_api_l2tpv3_set_tunnel_cookies_t_handler
154   (vl_api_l2tpv3_set_tunnel_cookies_t * mp)
155 {
156   vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
157   l2t_main_t *lm = &l2t_main;
158   int rv;
159
160   VALIDATE_SW_IF_INDEX (mp);
161
162   rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
163                                   clib_net_to_host_u64 (mp->new_local_cookie),
164                                   clib_net_to_host_u64
165                                   (mp->new_remote_cookie));
166
167   BAD_SW_IF_INDEX_LABEL;
168
169   REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
170 }
171
172 static void vl_api_l2tpv3_interface_enable_disable_t_handler
173   (vl_api_l2tpv3_interface_enable_disable_t * mp)
174 {
175   int rv;
176   vnet_main_t *vnm = vnet_get_main ();
177   vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
178
179   VALIDATE_SW_IF_INDEX (mp);
180
181   rv = l2tpv3_interface_enable_disable
182     (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
183
184   BAD_SW_IF_INDEX_LABEL;
185
186   REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
187 }
188
189 static void vl_api_l2tpv3_set_lookup_key_t_handler
190   (vl_api_l2tpv3_set_lookup_key_t * mp)
191 {
192   int rv = 0;
193   l2t_main_t *lm = &l2t_main;
194   vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
195
196   if (mp->key > L2T_LOOKUP_KEY_API_SESSION_ID)
197     {
198       rv = VNET_API_ERROR_INVALID_VALUE;
199       goto out;
200     }
201
202   lm->lookup_type = (ip6_to_l2_lookup_t) mp->key;
203
204 out:
205   REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
206 }
207
208 /*
209  * l2tp_api_hookup
210  * Add vpe's API message handlers to the table.
211  * vlib has already mapped shared memory and
212  * added the client registration handlers.
213  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
214  */
215 #include <l2tp/l2tp.api.c>
216
217 static clib_error_t *
218 l2tp_api_hookup (vlib_main_t * vm)
219 {
220   /*
221    * Set up the (msg_name, crc, message-id) table
222    */
223   l2tp_base_msg_id = setup_message_id_table ();
224
225   return 0;
226 }
227
228 VLIB_API_INIT_FUNCTION (l2tp_api_hookup);
229
230 #include <vlib/unix/plugin.h>
231 #include <vpp/app/version.h>
232
233 VLIB_PLUGIN_REGISTER () = {
234     .version = VPP_BUILD_VER,
235     .description = "Layer 2 Tunneling Protocol v3 (L2TP)",
236 };
237
238
239 /*
240  * fd.io coding-style-patch-verification: ON
241  *
242  * Local Variables:
243  * eval: (c-set-style "gnu")
244  * End:
245  */