blob: dcd3ec875df41fad4a06bd41d1d4eb87fd8a9d16 [file] [log] [blame]
Ole Troan298c6952018-03-08 12:30:43 +01001/*
2 * ipip_api.c - ipip api
3 *
4 * Copyright (c) 2018 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <vlibmemory/api.h>
19#include <vnet/api_errno.h>
20#include <vnet/fib/fib_table.h>
21#include <vnet/interface.h>
22#include <vnet/ipip/ipip.h>
23#include <vnet/vnet.h>
Neale Rannscbd08242019-05-26 11:34:27 -070024#include <vnet/ip/ip_types_api.h>
Neale Ranns59ff9182019-12-29 23:55:18 +000025#include <vnet/tunnel/tunnel_types_api.h>
Ole Troan298c6952018-03-08 12:30:43 +010026
Ole Troan2a1ca782019-09-19 01:08:30 +020027#include <vnet/ipip/ipip.api_enum.h>
28#include <vnet/ipip/ipip.api_types.h>
Ole Troan298c6952018-03-08 12:30:43 +010029
Ole Troan2a1ca782019-09-19 01:08:30 +020030#define REPLY_MSG_ID_BASE im->msg_id_base
Ole Troan298c6952018-03-08 12:30:43 +010031#include <vlibapi/api_helper_macros.h>
32
Ole Troan298c6952018-03-08 12:30:43 +010033static void
34vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp)
35{
Ole Troan2a1ca782019-09-19 01:08:30 +020036 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010037 vl_api_ipip_add_tunnel_reply_t *rmp;
38 int rv = 0;
Neale Ranns61502112018-08-22 00:21:14 -070039 u32 fib_index, sw_if_index = ~0;
Neale Ranns59ff9182019-12-29 23:55:18 +000040 tunnel_encap_decap_flags_t flags;
Neale Rannscbd08242019-05-26 11:34:27 -070041 ip46_address_t src, dst;
42 ip46_type_t itype[2];
Neale Ranns14053c92019-12-29 23:55:18 +000043 tunnel_mode_t mode;
Ole Troan298c6952018-03-08 12:30:43 +010044
Neale Rannscbd08242019-05-26 11:34:27 -070045 itype[0] = ip_address_decode (&mp->tunnel.src, &src);
46 itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
47
48 if (itype[0] != itype[1])
Ole Troan298c6952018-03-08 12:30:43 +010049 {
Neale Rannscbd08242019-05-26 11:34:27 -070050 rv = VNET_API_ERROR_INVALID_PROTOCOL;
51 goto out;
Ole Troan298c6952018-03-08 12:30:43 +010052 }
53
Neale Rannscbd08242019-05-26 11:34:27 -070054 if (ip46_address_is_equal (&src, &dst))
55 {
56 rv = VNET_API_ERROR_SAME_SRC_DST;
57 goto out;
58 }
59
Neale Ranns59ff9182019-12-29 23:55:18 +000060 rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags);
Neale Ranns95346962019-11-25 13:04:44 +000061
62 if (rv)
63 goto out;
64
Neale Ranns14053c92019-12-29 23:55:18 +000065 rv = tunnel_mode_decode (mp->tunnel.mode, &mode);
66
67 if (rv)
68 goto out;
69
Neale Rannscbd08242019-05-26 11:34:27 -070070 fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
71 ntohl (mp->tunnel.table_id));
Neale Ranns61502112018-08-22 00:21:14 -070072
73 if (~0 == fib_index)
74 {
75 rv = VNET_API_ERROR_NO_SUCH_FIB;
76 }
77 else
78 {
Neale Rannscbd08242019-05-26 11:34:27 -070079 rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
Neale Ranns61502112018-08-22 00:21:14 -070080 IPIP_TRANSPORT_IP6 :
81 IPIP_TRANSPORT_IP4),
Neale Rannscbd08242019-05-26 11:34:27 -070082 ntohl (mp->tunnel.instance), &src, &dst,
Neale Ranns95346962019-11-25 13:04:44 +000083 fib_index, flags,
Neale Ranns14053c92019-12-29 23:55:18 +000084 ip_dscp_decode (mp->tunnel.dscp), mode,
85 &sw_if_index);
Neale Ranns61502112018-08-22 00:21:14 -070086 }
Ole Troan298c6952018-03-08 12:30:43 +010087
Neale Rannscbd08242019-05-26 11:34:27 -070088out:
Ole Troan298c6952018-03-08 12:30:43 +010089 REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
Neale Ranns61502112018-08-22 00:21:14 -070090 ({
91 rmp->sw_if_index = ntohl(sw_if_index);
92 }));
Ole Troan298c6952018-03-08 12:30:43 +010093}
94
95static void
96vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
97{
Ole Troan2a1ca782019-09-19 01:08:30 +020098 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010099 vl_api_ipip_del_tunnel_reply_t *rmp;
100
101 int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
102
103 REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
104}
105
Matthew Smithe870d5b2023-07-14 16:05:39 +0000106static vl_api_tunnel_mode_t
107ipip_tunnel_mode_encode (ipip_mode_t mode)
108{
109 switch (mode)
110 {
111 case IPIP_MODE_P2P:
112 return TUNNEL_API_MODE_P2P;
113 case IPIP_MODE_P2MP:
114 return TUNNEL_API_MODE_MP;
115 case IPIP_MODE_6RD:
116 return TUNNEL_API_MODE_P2P;
117 default:
118 return TUNNEL_API_MODE_P2P;
119 }
120}
121
Ole Troan298c6952018-03-08 12:30:43 +0100122static void
Ole Troan2a1ca782019-09-19 01:08:30 +0200123send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp)
Ole Troan298c6952018-03-08 12:30:43 +0100124{
Ole Troan2a1ca782019-09-19 01:08:30 +0200125 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100126 vl_api_ipip_tunnel_details_t *rmp;
127 bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
128 fib_table_t *ft;
Neale Rannscbd08242019-05-26 11:34:27 -0700129
Matthew Smithe870d5b2023-07-14 16:05:39 +0000130 ft = fib_table_get (t->fib_index,
131 (is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4));
Neale Rannscbd08242019-05-26 11:34:27 -0700132
Ole Troan2a1ca782019-09-19 01:08:30 +0200133 REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
134 ({
135 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
136 ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst);
137 rmp->tunnel.table_id = htonl (ft->ft_table_id);
138 rmp->tunnel.instance = htonl (t->user_instance);
139 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
Neale Ranns95346962019-11-25 13:04:44 +0000140 rmp->tunnel.dscp = ip_dscp_encode(t->dscp);
Neale Ranns59ff9182019-12-29 23:55:18 +0000141 rmp->tunnel.flags = tunnel_encap_decap_flags_encode(t->flags);
Matthew Smithe870d5b2023-07-14 16:05:39 +0000142 rmp->tunnel.mode = ipip_tunnel_mode_encode (t->mode);
Ole Troan2a1ca782019-09-19 01:08:30 +0200143 }));
Ole Troan298c6952018-03-08 12:30:43 +0100144}
145
146static void
147vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
148{
Ole Troan2a1ca782019-09-19 01:08:30 +0200149 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100150 ipip_tunnel_t *t;
151 u32 sw_if_index;
152
Ole Troan298c6952018-03-08 12:30:43 +0100153 sw_if_index = ntohl (mp->sw_if_index);
154
155 if (sw_if_index == ~0)
156 {
Damjan Marionb2c31b62020-12-13 21:47:40 +0100157 pool_foreach (t, im->tunnels)
158 {
Ole Troan2a1ca782019-09-19 01:08:30 +0200159 send_ipip_tunnel_details(t, mp);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100160 }
Ole Troan298c6952018-03-08 12:30:43 +0100161 }
162 else
163 {
164 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
165 if (t)
Ole Troan2a1ca782019-09-19 01:08:30 +0200166 send_ipip_tunnel_details (t, mp);
Ole Troan298c6952018-03-08 12:30:43 +0100167 }
168}
169
170static void
171vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
172{
Ole Troan2a1ca782019-09-19 01:08:30 +0200173 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100174 vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
Neale Ranns61502112018-08-22 00:21:14 -0700175 u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
176 int rv;
Ole Troan298c6952018-03-08 12:30:43 +0100177
Neale Rannsd0576252020-02-20 13:16:49 +0000178 sixrd_tunnel_index = ~0;
Neale Ranns61502112018-08-22 00:21:14 -0700179 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
180 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
181
182 if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
183
184 {
185 rv = VNET_API_ERROR_NO_SUCH_FIB;
186 }
187 else
188 {
Paul Vinciguerraab055082019-06-06 14:07:55 -0400189 rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200190 mp->ip6_prefix.len,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400191 (ip4_address_t *) & mp->ip4_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200192 mp->ip4_prefix.len,
Ole Troan298c6952018-03-08 12:30:43 +0100193 (ip4_address_t *) & mp->ip4_src,
194 mp->security_check,
Neale Ranns61502112018-08-22 00:21:14 -0700195 ip4_fib_index, ip6_fib_index,
196 &sixrd_tunnel_index);
197 }
Ole Troan298c6952018-03-08 12:30:43 +0100198
Neale Ranns61502112018-08-22 00:21:14 -0700199 REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
200 ({
201 rmp->sw_if_index = htonl (sixrd_tunnel_index);
202 }));
Ole Troan298c6952018-03-08 12:30:43 +0100203}
204
205static void
206vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
207{
Ole Troan2a1ca782019-09-19 01:08:30 +0200208 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100209 vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
210
211 int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
212
213 REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
214}
215
216/*
217 * ipip_api_hookup
218 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500219 * vlib has already mapped shared memory and
Ole Troan298c6952018-03-08 12:30:43 +0100220 * added the client registration handlers.
221 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
222 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200223/* API definitions */
224#include <vnet/format_fns.h>
225#include <vnet/ipip/ipip.api.c>
Ole Troan298c6952018-03-08 12:30:43 +0100226
227static clib_error_t *
228ipip_api_hookup (vlib_main_t * vm)
229{
Ole Troan2a1ca782019-09-19 01:08:30 +0200230 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100231
232 /*
233 * Set up the (msg_name, crc, message-id) table
234 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200235 im->msg_id_base = setup_message_id_table ();
Ole Troan298c6952018-03-08 12:30:43 +0100236
237 return 0;
238}
239
240VLIB_API_INIT_FUNCTION (ipip_api_hookup);
241
242/*
243 * fd.io coding-style-patch-verification: ON
244 *
245 * Local Variables:
246 * eval: (c-set-style "gnu")
247 * End:
248 */