Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 1 | /* |
| 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> |
| 24 | #include <vnet/vnet_msg_enum.h> |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 25 | #include <vnet/ip/ip_types_api.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 26 | |
| 27 | #define vl_typedefs /* define message structures */ |
| 28 | #include <vnet/vnet_all_api_h.h> |
| 29 | #undef vl_typedefs |
| 30 | |
| 31 | #define vl_endianfun /* define message structures */ |
| 32 | #include <vnet/vnet_all_api_h.h> |
| 33 | #undef vl_endianfun |
| 34 | |
| 35 | /* instantiate all the print functions we know about */ |
| 36 | #define vl_print(handle, ...) vlib_cli_output(handle, __VA_ARGS__) |
| 37 | #define vl_printfun |
| 38 | #include <vnet/vnet_all_api_h.h> |
| 39 | #undef vl_printfun |
| 40 | |
| 41 | #include <vlibapi/api_helper_macros.h> |
| 42 | |
| 43 | #define foreach_vpe_api_msg \ |
| 44 | _(IPIP_ADD_TUNNEL, ipip_add_tunnel) \ |
| 45 | _(IPIP_DEL_TUNNEL, ipip_del_tunnel) \ |
| 46 | _(IPIP_6RD_ADD_TUNNEL, ipip_6rd_add_tunnel) \ |
| 47 | _(IPIP_6RD_DEL_TUNNEL, ipip_6rd_del_tunnel) \ |
| 48 | _(IPIP_TUNNEL_DUMP, ipip_tunnel_dump) |
| 49 | |
| 50 | static void |
| 51 | vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp) |
| 52 | { |
| 53 | vl_api_ipip_add_tunnel_reply_t *rmp; |
| 54 | int rv = 0; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 55 | u32 fib_index, sw_if_index = ~0; |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 56 | ip46_address_t src, dst; |
| 57 | ip46_type_t itype[2]; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 58 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 59 | itype[0] = ip_address_decode (&mp->tunnel.src, &src); |
| 60 | itype[1] = ip_address_decode (&mp->tunnel.dst, &dst); |
| 61 | |
| 62 | if (itype[0] != itype[1]) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 63 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 64 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
| 65 | goto out; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 68 | if (ip46_address_is_equal (&src, &dst)) |
| 69 | { |
| 70 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 71 | goto out; |
| 72 | } |
| 73 | |
| 74 | fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]), |
| 75 | ntohl (mp->tunnel.table_id)); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 76 | |
| 77 | if (~0 == fib_index) |
| 78 | { |
| 79 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 80 | } |
| 81 | else |
| 82 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 83 | rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ? |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 84 | IPIP_TRANSPORT_IP6 : |
| 85 | IPIP_TRANSPORT_IP4), |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 86 | ntohl (mp->tunnel.instance), &src, &dst, |
| 87 | fib_index, mp->tunnel.tc_tos, &sw_if_index); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 88 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 89 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 90 | out: |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 91 | /* *INDENT-OFF* */ |
| 92 | REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 93 | ({ |
| 94 | rmp->sw_if_index = ntohl(sw_if_index); |
| 95 | })); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 96 | /* *INDENT-ON* */ |
| 97 | } |
| 98 | |
| 99 | static void |
| 100 | vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp) |
| 101 | { |
| 102 | vl_api_ipip_del_tunnel_reply_t *rmp; |
| 103 | |
| 104 | int rv = ipip_del_tunnel (ntohl (mp->sw_if_index)); |
| 105 | |
| 106 | REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY); |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | send_ipip_tunnel_details (ipip_tunnel_t * t, |
| 111 | vl_api_registration_t * reg, u32 context) |
| 112 | { |
| 113 | vl_api_ipip_tunnel_details_t *rmp; |
| 114 | bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false; |
| 115 | fib_table_t *ft; |
| 116 | |
| 117 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 118 | clib_memset (rmp, 0, sizeof (*rmp)); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 119 | rmp->_vl_msg_id = htons (VL_API_IPIP_TUNNEL_DETAILS); |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 120 | |
| 121 | ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src); |
| 122 | ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst); |
| 123 | |
| 124 | ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 : |
| 125 | FIB_PROTOCOL_IP4)); |
| 126 | |
| 127 | rmp->tunnel.table_id = htonl (ft->ft_table_id); |
| 128 | rmp->tunnel.instance = htonl (t->user_instance); |
| 129 | rmp->tunnel.sw_if_index = htonl (t->sw_if_index); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 130 | rmp->context = context; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 131 | |
| 132 | vl_api_send_msg (reg, (u8 *) rmp); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp) |
| 137 | { |
| 138 | vl_api_registration_t *reg; |
| 139 | ipip_main_t *gm = &ipip_main; |
| 140 | ipip_tunnel_t *t; |
| 141 | u32 sw_if_index; |
| 142 | |
| 143 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 144 | if (!reg) |
| 145 | return; |
| 146 | |
| 147 | sw_if_index = ntohl (mp->sw_if_index); |
| 148 | |
| 149 | if (sw_if_index == ~0) |
| 150 | { |
| 151 | /* *INDENT-OFF* */ |
| 152 | pool_foreach(t, gm->tunnels, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 153 | ({ |
| 154 | send_ipip_tunnel_details(t, reg, mp->context); |
| 155 | })); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 156 | /* *INDENT-ON* */ |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 161 | if (t) |
| 162 | send_ipip_tunnel_details (t, reg, mp->context); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp) |
| 168 | { |
| 169 | vl_api_ipip_6rd_add_tunnel_reply_t *rmp; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 170 | u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index; |
| 171 | int rv; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 172 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 173 | ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id)); |
| 174 | ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id)); |
| 175 | |
| 176 | if (~0 == ip4_fib_index || ~0 == ip6_fib_index) |
| 177 | |
| 178 | { |
| 179 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 180 | } |
| 181 | else |
| 182 | { |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 183 | rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 184 | mp->ip6_prefix.len, |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 185 | (ip4_address_t *) & mp->ip4_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 186 | mp->ip4_prefix.len, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 187 | (ip4_address_t *) & mp->ip4_src, |
| 188 | mp->security_check, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 189 | ip4_fib_index, ip6_fib_index, |
| 190 | &sixrd_tunnel_index); |
| 191 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 192 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 193 | /* *INDENT-OFF* */ |
| 194 | REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY, |
| 195 | ({ |
| 196 | rmp->sw_if_index = htonl (sixrd_tunnel_index); |
| 197 | })); |
| 198 | /* *INDENT-ON* */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static void |
| 202 | vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp) |
| 203 | { |
| 204 | vl_api_ipip_6rd_del_tunnel_reply_t *rmp; |
| 205 | |
| 206 | int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index)); |
| 207 | |
| 208 | REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * ipip_api_hookup |
| 213 | * Add vpe's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 214 | * vlib has already mapped shared memory and |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 215 | * added the client registration handlers. |
| 216 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 217 | */ |
| 218 | #define vl_msg_name_crc_list |
| 219 | #include <vnet/vnet_all_api_h.h> |
| 220 | #undef vl_msg_name_crc_list |
| 221 | |
| 222 | static void |
| 223 | setup_message_id_table (api_main_t * am) |
| 224 | { |
| 225 | #define _(id, n, crc) vl_msg_api_add_msg_name_crc(am, #n "_" #crc, id); |
| 226 | foreach_vl_msg_name_crc_ipip; |
| 227 | #undef _ |
| 228 | } |
| 229 | |
| 230 | static clib_error_t * |
| 231 | ipip_api_hookup (vlib_main_t * vm) |
| 232 | { |
| 233 | api_main_t *am = &api_main; |
| 234 | |
| 235 | #define _(N, n) \ |
| 236 | vl_msg_api_set_handlers(VL_API_##N, #n, vl_api_##n##_t_handler, \ |
| 237 | vl_noop_handler, vl_api_##n##_t_endian, \ |
| 238 | vl_api_##n##_t_print, sizeof(vl_api_##n##_t), 1); |
| 239 | foreach_vpe_api_msg; |
| 240 | #undef _ |
| 241 | |
| 242 | /* |
| 243 | * Set up the (msg_name, crc, message-id) table |
| 244 | */ |
| 245 | setup_message_id_table (am); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | VLIB_API_INIT_FUNCTION (ipip_api_hookup); |
| 251 | |
| 252 | /* |
| 253 | * fd.io coding-style-patch-verification: ON |
| 254 | * |
| 255 | * Local Variables: |
| 256 | * eval: (c-set-style "gnu") |
| 257 | * End: |
| 258 | */ |