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> |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 24 | #include <vnet/ip/ip_types_api.h> |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 25 | #include <vnet/tunnel/tunnel_types_api.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 26 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 27 | #include <vnet/ipip/ipip.api_enum.h> |
| 28 | #include <vnet/ipip/ipip.api_types.h> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 29 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 30 | #define REPLY_MSG_ID_BASE im->msg_id_base |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 31 | #include <vlibapi/api_helper_macros.h> |
| 32 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 33 | static void |
| 34 | vl_api_ipip_add_tunnel_t_handler (vl_api_ipip_add_tunnel_t * mp) |
| 35 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 36 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 37 | vl_api_ipip_add_tunnel_reply_t *rmp; |
| 38 | int rv = 0; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 39 | u32 fib_index, sw_if_index = ~0; |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 40 | tunnel_encap_decap_flags_t flags; |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 41 | ip46_address_t src, dst; |
| 42 | ip46_type_t itype[2]; |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 43 | tunnel_mode_t mode; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 44 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 45 | 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 Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 49 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 50 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
| 51 | goto out; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 54 | if (ip46_address_is_equal (&src, &dst)) |
| 55 | { |
| 56 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 57 | goto out; |
| 58 | } |
| 59 | |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 60 | rv = tunnel_encap_decap_flags_decode (mp->tunnel.flags, &flags); |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 61 | |
| 62 | if (rv) |
| 63 | goto out; |
| 64 | |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 65 | rv = tunnel_mode_decode (mp->tunnel.mode, &mode); |
| 66 | |
| 67 | if (rv) |
| 68 | goto out; |
| 69 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 70 | fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]), |
| 71 | ntohl (mp->tunnel.table_id)); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 72 | |
| 73 | if (~0 == fib_index) |
| 74 | { |
| 75 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 76 | } |
| 77 | else |
| 78 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 79 | rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ? |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 80 | IPIP_TRANSPORT_IP6 : |
| 81 | IPIP_TRANSPORT_IP4), |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 82 | ntohl (mp->tunnel.instance), &src, &dst, |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 83 | fib_index, flags, |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 84 | ip_dscp_decode (mp->tunnel.dscp), mode, |
| 85 | &sw_if_index); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 86 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 87 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 88 | out: |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 89 | /* *INDENT-OFF* */ |
| 90 | REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 91 | ({ |
| 92 | rmp->sw_if_index = ntohl(sw_if_index); |
| 93 | })); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 94 | /* *INDENT-ON* */ |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp) |
| 99 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 100 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 101 | vl_api_ipip_del_tunnel_reply_t *rmp; |
| 102 | |
| 103 | int rv = ipip_del_tunnel (ntohl (mp->sw_if_index)); |
| 104 | |
| 105 | REPLY_MACRO (VL_API_IPIP_DEL_TUNNEL_REPLY); |
| 106 | } |
| 107 | |
Matthew Smith | e870d5b | 2023-07-14 16:05:39 +0000 | [diff] [blame] | 108 | static vl_api_tunnel_mode_t |
| 109 | ipip_tunnel_mode_encode (ipip_mode_t mode) |
| 110 | { |
| 111 | switch (mode) |
| 112 | { |
| 113 | case IPIP_MODE_P2P: |
| 114 | return TUNNEL_API_MODE_P2P; |
| 115 | case IPIP_MODE_P2MP: |
| 116 | return TUNNEL_API_MODE_MP; |
| 117 | case IPIP_MODE_6RD: |
| 118 | return TUNNEL_API_MODE_P2P; |
| 119 | default: |
| 120 | return TUNNEL_API_MODE_P2P; |
| 121 | } |
| 122 | } |
| 123 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 124 | static void |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 125 | send_ipip_tunnel_details (ipip_tunnel_t * t, vl_api_ipip_tunnel_dump_t * mp) |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 126 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 127 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 128 | vl_api_ipip_tunnel_details_t *rmp; |
| 129 | bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false; |
| 130 | fib_table_t *ft; |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 131 | |
Matthew Smith | e870d5b | 2023-07-14 16:05:39 +0000 | [diff] [blame] | 132 | ft = fib_table_get (t->fib_index, |
| 133 | (is_ipv6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4)); |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 134 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 135 | /* *INDENT-OFF* */ |
| 136 | REPLY_MACRO_DETAILS2(VL_API_IPIP_TUNNEL_DETAILS, |
| 137 | ({ |
| 138 | ip_address_encode (&t->tunnel_src, IP46_TYPE_ANY, &rmp->tunnel.src); |
| 139 | ip_address_encode (&t->tunnel_dst, IP46_TYPE_ANY, &rmp->tunnel.dst); |
| 140 | rmp->tunnel.table_id = htonl (ft->ft_table_id); |
| 141 | rmp->tunnel.instance = htonl (t->user_instance); |
| 142 | rmp->tunnel.sw_if_index = htonl (t->sw_if_index); |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 143 | rmp->tunnel.dscp = ip_dscp_encode(t->dscp); |
Neale Ranns | 59ff918 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 144 | rmp->tunnel.flags = tunnel_encap_decap_flags_encode(t->flags); |
Matthew Smith | e870d5b | 2023-07-14 16:05:39 +0000 | [diff] [blame] | 145 | rmp->tunnel.mode = ipip_tunnel_mode_encode (t->mode); |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 146 | })); |
| 147 | /* *INDENT-ON* */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static void |
| 151 | vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp) |
| 152 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 153 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 154 | ipip_tunnel_t *t; |
| 155 | u32 sw_if_index; |
| 156 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 157 | sw_if_index = ntohl (mp->sw_if_index); |
| 158 | |
| 159 | if (sw_if_index == ~0) |
| 160 | { |
| 161 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 162 | pool_foreach (t, im->tunnels) |
| 163 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 164 | send_ipip_tunnel_details(t, mp); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 165 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 166 | /* *INDENT-ON* */ |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 171 | if (t) |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 172 | send_ipip_tunnel_details (t, mp); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp) |
| 178 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 179 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 180 | vl_api_ipip_6rd_add_tunnel_reply_t *rmp; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 181 | u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index; |
| 182 | int rv; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 183 | |
Neale Ranns | d057625 | 2020-02-20 13:16:49 +0000 | [diff] [blame] | 184 | sixrd_tunnel_index = ~0; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 185 | ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->ip4_table_id)); |
| 186 | ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, ntohl (mp->ip6_table_id)); |
| 187 | |
| 188 | if (~0 == ip4_fib_index || ~0 == ip6_fib_index) |
| 189 | |
| 190 | { |
| 191 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 192 | } |
| 193 | else |
| 194 | { |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 195 | rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 196 | mp->ip6_prefix.len, |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 197 | (ip4_address_t *) & mp->ip4_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 198 | mp->ip4_prefix.len, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 199 | (ip4_address_t *) & mp->ip4_src, |
| 200 | mp->security_check, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 201 | ip4_fib_index, ip6_fib_index, |
| 202 | &sixrd_tunnel_index); |
| 203 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 204 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 205 | /* *INDENT-OFF* */ |
| 206 | REPLY_MACRO2 (VL_API_IPIP_6RD_ADD_TUNNEL_REPLY, |
| 207 | ({ |
| 208 | rmp->sw_if_index = htonl (sixrd_tunnel_index); |
| 209 | })); |
| 210 | /* *INDENT-ON* */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void |
| 214 | vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp) |
| 215 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 216 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 217 | vl_api_ipip_6rd_del_tunnel_reply_t *rmp; |
| 218 | |
| 219 | int rv = sixrd_del_tunnel (ntohl (mp->sw_if_index)); |
| 220 | |
| 221 | REPLY_MACRO (VL_API_IPIP_6RD_DEL_TUNNEL_REPLY); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * ipip_api_hookup |
| 226 | * Add vpe's API message handlers to the table. |
Jim Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 227 | * vlib has already mapped shared memory and |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 228 | * added the client registration handlers. |
| 229 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 230 | */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 231 | /* API definitions */ |
| 232 | #include <vnet/format_fns.h> |
| 233 | #include <vnet/ipip/ipip.api.c> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 234 | |
| 235 | static clib_error_t * |
| 236 | ipip_api_hookup (vlib_main_t * vm) |
| 237 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 238 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 239 | |
| 240 | /* |
| 241 | * Set up the (msg_name, crc, message-id) table |
| 242 | */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 243 | im->msg_id_base = setup_message_id_table (); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | VLIB_API_INIT_FUNCTION (ipip_api_hookup); |
| 249 | |
| 250 | /* |
| 251 | * fd.io coding-style-patch-verification: ON |
| 252 | * |
| 253 | * Local Variables: |
| 254 | * eval: (c-set-style "gnu") |
| 255 | * End: |
| 256 | */ |