FD.io VPP  v21.10.1-2-g0a485f517
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 <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  */
37 #define REPLY_MSG_ID_BASE l2tp_base_msg_id
38 
40 
41 static void
44  l2t_session_t * s,
45  l2t_main_t * lm, u32 context)
46 {
48  u8 *if_name = NULL;
49  vnet_sw_interface_t *si = NULL;
50 
52 
53  if_name = format (if_name, "%U",
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);
65  mp->local_cookie[0] = s->local_cookie[0];
66  mp->local_cookie[1] = s->local_cookie[1];
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);
73  mp->context = context;
74 
75  vl_api_send_msg (reg, (u8 *) mp);
76 }
77 
78 
79 static void
81  mp)
82 {
84  l2t_main_t *lm = &l2t_main;
86  l2t_session_t *session;
87 
89  if (!reg)
90  return;
91 
92  /* *INDENT-OFF* */
93  pool_foreach (session, lm->sessions)
94  {
95  send_sw_if_l2tpv3_tunnel_details (am, reg, session, lm, mp->context);
96  }
97  /* *INDENT-ON* */
98 }
99 
102 {
104  l2t_main_t *lm = &l2t_main;
105  u32 sw_if_index = (u32) ~ 0;
106  int rv;
107  ip46_address_t client, our;
108 
109  if (mp->our_address.af == ADDRESS_IP4)
110  {
111  rv = VNET_API_ERROR_UNIMPLEMENTED;
112  goto out;
113  }
114 
115  u32 encap_fib_index;
116 
117  if (mp->encap_vrf_id != ~0)
118  {
119  uword *p;
120  ip6_main_t *im = &ip6_main;
121  if (!
122  (p =
123  hash_get (im->fib_index_by_table_id, ntohl (mp->encap_vrf_id))))
124  {
125  rv = VNET_API_ERROR_NO_SUCH_FIB;
126  goto out;
127  }
128  encap_fib_index = p[0];
129  }
130  else
131  {
132  encap_fib_index = ~0;
133  }
134 
135  ip_address_decode (&mp->client_address, &client);
136  ip_address_decode (&mp->our_address, &our);
137 
139  &client.ip6,
140  &our.ip6,
141  ntohl (mp->local_session_id),
142  ntohl (mp->remote_session_id),
143  clib_net_to_host_u64 (mp->local_cookie),
144  clib_net_to_host_u64 (mp->remote_cookie),
146  encap_fib_index, &sw_if_index);
147 
148 out:
149  /* *INDENT-OFF* */
150  REPLY_MACRO2(VL_API_L2TPV3_CREATE_TUNNEL_REPLY,
151  ({
152  rmp->sw_if_index = ntohl (sw_if_index);
153  }));
154  /* *INDENT-ON* */
155 }
156 
159 {
160  vl_api_l2tpv3_set_tunnel_cookies_reply_t *rmp;
161  l2t_main_t *lm = &l2t_main;
162  int rv;
163 
165 
166  rv = l2tpv3_set_tunnel_cookies (lm, ntohl (mp->sw_if_index),
167  clib_net_to_host_u64 (mp->new_local_cookie),
168  clib_net_to_host_u64
169  (mp->new_remote_cookie));
170 
172 
173  REPLY_MACRO (VL_API_L2TPV3_SET_TUNNEL_COOKIES_REPLY);
174 }
175 
178 {
179  int rv;
180  vnet_main_t *vnm = vnet_get_main ();
181  vl_api_l2tpv3_interface_enable_disable_reply_t *rmp;
182 
184 
186  (vnm, ntohl (mp->sw_if_index), mp->enable_disable);
187 
189 
190  REPLY_MACRO (VL_API_L2TPV3_INTERFACE_ENABLE_DISABLE_REPLY);
191 }
192 
195 {
196  int rv = 0;
197  l2t_main_t *lm = &l2t_main;
198  vl_api_l2tpv3_set_lookup_key_reply_t *rmp;
199 
201  {
202  rv = VNET_API_ERROR_INVALID_VALUE;
203  goto out;
204  }
205 
206  lm->lookup_type = (ip6_to_l2_lookup_t) mp->key;
207 
208 out:
209  REPLY_MACRO (VL_API_L2TPV3_SET_LOOKUP_KEY_REPLY);
210 }
211 
212 /*
213  * l2tp_api_hookup
214  * Add vpe's API message handlers to the table.
215  * vlib has already mapped shared memory and
216  * added the client registration handlers.
217  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
218  */
219 #include <l2tp/l2tp.api.c>
220 
221 static clib_error_t *
223 {
224  /*
225  * Set up the (msg_name, crc, message-id) table
226  */
228 
229  return 0;
230 }
231 
233 
234 #include <vlib/unix/plugin.h>
235 #include <vpp/app/version.h>
236 
237 /* *INDENT-OFF* */
239  .version = VPP_BUILD_VER,
240  .description = "Layer 2 Tunneling Protocol v3 (L2TP)",
241 };
242 /* *INDENT-ON* */
243 
244 
245 /*
246  * fd.io coding-style-patch-verification: ON
247  *
248  * Local Variables:
249  * eval: (c-set-style "gnu")
250  * End:
251  */
vl_api_l2tpv3_create_tunnel_t::l2_sublayer_present
bool l2_sublayer_present
Definition: l2tp.api:44
vl_api_sw_if_l2tpv3_tunnel_details_t::local_cookie
u64 local_cookie[2]
Definition: l2tp.api:78
im
vnet_interface_main_t * im
Definition: interface_output.c:415
VALIDATE_SW_IF_INDEX
#define VALIDATE_SW_IF_INDEX(mp)
Definition: api_helper_macros.h:281
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
vl_api_sw_if_l2tpv3_tunnel_details_t::client_address
vl_api_address_t client_address
Definition: l2tp.api:74
api.h
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
vnet_sw_interface_t
Definition: interface.h:869
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
ip6_to_l2_lookup_t
ip6_to_l2_lookup_t
Definition: l2tp.h:51
l2t_session_t::l2_sublayer_present
u8 l2_sublayer_present
Definition: l2tp.h:45
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
l2tp_api_hookup
static clib_error_t * l2tp_api_hookup(vlib_main_t *vm)
Definition: l2tp_api.c:222
vl_api_l2tpv3_create_tunnel_t::local_session_id
u32 local_session_id
Definition: l2tp.api:40
l2t_main_t
Definition: l2tp.h:58
l2t_main_t::sessions
l2t_session_t * sessions
Definition: l2tp.h:61
vl_api_sw_if_l2tpv3_tunnel_details_t::remote_cookie
u64 remote_cookie
Definition: l2tp.api:79
vl_api_sw_if_l2tpv3_tunnel_details_t::remote_session_id
u32 remote_session_id
Definition: l2tp.api:77
vpe_api_main_t
Definition: api_helper_macros.h:414
vl_api_l2tpv3_create_tunnel_t::client_address
vl_api_address_t client_address
Definition: l2tp.api:38
vl_api_l2tpv3_set_lookup_key_t
Definition: l2tp.api:104
create_l2tpv3_ipv6_tunnel
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
l2t_session_t::hw_if_index
u32 hw_if_index
Definition: l2tp.h:38
am
app_main_t * am
Definition: application.c:489
l2t_session_t::local_session_id
u32 local_session_id
Definition: l2tp.h:34
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
send_sw_if_l2tpv3_tunnel_details
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:42
plugin.h
vl_api_sw_if_l2tpv3_tunnel_details_t::interface_name
string interface_name[64]
Definition: l2tp.api:73
vl_api_sw_if_l2tpv3_tunnel_details_t::our_address
vl_api_address_t our_address
Definition: l2tp.api:75
ip_address_decode
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:172
vnet_sw_interface_t::sw_if_index
u32 sw_if_index
Definition: interface.h:876
REPLY_MSG_ID_BASE
#define REPLY_MSG_ID_BASE
Definition: l2tp_api.c:37
pool_foreach
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:534
vl_api_l2tpv3_create_tunnel_t::our_address
vl_api_address_t our_address
Definition: l2tp.api:39
L2T_LOOKUP_KEY_API_SESSION_ID
@ L2T_LOOKUP_KEY_API_SESSION_ID
Definition: l2tp.api:101
l2t_main_t::vnet_main
vnet_main_t * vnet_main
Definition: l2tp.h:81
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
vl_api_sw_if_l2tpv3_tunnel_dump_t::context
u32 context
Definition: l2tp.api:86
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
hash_get
#define hash_get(h, key)
Definition: hash.h:249
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
l2t_main_t::lookup_type
ip6_to_l2_lookup_t lookup_type
Definition: l2tp.h:68
uword
u64 uword
Definition: types.h:112
interface.h
vl_api_l2tpv3_set_tunnel_cookies_t_handler
static void vl_api_l2tpv3_set_tunnel_cookies_t_handler(vl_api_l2tpv3_set_tunnel_cookies_t *mp)
Definition: l2tp_api.c:158
l2t_session_t::client_address
ip6_address_t client_address
Definition: l2tp.h:29
vl_api_sw_if_l2tpv3_tunnel_details_t::l2_sublayer_present
bool l2_sublayer_present
Definition: l2tp.api:80
vl_api_sw_if_l2tpv3_tunnel_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:72
vl_api_sw_if_l2tpv3_tunnel_details_t::context
u32 context
Definition: l2tp.api:71
vl_api_l2tpv3_create_tunnel_t::encap_vrf_id
u32 encap_vrf_id
Definition: l2tp.api:45
IP46_TYPE_IP6
@ IP46_TYPE_IP6
Definition: ip46_address.h:27
vl_api_l2tpv3_set_lookup_key_t_handler
static void vl_api_l2tpv3_set_lookup_key_t_handler(vl_api_l2tpv3_set_lookup_key_t *mp)
Definition: l2tp_api.c:194
l2tpv3_interface_enable_disable
int l2tpv3_interface_enable_disable(vnet_main_t *vnm, u32 sw_if_index, int enable_disable)
Definition: l2tp.c:614
vl_api_l2tpv3_create_tunnel_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:57
vl_api_l2tpv3_create_tunnel_t::remote_cookie
u64 remote_cookie
Definition: l2tp.api:43
vl_api_l2tpv3_create_tunnel_t
l2tpv3 tunnel interface create request
Definition: l2tp.api:34
BAD_SW_IF_INDEX_LABEL
#define BAD_SW_IF_INDEX_LABEL
Definition: api_helper_macros.h:289
ip6_main
ip6_main_t ip6_main
Definition: ip6_forward.c:2785
l2tp_base_msg_id
static u32 l2tp_base_msg_id
Base message ID fot the plugin.
Definition: l2tp_api.c:36
vnet_main_t
Definition: vnet.h:76
vl_api_l2tpv3_set_tunnel_cookies_t
Definition: l2tp.api:60
vl_api_l2tpv3_create_tunnel_t::local_cookie
u64 local_cookie
Definition: l2tp.api:42
l2t_session_t::remote_cookie
u64 remote_cookie
Definition: l2tp.h:33
vl_api_sw_if_l2tpv3_tunnel_details_t
Definition: l2tp.api:69
format_fns.h
vl_api_l2tpv3_set_tunnel_cookies_t::new_remote_cookie
u64 new_remote_cookie
Definition: l2tp.api:66
l2t_session_t::local_cookie
u64 local_cookie[2]
Definition: l2tp.h:32
format
description fragment has unexpected format
Definition: map.api:433
vl_api_l2tpv3_create_tunnel_t_handler
static void vl_api_l2tpv3_create_tunnel_t_handler(vl_api_l2tpv3_create_tunnel_t *mp)
Definition: l2tp_api.c:101
vnet_get_hw_sw_interface
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
Definition: interface_funcs.h:72
u32
unsigned int u32
Definition: types.h:88
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(l2tp_api_hookup)
si
vnet_sw_interface_t * si
Definition: interface_output.c:418
api_helper_macros.h
vl_api_l2tpv3_interface_enable_disable_t::enable_disable
bool enable_disable
Definition: l2tp.api:93
ip6_main_t
Definition: ip6.h:110
vl_api_sw_if_l2tpv3_tunnel_dump_t_handler
static void vl_api_sw_if_l2tpv3_tunnel_dump_t_handler(vl_api_sw_if_l2tpv3_tunnel_dump_t *mp)
Definition: l2tp_api.c:80
vl_api_sw_if_l2tpv3_tunnel_dump_t::client_index
u32 client_index
Definition: l2tp.api:85
l2t_main
l2t_main_t l2t_main
Definition: l2tp.c:26
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
vl_api_l2tpv3_interface_enable_disable_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:94
ip_address_encode
void ip_address_encode(const ip46_address_t *in, ip46_type_t type, vl_api_address_t *out)
Definition: ip_types_api.c:206
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_api_l2tpv3_create_tunnel_reply_t
l2tpv3 tunnel interface create response
Definition: l2tp.api:53
vl_api_sw_if_l2tpv3_tunnel_dump_t
Definition: l2tp.api:83
vl_api_sw_if_l2tpv3_tunnel_details_t::local_session_id
u32 local_session_id
Definition: l2tp.api:76
l2t_session_t
Definition: l2tp.h:25
l2tp.h
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
api_errno.h
vl_api_l2tpv3_set_tunnel_cookies_t::new_local_cookie
u64 new_local_cookie
Definition: l2tp.api:65
ADDRESS_IP4
@ ADDRESS_IP4
Definition: ip_types.api:22
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
l2tpv3_set_tunnel_cookies
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
vl_api_l2tpv3_set_tunnel_cookies_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: l2tp.api:64
ip_types_api.h
l2t_session_t::remote_session_id
u32 remote_session_id
Definition: l2tp.h:35
l2t_session_t::our_address
ip6_address_t our_address
Definition: l2tp.h:28
format_vnet_sw_interface_name
format_function_t format_vnet_sw_interface_name
Definition: interface_funcs.h:456
vpe_api_main
vpe_api_main_t vpe_api_main
Definition: interface_api.c:47
vl_api_l2tpv3_create_tunnel_t::remote_session_id
u32 remote_session_id
Definition: l2tp.api:41
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
vl_api_l2tpv3_set_lookup_key_t::key
vl_api_l2t_lookup_key_t key
Definition: l2tp.api:108
vl_api_l2tpv3_interface_enable_disable_t
Definition: l2tp.api:89
vl_api_l2tpv3_interface_enable_disable_t_handler
static void vl_api_l2tpv3_interface_enable_disable_t_handler(vl_api_l2tpv3_interface_enable_disable_t *mp)
Definition: l2tp_api.c:177