Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 Cisco and/or its affiliates. |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 19 | #include <vnet/udp/udp_local.h> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 20 | #include <vnet/udp/udp_encap.h> |
| 21 | #include <vnet/fib/fib_table.h> |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 22 | #include <vnet/ip/ip_types_api.h> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 23 | |
| 24 | #include <vnet/vnet_msg_enum.h> |
| 25 | |
| 26 | #define vl_typedefs /* define message structures */ |
| 27 | #include <vnet/vnet_all_api_h.h> |
| 28 | #undef vl_typedefs |
| 29 | |
| 30 | #define vl_endianfun /* define message structures */ |
| 31 | #include <vnet/vnet_all_api_h.h> |
| 32 | #undef vl_endianfun |
| 33 | |
| 34 | /* instantiate all the print functions we know about */ |
| 35 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 36 | #define vl_printfun |
| 37 | #include <vnet/vnet_all_api_h.h> |
| 38 | #undef vl_printfun |
| 39 | |
| 40 | #include <vlibapi/api_helper_macros.h> |
| 41 | |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 42 | #define foreach_udp_api_msg \ |
| 43 | _ (UDP_ENCAP_DEL, udp_encap_del) \ |
| 44 | _ (UDP_ENCAP_ADD, udp_encap_add) \ |
| 45 | _ (UDP_ENCAP_DUMP, udp_encap_dump) \ |
| 46 | _ (UDP_DECAP_ADD_DEL, udp_decap_add_del) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 47 | |
| 48 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 49 | send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg, |
| 50 | u32 context) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 51 | { |
| 52 | vl_api_udp_encap_details_t *mp; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 53 | |
| 54 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 55 | clib_memset (mp, 0, sizeof (*mp)); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 56 | mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS); |
| 57 | mp->context = context; |
| 58 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 59 | if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto) |
| 60 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 61 | clib_memcpy (&mp->udp_encap.src_ip.un.ip4, |
| 62 | &ue->ue_hdrs.ip4.ue_ip4.src_address, 4); |
| 63 | clib_memcpy (&mp->udp_encap.dst_ip.un.ip4, |
| 64 | &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4); |
Jakub Grajciar | 7dd63e5 | 2020-03-19 08:03:55 +0100 | [diff] [blame] | 65 | mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP4); |
| 66 | mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP4); |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 67 | |
| 68 | /* ports aren't byte swapped because they are stored in network |
| 69 | * byte order */ |
| 70 | mp->udp_encap.src_port = ue->ue_hdrs.ip4.ue_udp.src_port; |
| 71 | mp->udp_encap.dst_port = ue->ue_hdrs.ip4.ue_udp.dst_port; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 72 | } |
| 73 | else |
| 74 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 75 | clib_memcpy (&mp->udp_encap.src_ip.un.ip6, |
| 76 | &ue->ue_hdrs.ip6.ue_ip6.src_address, 16); |
| 77 | clib_memcpy (&mp->udp_encap.dst_ip.un.ip6, |
| 78 | &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16); |
Jakub Grajciar | 7dd63e5 | 2020-03-19 08:03:55 +0100 | [diff] [blame] | 79 | mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP6); |
| 80 | mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP6); |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 81 | |
| 82 | /* ports aren't byte swapped because they are stored in network |
| 83 | * byte order */ |
| 84 | mp->udp_encap.src_port = ue->ue_hdrs.ip6.ue_udp.src_port; |
| 85 | mp->udp_encap.dst_port = ue->ue_hdrs.ip6.ue_udp.dst_port; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 88 | mp->udp_encap.table_id = |
| 89 | htonl (fib_table_get_table_id (ue->ue_fib_index, ue->ue_ip_proto)); |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 90 | mp->udp_encap.id = htonl (ue - udp_encap_pool); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 91 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 92 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 96 | vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t *mp) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 97 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 98 | vl_api_registration_t *reg; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 99 | udp_encap_t *ue; |
| 100 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 101 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 102 | if (!reg) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 103 | return; |
| 104 | |
| 105 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 106 | pool_foreach (ue, udp_encap_pool) |
| 107 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 108 | send_udp_encap_details(ue, reg, mp->context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 109 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 110 | /* *INDENT-ON* */ |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 114 | vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t *mp) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 115 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 116 | vl_api_udp_encap_add_reply_t *rmp; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 117 | ip46_address_t src_ip, dst_ip; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 118 | u32 fib_index, table_id; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 119 | fib_protocol_t fproto; |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 120 | ip46_type_t itype; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 121 | index_t uei; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 122 | int rv = 0; |
| 123 | |
Neale Ranns | 4cabcff | 2018-09-25 00:30:23 -0700 | [diff] [blame] | 124 | uei = INDEX_INVALID; |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 125 | table_id = ntohl (mp->udp_encap.table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 126 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 127 | itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip); |
| 128 | itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip); |
| 129 | fproto = fib_proto_from_ip46 (itype); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 130 | fib_index = fib_table_find (fproto, table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 131 | |
| 132 | if (~0 == fib_index) |
| 133 | { |
| 134 | rv = VNET_API_ERROR_NO_SUCH_TABLE; |
| 135 | goto done; |
| 136 | } |
| 137 | |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 138 | uei = udp_encap_add_and_lock (fproto, fib_index, |
| 139 | &src_ip, &dst_ip, |
| 140 | ntohs (mp->udp_encap.src_port), |
| 141 | ntohs (mp->udp_encap.dst_port), |
| 142 | UDP_ENCAP_FIXUP_NONE); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 143 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 144 | done: |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 145 | /* *INDENT-OFF* */ |
| 146 | REPLY_MACRO2 (VL_API_UDP_ENCAP_ADD_REPLY, |
| 147 | ({ |
| 148 | rmp->id = ntohl (uei); |
| 149 | })); |
| 150 | /* *INDENT-ON* */ |
| 151 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 155 | vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t *mp) |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 156 | { |
| 157 | vl_api_udp_encap_del_reply_t *rmp; |
| 158 | int rv = 0; |
| 159 | |
| 160 | udp_encap_unlock (ntohl (mp->id)); |
| 161 | |
| 162 | REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 165 | u32 |
| 166 | udp_api_decap_proto_to_index (vlib_main_t *vm, |
| 167 | vl_api_udp_decap_next_proto_t iproto) |
| 168 | { |
| 169 | switch (iproto) |
| 170 | { |
| 171 | case UDP_API_DECAP_PROTO_IP4: |
| 172 | return vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index; |
| 173 | case UDP_API_DECAP_PROTO_IP6: |
| 174 | return vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index; |
| 175 | case UDP_API_DECAP_PROTO_MPLS: |
| 176 | return vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index; |
| 177 | } |
| 178 | return ~0; |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | vl_api_udp_decap_add_del_t_handler (vl_api_udp_decap_add_del_t *mp) |
| 183 | { |
| 184 | vl_api_udp_decap_add_del_reply_t *rmp; |
| 185 | vlib_main_t *vm = vlib_get_main (); |
| 186 | int rv = 0; |
| 187 | |
| 188 | if (mp->is_add) |
| 189 | { |
| 190 | u32 node_index = |
| 191 | udp_api_decap_proto_to_index (vm, ntohl (mp->udp_decap.next_proto)); |
| 192 | if (node_index == ~0) |
| 193 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
| 194 | else |
| 195 | udp_register_dst_port (vm, ntohs (mp->udp_decap.port), node_index, |
| 196 | mp->udp_decap.is_ip4); |
| 197 | } |
| 198 | else |
| 199 | udp_unregister_dst_port (vm, ntohs (mp->udp_decap.port), |
| 200 | mp->udp_decap.is_ip4); |
| 201 | REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY); |
| 202 | } |
| 203 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 204 | #define vl_msg_name_crc_list |
| 205 | #include <vnet/udp/udp.api.h> |
| 206 | #undef vl_msg_name_crc_list |
| 207 | |
| 208 | static void |
| 209 | setup_message_id_table (api_main_t * am) |
| 210 | { |
| 211 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 212 | foreach_vl_msg_name_crc_udp; |
| 213 | #undef _ |
| 214 | } |
| 215 | |
| 216 | static clib_error_t * |
| 217 | udp_api_hookup (vlib_main_t * vm) |
| 218 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 219 | api_main_t *am = vlibapi_get_main (); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 220 | |
| 221 | #define _(N,n) \ |
| 222 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 223 | vl_api_##n##_t_handler, \ |
| 224 | vl_noop_handler, \ |
| 225 | vl_api_##n##_t_endian, \ |
| 226 | vl_api_##n##_t_print, \ |
| 227 | sizeof(vl_api_##n##_t), 1); |
| 228 | foreach_udp_api_msg; |
| 229 | #undef _ |
| 230 | |
| 231 | /* |
| 232 | * Set up the (msg_name, crc, message-id) table |
| 233 | */ |
| 234 | setup_message_id_table (am); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | VLIB_API_INIT_FUNCTION (udp_api_hookup); |
| 240 | |
| 241 | /* |
| 242 | * fd.io coding-style-patch-verification: ON |
| 243 | * |
| 244 | * Local Variables: |
| 245 | * eval: (c-set-style "gnu") |
| 246 | * End: |
| 247 | */ |