blob: 24c1f0d56004cd40f25244d523715d114d26421c [file] [log] [blame]
Pavel Kotucek56f32cc2016-12-22 10:03:25 +01001/*
2 *------------------------------------------------------------------
3 * ipsec_gre_api.c - ipsec_gre 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/ipsec-gre/ipsec_gre.h>
Neale Rannse524d452019-02-19 15:22:46 +000026#include <vnet/ip/ip_types_api.h>
Pavel Kotucek56f32cc2016-12-22 10:03:25 +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 \
Neale Rannse524d452019-02-19 15:22:46 +000047_(IPSEC_GRE_TUNNEL_ADD_DEL, ipsec_gre_tunnel_add_del) \
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010048_(IPSEC_GRE_TUNNEL_DUMP, ipsec_gre_tunnel_dump)
49
50static void
Neale Rannse524d452019-02-19 15:22:46 +000051vl_api_ipsec_gre_tunnel_add_del_t_handler (vl_api_ipsec_gre_tunnel_add_del_t *
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010052 mp)
53{
Neale Rannse524d452019-02-19 15:22:46 +000054 vl_api_ipsec_gre_tunnel_add_del_reply_t *rmp;
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010055 int rv = 0;
Neale Rannse524d452019-02-19 15:22:46 +000056 ipsec_gre_tunnel_add_del_args_t _a, *a = &_a;
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010057 u32 sw_if_index = ~0;
58
Neale Rannse524d452019-02-19 15:22:46 +000059 clib_memset (a, 0, sizeof (*a));
60
61 ip4_address_decode (mp->tunnel.src, &a->src);
62 ip4_address_decode (mp->tunnel.dst, &a->dst);
63
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010064 /* Check src & dst are different */
Neale Rannse524d452019-02-19 15:22:46 +000065 if (a->src.as_u32 == a->dst.as_u32)
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010066 {
67 rv = VNET_API_ERROR_SAME_SRC_DST;
68 goto out;
69 }
70
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010071 a->is_add = mp->is_add;
Neale Rannse524d452019-02-19 15:22:46 +000072 a->local_sa_id = ntohl (mp->tunnel.local_sa_id);
73 a->remote_sa_id = ntohl (mp->tunnel.remote_sa_id);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010074
Neale Rannse524d452019-02-19 15:22:46 +000075 rv = vnet_ipsec_gre_tunnel_add_del (a, &sw_if_index);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010076
77out:
Neale Rannse524d452019-02-19 15:22:46 +000078 /* *INDENT-OFF* */
79 REPLY_MACRO2(VL_API_GRE_ADD_DEL_TUNNEL_REPLY,
80 ({
81 rmp->sw_if_index = ntohl (sw_if_index);
82 }));
83 /* *INDENT-ON* */
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010084}
85
86static void send_ipsec_gre_tunnel_details
Florin Coras6c4dae22018-01-09 06:39:23 -080087 (ipsec_gre_tunnel_t * t, vl_api_registration_t * reg, u32 context)
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010088{
89 vl_api_ipsec_gre_tunnel_details_t *rmp;
90
91 rmp = vl_msg_api_alloc (sizeof (*rmp));
Dave Barachb7b92992018-10-17 10:38:51 -040092 clib_memset (rmp, 0, sizeof (*rmp));
Pavel Kotucek56f32cc2016-12-22 10:03:25 +010093 rmp->_vl_msg_id = ntohs (VL_API_IPSEC_GRE_TUNNEL_DETAILS);
Neale Rannse524d452019-02-19 15:22:46 +000094
95 ip4_address_encode (&t->tunnel_src, rmp->tunnel.src);
96 ip4_address_encode (&t->tunnel_dst, rmp->tunnel.dst);
97 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
98 rmp->tunnel.local_sa_id = htonl (t->local_sa_id);
99 rmp->tunnel.remote_sa_id = htonl (t->remote_sa_id);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100100 rmp->context = context;
101
Florin Coras6c4dae22018-01-09 06:39:23 -0800102 vl_api_send_msg (reg, (u8 *) rmp);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100103}
104
105static void vl_api_ipsec_gre_tunnel_dump_t_handler
106 (vl_api_ipsec_gre_tunnel_dump_t * mp)
107{
Florin Coras6c4dae22018-01-09 06:39:23 -0800108 vl_api_registration_t *reg;
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100109 ipsec_gre_main_t *igm = &ipsec_gre_main;
110 ipsec_gre_tunnel_t *t;
111 u32 sw_if_index;
112
Florin Coras6c4dae22018-01-09 06:39:23 -0800113 reg = vl_api_client_index_to_registration (mp->client_index);
114 if (!reg)
115 return;
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100116
117 sw_if_index = ntohl (mp->sw_if_index);
118
119 if (~0 == sw_if_index)
120 {
121 /* *INDENT-OFF* */
122 pool_foreach (t, igm->tunnels,
123 ({
Florin Coras6c4dae22018-01-09 06:39:23 -0800124 send_ipsec_gre_tunnel_details(t, reg, mp->context);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100125 }));
126 /* *INDENT-ON* */
127 }
128 else
129 {
130 if ((sw_if_index >= vec_len (igm->tunnel_index_by_sw_if_index)) ||
131 (~0 == igm->tunnel_index_by_sw_if_index[sw_if_index]))
132 {
133 return;
134 }
135 t = &igm->tunnels[igm->tunnel_index_by_sw_if_index[sw_if_index]];
Florin Coras6c4dae22018-01-09 06:39:23 -0800136 send_ipsec_gre_tunnel_details (t, reg, mp->context);
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100137 }
138}
139
140/*
141 * ipsec_gre_api_hookup
142 * Add vpe's API message handlers to the table.
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700143 * vlib has already mapped shared memory and
Pavel Kotucek56f32cc2016-12-22 10:03:25 +0100144 * added the client registration handlers.
145 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
146 */
147#define vl_msg_name_crc_list
148#include <vnet/vnet_all_api_h.h>
149#undef vl_msg_name_crc_list
150
151static void
152setup_message_id_table (api_main_t * am)
153{
154#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
155 foreach_vl_msg_name_crc_ipsec_gre;
156#undef _
157}
158
159static clib_error_t *
160ipsec_gre_api_hookup (vlib_main_t * vm)
161{
162 api_main_t *am = &api_main;
163
164#define _(N,n) \
165 vl_msg_api_set_handlers(VL_API_##N, #n, \
166 vl_api_##n##_t_handler, \
167 vl_noop_handler, \
168 vl_api_##n##_t_endian, \
169 vl_api_##n##_t_print, \
170 sizeof(vl_api_##n##_t), 1);
171 foreach_vpe_api_msg;
172#undef _
173
174 /*
175 * Set up the (msg_name, crc, message-id) table
176 */
177 setup_message_id_table (am);
178
179 return 0;
180}
181
182VLIB_API_INIT_FUNCTION (ipsec_gre_api_hookup);
183
184/*
185 * fd.io coding-style-patch-verification: ON
186 *
187 * Local Variables:
188 * eval: (c-set-style "gnu")
189 * End:
190 */