FD.io VPP  v18.07-rc0-415-g6c78436
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 
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 
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
55  l2t_session_t * s,
56  l2t_main_t * lm, u32 context)
57 {
59  u8 *if_name = NULL;
61 
63 
64  if_name = format (if_name, "%U",
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);
75  mp->local_cookie[0] = s->local_cookie[0];
76  mp->local_cookie[1] = s->local_cookie[1];
79  sizeof (s->client_address));
80  clib_memcpy (mp->our_address, &s->our_address, sizeof (s->our_address));
82  mp->context = context;
83 
84  vl_api_send_msg (reg, (u8 *) mp);
85 }
86 
87 
88 static void
90  mp)
91 {
93  l2t_main_t *lm = &l2t_main;
95  l2t_session_t *session;
96 
98  if (!reg)
99  return;
100 
101  /* *INDENT-OFF* */
102  pool_foreach (session, lm->sessions,
103  ({
104  send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
105  }));
106  /* *INDENT-ON* */
107 }
108 
111 {
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,
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),
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 
164 {
165  vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
166  l2t_main_t *lm = &l2t_main;
167  int rv;
168 
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 
177 
178  REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
179 }
180 
183 {
184  int rv;
185  vnet_main_t *vnm = vnet_get_main ();
186  vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
187 
189 
191  (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
192 
194 
195  REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
196 }
197 
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.
220  * vlib has alread mapped shared memory and
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
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 *
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);
249 #undef _
250 
251  /*
252  * Set up the (msg_name, crc, message-id) table
253  */
255 
256  return 0;
257 }
258 
260 
261 /*
262  * fd.io coding-style-patch-verification: ON
263  *
264  * Local Variables:
265  * eval: (c-set-style "gnu")
266  * End:
267  */
u64 local_cookie[2]
Definition: l2tp.h:32
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
static void vl_api_l2tpv3_interface_enable_disable_t_handler(vl_api_l2tpv3_interface_enable_disable_t *mp)
Definition: l2tp_api.c:182
#define REPLY_MACRO2(t, body)
#define NULL
Definition: clib.h:55
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:34
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
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)
l2t_main_t l2t_main
Definition: l2tp.c:26
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
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:617
u32 hw_if_index
Definition: l2tp.h:38
l2tpv3 tunnel interface create response
Definition: l2tp.api:50
#define hash_get(h, key)
Definition: hash.h:249
format_function_t format_vnet_sw_interface_name
u32 local_session_id
Definition: l2tp.h:34
#define foreach_vpe_api_msg
Definition: l2tp_api.c:45
#define REPLY_MACRO(t)
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:201
uword * fib_index_by_table_id
Definition: ip6.h:186
An API client registration, only in vpp/vlib.
Definition: api_common.h:44
#define BAD_SW_IF_INDEX_LABEL
vlib_main_t * vm
Definition: buffer.c:294
#define clib_memcpy(a, b, c)
Definition: string.h:75
static void vl_api_l2tpv3_set_lookup_key_t_handler(vl_api_l2tpv3_set_lookup_key_t *mp)
Definition: l2tp_api.c:199
#define ARRAY_LEN(x)
Definition: clib.h:59
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:56
static void setup_message_id_table(api_main_t *am)
Definition: l2tp_api.c:229
u32 remote_session_id
Definition: l2tp.h:35
ip6_main_t ip6_main
Definition: ip6_forward.c:2574
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:292
vnet_main_t * vnet_main
Definition: l2tp.h:81
static void vl_api_l2tpv3_create_tunnel_t_handler(vl_api_l2tpv3_create_tunnel_t *mp)
Definition: l2tp_api.c:110
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:89
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:163
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:53
u8 l2_sublayer_present
Definition: l2tp.h:45
l2tpv3 tunnel interface create request
Definition: l2tp.api:30
vpe_api_main_t vpe_api_main
Definition: interface_api.c:49
api_main_t api_main
Definition: api_shared.c:35
#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:534
static clib_error_t * l2tp_api_hookup(vlib_main_t *vm)
Definition: l2tp_api.c:237