blob: 0f2d014946f56273af709ba770ca0e9071b034be [file] [log] [blame]
Neale Ranns810086d2017-11-05 16:26:46 -08001/*
Florin Corasc5df8c72019-04-08 07:42:30 -07002 * Copyright (c) 2018-2019 Cisco and/or its affiliates.
Neale Ranns810086d2017-11-05 16:26:46 -08003 * 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 Kerhor8a6f5d32021-05-20 11:48:00 +020019#include <vnet/udp/udp_local.h>
Neale Ranns810086d2017-11-05 16:26:46 -080020#include <vnet/udp/udp_encap.h>
21#include <vnet/fib/fib_table.h>
Neale Rannsd0df49f2018-08-08 01:06:40 -070022#include <vnet/ip/ip_types_api.h>
Filip Tehlar30062892021-06-21 13:01:24 +000023#include <vnet/udp/udp.h>
Neale Ranns810086d2017-11-05 16:26:46 -080024
Filip Tehlar30062892021-06-21 13:01:24 +000025#include <vnet/format_fns.h>
26#include <vnet/udp/udp.api_enum.h>
27#include <vnet/udp/udp.api_types.h>
Neale Ranns810086d2017-11-05 16:26:46 -080028
Filip Tehlar30062892021-06-21 13:01:24 +000029#define REPLY_MSG_ID_BASE udp_main.msg_id_base
Neale Ranns810086d2017-11-05 16:26:46 -080030#include <vlibapi/api_helper_macros.h>
31
Neale Ranns810086d2017-11-05 16:26:46 -080032static void
Florin Coras6c4dae22018-01-09 06:39:23 -080033send_udp_encap_details (const udp_encap_t * ue, vl_api_registration_t * reg,
34 u32 context)
Neale Ranns810086d2017-11-05 16:26:46 -080035{
36 vl_api_udp_encap_details_t *mp;
Neale Ranns810086d2017-11-05 16:26:46 -080037
38 mp = vl_msg_api_alloc (sizeof (*mp));
Dave Barachb7b92992018-10-17 10:38:51 -040039 clib_memset (mp, 0, sizeof (*mp));
Filip Tehlar30062892021-06-21 13:01:24 +000040 mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_UDP_ENCAP_DETAILS);
Neale Ranns810086d2017-11-05 16:26:46 -080041 mp->context = context;
42
Neale Ranns810086d2017-11-05 16:26:46 -080043 if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
44 {
Neale Rannsd0df49f2018-08-08 01:06:40 -070045 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 Grajciar7dd63e52020-03-19 08:03:55 +010049 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 Rannsd0df49f2018-08-08 01:06:40 -070051
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 Ranns810086d2017-11-05 16:26:46 -080056 }
57 else
58 {
Neale Rannsd0df49f2018-08-08 01:06:40 -070059 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 Grajciar7dd63e52020-03-19 08:03:55 +010063 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 Rannsd0df49f2018-08-08 01:06:40 -070065
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 Ranns810086d2017-11-05 16:26:46 -080070 }
71
Neale Rannsd0df49f2018-08-08 01:06:40 -070072 mp->udp_encap.table_id =
73 htonl (fib_table_get_table_id (ue->ue_fib_index, ue->ue_ip_proto));
Neale Ranns9c0a3c42018-09-07 08:57:41 -070074 mp->udp_encap.id = htonl (ue - udp_encap_pool);
Neale Ranns810086d2017-11-05 16:26:46 -080075
Florin Coras6c4dae22018-01-09 06:39:23 -080076 vl_api_send_msg (reg, (u8 *) mp);
Neale Ranns810086d2017-11-05 16:26:46 -080077}
78
79static void
Arthur de Kerhor8a6f5d32021-05-20 11:48:00 +020080vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t *mp)
Neale Ranns810086d2017-11-05 16:26:46 -080081{
Florin Coras6c4dae22018-01-09 06:39:23 -080082 vl_api_registration_t *reg;
Neale Ranns810086d2017-11-05 16:26:46 -080083 udp_encap_t *ue;
84
Florin Coras6c4dae22018-01-09 06:39:23 -080085 reg = vl_api_client_index_to_registration (mp->client_index);
86 if (!reg)
Neale Ranns810086d2017-11-05 16:26:46 -080087 return;
88
89 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +010090 pool_foreach (ue, udp_encap_pool)
91 {
Florin Coras6c4dae22018-01-09 06:39:23 -080092 send_udp_encap_details(ue, reg, mp->context);
Damjan Marionb2c31b62020-12-13 21:47:40 +010093 }
Florin Corase86a8ed2018-01-05 03:20:25 -080094 /* *INDENT-ON* */
Neale Ranns810086d2017-11-05 16:26:46 -080095}
96
97static void
Arthur de Kerhor8a6f5d32021-05-20 11:48:00 +020098vl_api_udp_encap_add_t_handler (vl_api_udp_encap_add_t *mp)
Neale Ranns810086d2017-11-05 16:26:46 -080099{
Neale Rannsd0df49f2018-08-08 01:06:40 -0700100 vl_api_udp_encap_add_reply_t *rmp;
Neale Ranns810086d2017-11-05 16:26:46 -0800101 ip46_address_t src_ip, dst_ip;
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700102 u32 fib_index, table_id;
Neale Ranns810086d2017-11-05 16:26:46 -0800103 fib_protocol_t fproto;
Neale Rannsd0df49f2018-08-08 01:06:40 -0700104 ip46_type_t itype;
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700105 index_t uei;
Neale Ranns810086d2017-11-05 16:26:46 -0800106 int rv = 0;
107
Neale Ranns4cabcff2018-09-25 00:30:23 -0700108 uei = INDEX_INVALID;
Neale Rannsd0df49f2018-08-08 01:06:40 -0700109 table_id = ntohl (mp->udp_encap.table_id);
Neale Ranns810086d2017-11-05 16:26:46 -0800110
Neale Rannsd0df49f2018-08-08 01:06:40 -0700111 itype = ip_address_decode (&mp->udp_encap.src_ip, &src_ip);
112 itype = ip_address_decode (&mp->udp_encap.dst_ip, &dst_ip);
113 fproto = fib_proto_from_ip46 (itype);
Florin Corase86a8ed2018-01-05 03:20:25 -0800114 fib_index = fib_table_find (fproto, table_id);
Neale Ranns810086d2017-11-05 16:26:46 -0800115
116 if (~0 == fib_index)
117 {
118 rv = VNET_API_ERROR_NO_SUCH_TABLE;
119 goto done;
120 }
121
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700122 uei = udp_encap_add_and_lock (fproto, fib_index,
123 &src_ip, &dst_ip,
124 ntohs (mp->udp_encap.src_port),
125 ntohs (mp->udp_encap.dst_port),
126 UDP_ENCAP_FIXUP_NONE);
Neale Ranns810086d2017-11-05 16:26:46 -0800127
Florin Corase86a8ed2018-01-05 03:20:25 -0800128done:
Neale Ranns9c0a3c42018-09-07 08:57:41 -0700129 /* *INDENT-OFF* */
130 REPLY_MACRO2 (VL_API_UDP_ENCAP_ADD_REPLY,
131 ({
132 rmp->id = ntohl (uei);
133 }));
134 /* *INDENT-ON* */
135
Neale Rannsd0df49f2018-08-08 01:06:40 -0700136}
137
138static void
Arthur de Kerhor8a6f5d32021-05-20 11:48:00 +0200139vl_api_udp_encap_del_t_handler (vl_api_udp_encap_del_t *mp)
Neale Rannsd0df49f2018-08-08 01:06:40 -0700140{
141 vl_api_udp_encap_del_reply_t *rmp;
142 int rv = 0;
143
144 udp_encap_unlock (ntohl (mp->id));
145
146 REPLY_MACRO (VL_API_UDP_ENCAP_DEL_REPLY);
Neale Ranns810086d2017-11-05 16:26:46 -0800147}
148
Arthur de Kerhor8a6f5d32021-05-20 11:48:00 +0200149u32
150udp_api_decap_proto_to_index (vlib_main_t *vm,
151 vl_api_udp_decap_next_proto_t iproto)
152{
153 switch (iproto)
154 {
155 case UDP_API_DECAP_PROTO_IP4:
156 return vlib_get_node_by_name (vm, (u8 *) "ip4-input")->index;
157 case UDP_API_DECAP_PROTO_IP6:
158 return vlib_get_node_by_name (vm, (u8 *) "ip6-input")->index;
159 case UDP_API_DECAP_PROTO_MPLS:
160 return vlib_get_node_by_name (vm, (u8 *) "mpls-input")->index;
161 }
162 return ~0;
163}
164
165static void
166vl_api_udp_decap_add_del_t_handler (vl_api_udp_decap_add_del_t *mp)
167{
168 vl_api_udp_decap_add_del_reply_t *rmp;
169 vlib_main_t *vm = vlib_get_main ();
170 int rv = 0;
171
172 if (mp->is_add)
173 {
174 u32 node_index =
175 udp_api_decap_proto_to_index (vm, ntohl (mp->udp_decap.next_proto));
176 if (node_index == ~0)
177 rv = VNET_API_ERROR_INVALID_PROTOCOL;
178 else
179 udp_register_dst_port (vm, ntohs (mp->udp_decap.port), node_index,
180 mp->udp_decap.is_ip4);
181 }
182 else
183 udp_unregister_dst_port (vm, ntohs (mp->udp_decap.port),
184 mp->udp_decap.is_ip4);
185 REPLY_MACRO (VL_API_UDP_DECAP_ADD_DEL_REPLY);
186}
187
Filip Tehlar30062892021-06-21 13:01:24 +0000188#include <vnet/udp/udp.api.c>
Neale Ranns810086d2017-11-05 16:26:46 -0800189static clib_error_t *
190udp_api_hookup (vlib_main_t * vm)
191{
Neale Ranns810086d2017-11-05 16:26:46 -0800192 /*
193 * Set up the (msg_name, crc, message-id) table
194 */
Filip Tehlar30062892021-06-21 13:01:24 +0000195 REPLY_MSG_ID_BASE = setup_message_id_table ();
Neale Ranns810086d2017-11-05 16:26:46 -0800196
197 return 0;
198}
199
200VLIB_API_INIT_FUNCTION (udp_api_hookup);
201
202/*
203 * fd.io coding-style-patch-verification: ON
204 *
205 * Local Variables:
206 * eval: (c-set-style "gnu")
207 * End:
208 */