FD.io VPP  v20.05.1-6-gf53edbc3b
Vector Packet Processing
l2tp_api.c
Go to the documentation of this file.
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 #include <vnet/ip/ip_types_api.h>
27 
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 
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 
53 static void
56  l2t_session_t * s,
57  l2t_main_t * lm, u32 context)
58 {
60  u8 *if_name = NULL;
61  vnet_sw_interface_t *si = NULL;
62 
64 
65  if_name = format (if_name, "%U",
67 
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);
76  mp->local_cookie[0] = s->local_cookie[0];
77  mp->local_cookie[1] = s->local_cookie[1];
79  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);
84  mp->context = context;
85 
86  vl_api_send_msg (reg, (u8 *) mp);
87 }
88 
89 
90 static void
92  mp)
93 {
95  l2t_main_t *lm = &l2t_main;
97  l2t_session_t *session;
98 
100  if (!reg)
101  return;
102 
103  /* *INDENT-OFF* */
104  pool_foreach (session, lm->sessions,
105  ({
106  send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
107  }));
108  /* *INDENT-ON* */
109 }
110 
113 {
115  l2t_main_t *lm = &l2t_main;
116  u32 sw_if_index = (u32) ~ 0;
117  int rv;
118  ip46_address_t client, our;
119 
120  if (mp->our_address.af == ADDRESS_IP4)
121  {
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 
146  ip_address_decode (&mp->client_address, &client);
147  ip_address_decode (&mp->our_address, &our);
148 
149  rv = create_l2tpv3_ipv6_tunnel (lm,
150  &client.ip6,
151  &our.ip6,
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),
157  encap_fib_index, &sw_if_index);
158 
159 out:
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 
170 {
171  vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
172  l2t_main_t *lm = &l2t_main;
173  int rv;
174 
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 
183 
184  REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
185 }
186 
189 {
190  int rv;
191  vnet_main_t *vnm = vnet_get_main ();
192  vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
193 
195 
197  (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
198 
200 
201  REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
202 }
203 
206 {
207  int rv = 0;
208  l2t_main_t *lm = &l2t_main;
209  vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
210 
212  {
213  rv = VNET_API_ERROR_INVALID_VALUE;
214  goto out;
215  }
216 
217  lm->lookup_type = (ip6_to_l2_lookup_t) mp->key;
218 
219 out:
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.
226  * vlib has already mapped shared memory and
227  * 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 
234 static void
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 
242 static clib_error_t *
244 {
245  api_main_t *am = vlibapi_get_main ();
246 
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);
255 #undef _
256 
257  /*
258  * Set up the (msg_name, crc, message-id) table
259  */
261 
262  return 0;
263 }
264 
266 
267 /*
268  * fd.io coding-style-patch-verification: ON
269  *
270  * Local Variables:
271  * eval: (c-set-style "gnu")
272  * End:
273  */
u64 local_cookie[2]
Definition: l2tp.h:32
ip6_to_l2_lookup_t
Definition: l2tp.h:51
vl_api_address_t client_address
Definition: l2tp.api:74
vl_api_l2t_lookup_key_t key
Definition: l2tp.api:108
vl_api_address_t client_address
Definition: l2tp.api:38
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static void vl_api_l2tpv3_interface_enable_disable_t_handler(vl_api_l2tpv3_interface_enable_disable_t *mp)
Definition: l2tp_api.c:188
#define REPLY_MACRO2(t, body)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
ip6_address_t our_address
Definition: l2tp.h:28
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
VLIB_API_INIT_FUNCTION(l2tp_api_hookup)
void * vl_msg_api_alloc(int nbytes)
u64 remote_cookie
Definition: l2tp.h:33
unsigned char u8
Definition: types.h:56
ip6_address_t client_address
Definition: l2tp.h:29
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
vl_api_address_t our_address
Definition: l2tp.api:75
l2t_main_t l2t_main
Definition: l2tp.c:26
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:513
vl_api_interface_index_t sw_if_index
Definition: gre.api:53
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:72
unsigned int u32
Definition: types.h:88
int l2tpv3_interface_enable_disable(vnet_main_t *vnm, u32 sw_if_index, int enable_disable)
Definition: l2tp.c:614
ip46_type_t ip_address_decode(const vl_api_address_t *in, ip46_address_t *out)
Decode/Encode for struct/union types.
Definition: ip_types_api.c:160
u32 hw_if_index
Definition: l2tp.h:38
l2tpv3 tunnel interface create response
Definition: l2tp.api:53
#define hash_get(h, key)
Definition: hash.h:249
format_function_t format_vnet_sw_interface_name
vl_api_address_t our_address
Definition: l2tp.api:39
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:94
u32 local_session_id
Definition: l2tp.h:34
#define foreach_vpe_api_msg
Definition: l2tp_api.c:46
#define REPLY_MACRO(t)
ip6_main_t ip6_main
Definition: ip6_forward.c:2784
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:57
vlib_main_t * vm
Definition: in2out_ed.c:1599
ip6_to_l2_lookup_t lookup_type
Definition: l2tp.h:68
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:226
uword * fib_index_by_table_id
Definition: ip6.h:206
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
#define BAD_SW_IF_INDEX_LABEL
static void vl_api_l2tpv3_set_lookup_key_t_handler(vl_api_l2tpv3_set_lookup_key_t *mp)
Definition: l2tp_api.c:205
#define ARRAY_LEN(x)
Definition: clib.h:66
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:57
static void setup_message_id_table(api_main_t *am)
Definition: l2tp_api.c:235
u32 remote_session_id
Definition: l2tp.h:35
int create_l2tpv3_ipv6_tunnel(l2t_main_t *lm, ip6_address_t *client_address, ip6_address_t *our_address, u32 local_session_id, u32 remote_session_id, u64 local_cookie, u64 remote_cookie, int l2_sublayer_present, u32 encap_fib_index, u32 *sw_if_index)
Definition: l2tp.c:283
vnet_main_t * vnet_main
Definition: l2tp.h:81
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:64
static void vl_api_l2tpv3_create_tunnel_t_handler(vl_api_l2tpv3_create_tunnel_t *mp)
Definition: l2tp_api.c:112
u64 uword
Definition: types.h:112
static void vl_api_sw_if_l2tpv3_tunnel_dump_t_handler(vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
Definition: l2tp_api.c:91
l2t_session_t * sessions
Definition: l2tp.h:61
static void vl_api_l2tpv3_set_tunnel_cookies_t_handler(vl_api_l2tpv3_set_tunnel_cookies_t *mp)
Definition: l2tp_api.c:169
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:194
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:379
static void send_sw_if_l2tpv3_tunnel_details(vpe_api_main_t *am, vl_api_registration_t *reg, l2t_session_t *s, l2t_main_t *lm, u32 context)
Definition: l2tp_api.c:54
u8 si
Definition: lisp_types.api:47
u8 l2_sublayer_present
Definition: l2tp.h:45
l2tpv3 tunnel interface create request
Definition: l2tp.api:34
vpe_api_main_t vpe_api_main
Definition: interface_api.c:54
#define VALIDATE_SW_IF_INDEX(mp)
int l2tpv3_set_tunnel_cookies(l2t_main_t *lm, u32 sw_if_index, u64 new_local_cookie, u64 new_remote_cookie)
Definition: l2tp.c:531
static clib_error_t * l2tp_api_hookup(vlib_main_t *vm)
Definition: l2tp_api.c:243