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 | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 25 | #include <vnet/ipip/ipip_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 | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 40 | ipip_tunnel_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]; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 43 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 44 | 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 Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 48 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 49 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
| 50 | goto out; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 53 | if (ip46_address_is_equal (&src, &dst)) |
| 54 | { |
| 55 | rv = VNET_API_ERROR_SAME_SRC_DST; |
| 56 | goto out; |
| 57 | } |
| 58 | |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 59 | rv = ipip_tunnel_flags_decode (mp->tunnel.flags, &flags); |
| 60 | |
| 61 | if (rv) |
| 62 | goto out; |
| 63 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 64 | fib_index = fib_table_find (fib_proto_from_ip46 (itype[0]), |
| 65 | ntohl (mp->tunnel.table_id)); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 66 | |
| 67 | if (~0 == fib_index) |
| 68 | { |
| 69 | rv = VNET_API_ERROR_NO_SUCH_FIB; |
| 70 | } |
| 71 | else |
| 72 | { |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 73 | rv = ipip_add_tunnel ((itype[0] == IP46_TYPE_IP6 ? |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 74 | IPIP_TRANSPORT_IP6 : |
| 75 | IPIP_TRANSPORT_IP4), |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 76 | ntohl (mp->tunnel.instance), &src, &dst, |
Neale Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 77 | fib_index, flags, |
| 78 | ip_dscp_decode (mp->tunnel.dscp), &sw_if_index); |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 79 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 80 | |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 81 | out: |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 82 | /* *INDENT-OFF* */ |
| 83 | REPLY_MACRO2(VL_API_IPIP_ADD_TUNNEL_REPLY, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 84 | ({ |
| 85 | rmp->sw_if_index = ntohl(sw_if_index); |
| 86 | })); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 87 | /* *INDENT-ON* */ |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | vl_api_ipip_del_tunnel_t_handler (vl_api_ipip_del_tunnel_t * mp) |
| 92 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 93 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 94 | 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 | |
| 101 | static void |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 102 | 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] | 103 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 104 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 105 | vl_api_ipip_tunnel_details_t *rmp; |
| 106 | bool is_ipv6 = t->transport == IPIP_TRANSPORT_IP6 ? true : false; |
| 107 | fib_table_t *ft; |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 108 | int rv = 0; |
Neale Ranns | cbd0824 | 2019-05-26 11:34:27 -0700 | [diff] [blame] | 109 | |
| 110 | ft = fib_table_get (t->fib_index, (is_ipv6 ? FIB_PROTOCOL_IP6 : |
| 111 | FIB_PROTOCOL_IP4)); |
| 112 | |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 113 | /* *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 Ranns | 9534696 | 2019-11-25 13:04:44 +0000 | [diff] [blame] | 121 | rmp->tunnel.dscp = ip_dscp_encode(t->dscp); |
| 122 | rmp->tunnel.flags = ipip_tunnel_flags_encode(t->flags); |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 123 | })); |
| 124 | /* *INDENT-ON* */ |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void |
| 128 | vl_api_ipip_tunnel_dump_t_handler (vl_api_ipip_tunnel_dump_t * mp) |
| 129 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 130 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 131 | ipip_tunnel_t *t; |
| 132 | u32 sw_if_index; |
| 133 | |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 134 | sw_if_index = ntohl (mp->sw_if_index); |
| 135 | |
| 136 | if (sw_if_index == ~0) |
| 137 | { |
| 138 | /* *INDENT-OFF* */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 139 | pool_foreach(t, im->tunnels, |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 140 | ({ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 141 | send_ipip_tunnel_details(t, mp); |
Neale Ranns | c87b66c | 2019-02-07 07:26:12 -0800 | [diff] [blame] | 142 | })); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 143 | /* *INDENT-ON* */ |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | t = ipip_tunnel_db_find_by_sw_if_index (sw_if_index); |
| 148 | if (t) |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 149 | send_ipip_tunnel_details (t, mp); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | vl_api_ipip_6rd_add_tunnel_t_handler (vl_api_ipip_6rd_add_tunnel_t * mp) |
| 155 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 156 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 157 | vl_api_ipip_6rd_add_tunnel_reply_t *rmp; |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 158 | u32 sixrd_tunnel_index, ip4_fib_index, ip6_fib_index; |
| 159 | int rv; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 160 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 161 | 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 Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 171 | rv = sixrd_add_tunnel ((ip6_address_t *) & mp->ip6_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 172 | mp->ip6_prefix.len, |
Paul Vinciguerra | ab05508 | 2019-06-06 14:07:55 -0400 | [diff] [blame] | 173 | (ip4_address_t *) & mp->ip4_prefix.address, |
Ole Troan | 288e093 | 2019-05-29 12:30:05 +0200 | [diff] [blame] | 174 | mp->ip4_prefix.len, |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 175 | (ip4_address_t *) & mp->ip4_src, |
| 176 | mp->security_check, |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 177 | ip4_fib_index, ip6_fib_index, |
| 178 | &sixrd_tunnel_index); |
| 179 | } |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 180 | |
Neale Ranns | 6150211 | 2018-08-22 00:21:14 -0700 | [diff] [blame] | 181 | /* *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 Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static void |
| 190 | vl_api_ipip_6rd_del_tunnel_t_handler (vl_api_ipip_6rd_del_tunnel_t * mp) |
| 191 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 192 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 193 | 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 Thompson | f324dec | 2019-04-08 03:22:21 -0500 | [diff] [blame] | 203 | * vlib has already mapped shared memory and |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 204 | * added the client registration handlers. |
| 205 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 206 | */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 207 | /* API definitions */ |
| 208 | #include <vnet/format_fns.h> |
| 209 | #include <vnet/ipip/ipip.api.c> |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 210 | |
| 211 | static clib_error_t * |
| 212 | ipip_api_hookup (vlib_main_t * vm) |
| 213 | { |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 214 | ipip_main_t *im = &ipip_main; |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * Set up the (msg_name, crc, message-id) table |
| 218 | */ |
Ole Troan | 2a1ca78 | 2019-09-19 01:08:30 +0200 | [diff] [blame] | 219 | im->msg_id_base = setup_message_id_table (); |
Ole Troan | 298c695 | 2018-03-08 12:30:43 +0100 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | VLIB_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 | */ |