blob: 47ff159b703c5d33ddd2128fa251e569556a24d8 [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 Ranns95346962019-11-25 13:04:44 +000025#include <vnet/ipip/ipip_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 Ranns95346962019-11-25 13:04:44 +000040 ipip_tunnel_flags_t flags;
Neale Rannscbd08242019-05-26 11:34:27 -070041 ip46_address_t src, dst;
42 ip46_type_t itype[2];
Ole Troan298c6952018-03-08 12:30:43 +010043
Neale Rannscbd08242019-05-26 11:34:27 -070044 itype[0] = ip_address_decode (&mp->tunnel.src, &src);
45 itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
46
47 if (itype[0] != itype[1])
Ole Troan298c6952018-03-08 12:30:43 +010048 {
Neale Rannscbd08242019-05-26 11:34:27 -070049 rv = VNET_API_ERROR_INVALID_PROTOCOL;
50 goto out;
Ole Troan298c6952018-03-08 12:30:43 +010051 }
52
Neale Rannscbd08242019-05-26 11:34:27 -070053 if (ip46_address_is_equal (&src, &dst))
54 {
55 rv = VNET_API_ERROR_SAME_SRC_DST;
56 goto out;
57 }
58
Neale Ranns95346962019-11-25 13:04:44 +000059 rv = ipip_tunnel_flags_decode (mp->tunnel.flags, &flags);
60
61 if (rv)
62 goto out;
63
Neale Rannscbd08242019-05-26 11:34:27 -070064 fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
65 ntohl (mp->tunnel.table_id));
Neale Ranns61502112018-08-22 00:21:14 -070066
67 if (~0 == fib_index)
68 {
69 rv = VNET_API_ERROR_NO_SUCH_FIB;
70 }
71 else
72 {
Neale Rannscbd08242019-05-26 11:34:27 -070073 rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
Neale Ranns61502112018-08-22 00:21:14 -070074 IPIP_TRANSPORT_IP6 :
75 IPIP_TRANSPORT_IP4),
Neale Rannscbd08242019-05-26 11:34:27 -070076 ntohl (mp->tunnel.instance), &src, &dst,
Neale Ranns95346962019-11-25 13:04:44 +000077 fib_index, flags,
78 ip_dscp_decode (mp->tunnel.dscp), &sw_if_index);
Neale Ranns61502112018-08-22 00:21:14 -070079 }
Ole Troan298c6952018-03-08 12:30:43 +010080
Neale Rannscbd08242019-05-26 11:34:27 -070081out:
Ole Troan298c6952018-03-08 12:30:43 +010082 /* *INDENT-OFF* */
83 REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
Neale Ranns61502112018-08-22 00:21:14 -070084 ({
85 rmp->sw_if_index = ntohl(sw_if_index);
86 }));
Ole Troan298c6952018-03-08 12:30:43 +010087 /* *INDENT-ON* */
88}
89
90static void
91vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
92{
Ole Troan2a1ca782019-09-19 01:08:30 +020093 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010094 vl_api_ipip_del_tunnel_reply_t *rmp;
95
96 int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
97
98 REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
99}
100
101static void
Ole Troan2a1ca782019-09-19 01:08:30 +0200102send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp)
Ole Troan298c6952018-03-08 12:30:43 +0100103{
Ole Troan2a1ca782019-09-19 01:08:30 +0200104 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100105 vl_api_ipip_tunnel_details_t *rmp;
106 bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
107 fib_table_t *ft;
Ole Troan2a1ca782019-09-19 01:08:30 +0200108 int rv = 0;
Neale Rannscbd08242019-05-26 11:34:27 -0700109
110 ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
111 FIB_PROTOCOL_IP4));
112
Ole Troan2a1ca782019-09-19 01:08:30 +0200113 /* *INDENT-OFF* */
114 REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
115 ({
116 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
117 ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst);
118 rmp->tunnel.table_id = htonl (ft->ft_table_id);
119 rmp->tunnel.instance = htonl (t->user_instance);
120 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
Neale Ranns95346962019-11-25 13:04:44 +0000121 rmp->tunnel.dscp = ip_dscp_encode(t->dscp);
122 rmp->tunnel.flags = ipip_tunnel_flags_encode(t->flags);
Ole Troan2a1ca782019-09-19 01:08:30 +0200123 }));
124 /* *INDENT-ON* */
Ole Troan298c6952018-03-08 12:30:43 +0100125}
126
127static void
128vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
129{
Ole Troan2a1ca782019-09-19 01:08:30 +0200130 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100131 ipip_tunnel_t *t;
132 u32 sw_if_index;
133
Ole Troan298c6952018-03-08 12:30:43 +0100134 sw_if_index = ntohl (mp->sw_if_index);
135
136 if (sw_if_index == ~0)
137 {
138 /* *INDENT-OFF* */
Ole Troan2a1ca782019-09-19 01:08:30 +0200139 pool_foreach(t, im->tunnels,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800140 ({
Ole Troan2a1ca782019-09-19 01:08:30 +0200141 send_ipip_tunnel_details(t, mp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800142 }));
Ole Troan298c6952018-03-08 12:30:43 +0100143 /* *INDENT-ON* */
144 }
145 else
146 {
147 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
148 if (t)
Ole Troan2a1ca782019-09-19 01:08:30 +0200149 send_ipip_tunnel_details (t, mp);
Ole Troan298c6952018-03-08 12:30:43 +0100150 }
151}
152
153static void
154vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
155{
Ole Troan2a1ca782019-09-19 01:08:30 +0200156 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100157 vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
Neale Ranns61502112018-08-22 00:21:14 -0700158 u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
159 int rv;
Ole Troan298c6952018-03-08 12:30:43 +0100160
Neale Ranns61502112018-08-22 00:21:14 -0700161 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
162 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
163
164 if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
165
166 {
167 rv = VNET_API_ERROR_NO_SUCH_FIB;
168 }
169 else
170 {
Paul Vinciguerraab055082019-06-06 14:07:55 -0400171 rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200172 mp->ip6_prefix.len,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400173 (ip4_address_t *) & mp->ip4_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200174 mp->ip4_prefix.len,
Ole Troan298c6952018-03-08 12:30:43 +0100175 (ip4_address_t *) & mp->ip4_src,
176 mp->security_check,
Neale Ranns61502112018-08-22 00:21:14 -0700177 ip4_fib_index, ip6_fib_index,
178 &sixrd_tunnel_index);
179 }
Ole Troan298c6952018-03-08 12:30:43 +0100180
Neale Ranns61502112018-08-22 00:21:14 -0700181 /* *INDENT-OFF* */
182 REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
183 ({
184 rmp->sw_if_index = htonl (sixrd_tunnel_index);
185 }));
186 /* *INDENT-ON* */
Ole Troan298c6952018-03-08 12:30:43 +0100187}
188
189static void
190vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
191{
Ole Troan2a1ca782019-09-19 01:08:30 +0200192 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100193 vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
194
195 int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
196
197 REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
198}
199
200/*
201 * ipip_api_hookup
202 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500203 * vlib has already mapped shared memory and
Ole Troan298c6952018-03-08 12:30:43 +0100204 * added the client registration handlers.
205 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
206 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200207/* API definitions */
208#include <vnet/format_fns.h>
209#include <vnet/ipip/ipip.api.c>
Ole Troan298c6952018-03-08 12:30:43 +0100210
211static clib_error_t *
212ipip_api_hookup (vlib_main_t * vm)
213{
Ole Troan2a1ca782019-09-19 01:08:30 +0200214 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100215
216 /*
217 * Set up the (msg_name, crc, message-id) table
218 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200219 im->msg_id_base = setup_message_id_table ();
Ole Troan298c6952018-03-08 12:30:43 +0100220
221 return 0;
222}
223
224VLIB_API_INIT_FUNCTION (ipip_api_hookup);
225
226/*
227 * fd.io coding-style-patch-verification: ON
228 *
229 * Local Variables:
230 * eval: (c-set-style "gnu")
231 * End:
232 */