blob: da0cb169296c39161fd809adc779bd3388304feb [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>
Ole Troan298c6952018-03-08 12:30:43 +010025
Ole Troan2a1ca782019-09-19 01:08:30 +020026#include <vnet/ipip/ipip.api_enum.h>
27#include <vnet/ipip/ipip.api_types.h>
Ole Troan298c6952018-03-08 12:30:43 +010028
Ole Troan2a1ca782019-09-19 01:08:30 +020029#define REPLY_MSG_ID_BASE im->msg_id_base
Ole Troan298c6952018-03-08 12:30:43 +010030#include <vlibapi/api_helper_macros.h>
31
Ole Troan298c6952018-03-08 12:30:43 +010032static void
33vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp)
34{
Ole Troan2a1ca782019-09-19 01:08:30 +020035 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010036 vl_api_ipip_add_tunnel_reply_t *rmp;
37 int rv = 0;
Neale Ranns61502112018-08-22 00:21:14 -070038 u32 fib_index, sw_if_index = ~0;
Neale Rannscbd08242019-05-26 11:34:27 -070039 ip46_address_t src, dst;
40 ip46_type_t itype[2];
Ole Troan298c6952018-03-08 12:30:43 +010041
Neale Rannscbd08242019-05-26 11:34:27 -070042 itype[0] = ip_address_decode (&mp->tunnel.src, &src);
43 itype[1] = ip_address_decode (&mp->tunnel.dst, &dst);
44
45 if (itype[0] != itype[1])
Ole Troan298c6952018-03-08 12:30:43 +010046 {
Neale Rannscbd08242019-05-26 11:34:27 -070047 rv = VNET_API_ERROR_INVALID_PROTOCOL;
48 goto out;
Ole Troan298c6952018-03-08 12:30:43 +010049 }
50
Neale Rannscbd08242019-05-26 11:34:27 -070051 if (ip46_address_is_equal (&src, &dst))
52 {
53 rv = VNET_API_ERROR_SAME_SRC_DST;
54 goto out;
55 }
56
57 fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]),
58 ntohl (mp->tunnel.table_id));
Neale Ranns61502112018-08-22 00:21:14 -070059
60 if (~0 == fib_index)
61 {
62 rv = VNET_API_ERROR_NO_SUCH_FIB;
63 }
64 else
65 {
Neale Rannscbd08242019-05-26 11:34:27 -070066 rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ?
Neale Ranns61502112018-08-22 00:21:14 -070067 IPIP_TRANSPORT_IP6 :
68 IPIP_TRANSPORT_IP4),
Neale Rannscbd08242019-05-26 11:34:27 -070069 ntohl (mp->tunnel.instance), &src, &dst,
70 fib_index, mp->tunnel.tc_tos, &sw_if_index);
Neale Ranns61502112018-08-22 00:21:14 -070071 }
Ole Troan298c6952018-03-08 12:30:43 +010072
Neale Rannscbd08242019-05-26 11:34:27 -070073out:
Ole Troan298c6952018-03-08 12:30:43 +010074 /* *INDENT-OFF* */
75 REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY,
Neale Ranns61502112018-08-22 00:21:14 -070076 ({
77 rmp->sw_if_index = ntohl(sw_if_index);
78 }));
Ole Troan298c6952018-03-08 12:30:43 +010079 /* *INDENT-ON* */
80}
81
82static void
83vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp)
84{
Ole Troan2a1ca782019-09-19 01:08:30 +020085 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010086 vl_api_ipip_del_tunnel_reply_t *rmp;
87
88 int rv = ipip_del_tunnel (ntohl (mp->sw_if_index));
89
90 REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY);
91}
92
93static void
Ole Troan2a1ca782019-09-19 01:08:30 +020094send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp)
Ole Troan298c6952018-03-08 12:30:43 +010095{
Ole Troan2a1ca782019-09-19 01:08:30 +020096 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +010097 vl_api_ipip_tunnel_details_t *rmp;
98 bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false;
99 fib_table_t *ft;
Ole Troan2a1ca782019-09-19 01:08:30 +0200100 int rv = 0;
Neale Rannscbd08242019-05-26 11:34:27 -0700101
102 ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 :
103 FIB_PROTOCOL_IP4));
104
Ole Troan2a1ca782019-09-19 01:08:30 +0200105 /* *INDENT-OFF* */
106 REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS,
107 ({
108 ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src);
109 ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst);
110 rmp->tunnel.table_id = htonl (ft->ft_table_id);
111 rmp->tunnel.instance = htonl (t->user_instance);
112 rmp->tunnel.sw_if_index = htonl (t->sw_if_index);
113 }));
114 /* *INDENT-ON* */
Ole Troan298c6952018-03-08 12:30:43 +0100115}
116
117static void
118vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp)
119{
Ole Troan2a1ca782019-09-19 01:08:30 +0200120 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100121 ipip_tunnel_t *t;
122 u32 sw_if_index;
123
Ole Troan298c6952018-03-08 12:30:43 +0100124 sw_if_index = ntohl (mp->sw_if_index);
125
126 if (sw_if_index == ~0)
127 {
128 /* *INDENT-OFF* */
Ole Troan2a1ca782019-09-19 01:08:30 +0200129 pool_foreach(t, im->tunnels,
Neale Rannsc87b66c2019-02-07 07:26:12 -0800130 ({
Ole Troan2a1ca782019-09-19 01:08:30 +0200131 send_ipip_tunnel_details(t, mp);
Neale Rannsc87b66c2019-02-07 07:26:12 -0800132 }));
Ole Troan298c6952018-03-08 12:30:43 +0100133 /* *INDENT-ON* */
134 }
135 else
136 {
137 t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index);
138 if (t)
Ole Troan2a1ca782019-09-19 01:08:30 +0200139 send_ipip_tunnel_details (t, mp);
Ole Troan298c6952018-03-08 12:30:43 +0100140 }
141}
142
143static void
144vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp)
145{
Ole Troan2a1ca782019-09-19 01:08:30 +0200146 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100147 vl_api_ipip_6rd_add_tunnel_reply_t *rmp;
Neale Ranns61502112018-08-22 00:21:14 -0700148 u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index;
149 int rv;
Ole Troan298c6952018-03-08 12:30:43 +0100150
Neale Ranns61502112018-08-22 00:21:14 -0700151 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id));
152 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id));
153
154 if (~0 == ip4_fib_index || ~0 == ip6_fib_index)
155
156 {
157 rv = VNET_API_ERROR_NO_SUCH_FIB;
158 }
159 else
160 {
Paul Vinciguerraab055082019-06-06 14:07:55 -0400161 rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200162 mp->ip6_prefix.len,
Paul Vinciguerraab055082019-06-06 14:07:55 -0400163 (ip4_address_t *) & mp->ip4_prefix.address,
Ole Troan288e0932019-05-29 12:30:05 +0200164 mp->ip4_prefix.len,
Ole Troan298c6952018-03-08 12:30:43 +0100165 (ip4_address_t *) & mp->ip4_src,
166 mp->security_check,
Neale Ranns61502112018-08-22 00:21:14 -0700167 ip4_fib_index, ip6_fib_index,
168 &sixrd_tunnel_index);
169 }
Ole Troan298c6952018-03-08 12:30:43 +0100170
Neale Ranns61502112018-08-22 00:21:14 -0700171 /* *INDENT-OFF* */
172 REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY,
173 ({
174 rmp->sw_if_index = htonl (sixrd_tunnel_index);
175 }));
176 /* *INDENT-ON* */
Ole Troan298c6952018-03-08 12:30:43 +0100177}
178
179static void
180vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp)
181{
Ole Troan2a1ca782019-09-19 01:08:30 +0200182 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100183 vl_api_ipip_6rd_del_tunnel_reply_t *rmp;
184
185 int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index));
186
187 REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY);
188}
189
190/*
191 * ipip_api_hookup
192 * Add vpe's API message handlers to the table.
Jim Thompsonf324dec2019-04-08 03:22:21 -0500193 * vlib has already mapped shared memory and
Ole Troan298c6952018-03-08 12:30:43 +0100194 * added the client registration handlers.
195 * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
196 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200197/* API definitions */
198#include <vnet/format_fns.h>
199#include <vnet/ipip/ipip.api.c>
Ole Troan298c6952018-03-08 12:30:43 +0100200
201static clib_error_t *
202ipip_api_hookup (vlib_main_t * vm)
203{
Ole Troan2a1ca782019-09-19 01:08:30 +0200204 ipip_main_t *im = &ipip_main;
Ole Troan298c6952018-03-08 12:30:43 +0100205
206 /*
207 * Set up the (msg_name, crc, message-id) table
208 */
Ole Troan2a1ca782019-09-19 01:08:30 +0200209 im->msg_id_base = setup_message_id_table ();
Ole Troan298c6952018-03-08 12:30:43 +0100210
211 return 0;
212}
213
214VLIB_API_INIT_FUNCTION (ipip_api_hookup);
215
216/*
217 * fd.io coding-style-patch-verification: ON
218 *
219 * Local Variables:
220 * eval: (c-set-style "gnu")
221 * End:
222 */