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> |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 23 | #include <vnet/udp/udp.h> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 24 | |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 25 | #include <vnet/format_fns.h> |
| 26 | #include <vnet/udp/udp.api_enum.h> |
| 27 | #include <vnet/udp/udp.api_types.h> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 28 | |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 29 | #define REPLY_MSG_ID_BASE udp_main.msg_id_base |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 30 | #include <vlibapi/api_helper_macros.h> |
| 31 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 32 | static void |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 33 | send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg, |
| 34 | u32 context) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 35 | { |
| 36 | vl_api_udp_encap_details_t *mp; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 37 | |
| 38 | mp = vl_msg_api_alloc (sizeof (*mp)); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 39 | clib_memset (mp, 0, sizeof (*mp)); |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 40 | mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DETAILS); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 41 | mp->context = context; |
| 42 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 43 | if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto) |
| 44 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 45 | clib_memcpy (&mp->udp_encap.src_ip.un.ip4, |
| 46 | &ue->ue_hdrs.ip4.ue_ip4.src_address, 4); |
| 47 | clib_memcpy (&mp->udp_encap.dst_ip.un.ip4, |
| 48 | &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4); |
Jakub Grajciar | 7dd63e5 | 2020-03-19 08:03:55 +0100 | [diff] [blame] | 49 | mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP4); |
| 50 | mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP4); |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 51 | |
| 52 | /* ports aren't byte swapped because they are stored in network |
| 53 | * byte order */ |
| 54 | mp->udp_encap.src_port = ue->ue_hdrs.ip4.ue_udp.src_port; |
| 55 | 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] | 56 | } |
| 57 | else |
| 58 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 59 | clib_memcpy (&mp->udp_encap.src_ip.un.ip6, |
| 60 | &ue->ue_hdrs.ip6.ue_ip6.src_address, 16); |
| 61 | clib_memcpy (&mp->udp_encap.dst_ip.un.ip6, |
| 62 | &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16); |
Jakub Grajciar | 7dd63e5 | 2020-03-19 08:03:55 +0100 | [diff] [blame] | 63 | mp->udp_encap.dst_ip.af = ip_address_family_encode (AF_IP6); |
| 64 | mp->udp_encap.src_ip.af = ip_address_family_encode (AF_IP6); |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 65 | |
| 66 | /* ports aren't byte swapped because they are stored in network |
| 67 | * byte order */ |
| 68 | mp->udp_encap.src_port = ue->ue_hdrs.ip6.ue_udp.src_port; |
| 69 | 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] | 70 | } |
| 71 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 72 | mp->udp_encap.table_id = |
| 73 | 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] | 74 | mp->udp_encap.id = htonl (ue - udp_encap_pool); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 75 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 76 | vl_api_send_msg (reg, (u8 *) mp); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 80 | 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] | 81 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 82 | vl_api_registration_t *reg; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 83 | udp_encap_t *ue; |
| 84 | |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 85 | reg = vl_api_client_index_to_registration (mp->client_index); |
| 86 | if (!reg) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 87 | return; |
| 88 | |
| 89 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 90 | pool_foreach (ue, udp_encap_pool) |
| 91 | { |
Florin Coras | 6c4dae2 | 2018-01-09 06:39:23 -0800 | [diff] [blame] | 92 | send_udp_encap_details(ue, reg, mp->context); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 93 | } |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 94 | /* *INDENT-ON* */ |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 98 | 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] | 99 | { |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 100 | vl_api_udp_encap_add_reply_t *rmp; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 101 | ip46_address_t src_ip, dst_ip; |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 102 | udp_encap_fixup_flags_t flags; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 103 | u32 fib_index, table_id; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 104 | fib_protocol_t fproto; |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 105 | ip46_type_t itype; |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 106 | index_t uei; |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 107 | int rv = 0; |
| 108 | |
Neale Ranns | 4cabcff | 2018-09-25 00:30:23 -0700 | [diff] [blame] | 109 | uei = INDEX_INVALID; |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 110 | table_id = ntohl (mp->udp_encap.table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 111 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 112 | itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip); |
| 113 | itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip); |
| 114 | fproto = fib_proto_from_ip46 (itype); |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 115 | fib_index = fib_table_find (fproto, table_id); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 116 | |
| 117 | if (~0 == fib_index) |
| 118 | { |
| 119 | rv = VNET_API_ERROR_NO_SUCH_TABLE; |
| 120 | goto done; |
| 121 | } |
| 122 | |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 123 | flags = UDP_ENCAP_FIXUP_NONE; |
| 124 | if (mp->udp_encap.src_port == 0) |
| 125 | flags |= UDP_ENCAP_FIXUP_UDP_SRC_PORT_ENTROPY; |
| 126 | |
| 127 | uei = udp_encap_add_and_lock (fproto, fib_index, &src_ip, &dst_ip, |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 128 | ntohs (mp->udp_encap.src_port), |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 129 | ntohs (mp->udp_encap.dst_port), flags); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 130 | |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 131 | done: |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 132 | /* *INDENT-OFF* */ |
| 133 | REPLY_MACRO2 (VL_API_UDP_ENCAP_ADD_REPLY, |
| 134 | ({ |
| 135 | rmp->id = ntohl (uei); |
| 136 | })); |
| 137 | /* *INDENT-ON* */ |
| 138 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 142 | 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] | 143 | { |
| 144 | vl_api_udp_encap_del_reply_t *rmp; |
| 145 | int rv = 0; |
| 146 | |
| 147 | udp_encap_unlock (ntohl (mp->id)); |
| 148 | |
| 149 | REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Arthur de Kerhor | 8a6f5d3 | 2021-05-20 11:48:00 +0200 | [diff] [blame] | 152 | u32 |
| 153 | udp_api_decap_proto_to_index (vlib_main_t *vm, |
| 154 | vl_api_udp_decap_next_proto_t iproto) |
| 155 | { |
| 156 | switch (iproto) |
| 157 | { |
| 158 | case UDP_API_DECAP_PROTO_IP4: |
| 159 | return vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index; |
| 160 | case UDP_API_DECAP_PROTO_IP6: |
| 161 | return vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index; |
| 162 | case UDP_API_DECAP_PROTO_MPLS: |
| 163 | return vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index; |
| 164 | } |
| 165 | return ~0; |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | vl_api_udp_decap_add_del_t_handler (vl_api_udp_decap_add_del_t *mp) |
| 170 | { |
| 171 | vl_api_udp_decap_add_del_reply_t *rmp; |
| 172 | vlib_main_t *vm = vlib_get_main (); |
| 173 | int rv = 0; |
| 174 | |
| 175 | if (mp->is_add) |
| 176 | { |
| 177 | u32 node_index = |
| 178 | udp_api_decap_proto_to_index (vm, ntohl (mp->udp_decap.next_proto)); |
| 179 | if (node_index == ~0) |
| 180 | rv = VNET_API_ERROR_INVALID_PROTOCOL; |
| 181 | else |
| 182 | udp_register_dst_port (vm, ntohs (mp->udp_decap.port), node_index, |
| 183 | mp->udp_decap.is_ip4); |
| 184 | } |
| 185 | else |
| 186 | udp_unregister_dst_port (vm, ntohs (mp->udp_decap.port), |
| 187 | mp->udp_decap.is_ip4); |
| 188 | REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY); |
| 189 | } |
| 190 | |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 191 | #include <vnet/udp/udp.api.c> |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 192 | static clib_error_t * |
| 193 | udp_api_hookup (vlib_main_t * vm) |
| 194 | { |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 195 | api_main_t *am = vlibapi_get_main (); |
| 196 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 197 | /* |
| 198 | * Set up the (msg_name, crc, message-id) table |
| 199 | */ |
Filip Tehlar | 3006289 | 2021-06-21 13:01:24 +0000 | [diff] [blame] | 200 | REPLY_MSG_ID_BASE = setup_message_id_table (); |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 201 | |
Vladislav Grishenko | 5c801b3 | 2022-06-23 00:45:16 +0500 | [diff] [blame] | 202 | /* Mark these APIs as mp safe */ |
| 203 | vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_ADD, 1); |
| 204 | vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DEL, 1); |
| 205 | vl_api_set_msg_thread_safe (am, REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DUMP, |
| 206 | 1); |
| 207 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | VLIB_API_INIT_FUNCTION (udp_api_hookup); |
| 212 | |
| 213 | /* |
| 214 | * fd.io coding-style-patch-verification: ON |
| 215 | * |
| 216 | * Local Variables: |
| 217 | * eval: (c-set-style "gnu") |
| 218 | * End: |
| 219 | */ |