blob: 4ad5660fdf6dad217266801843898b39dadbd251 [file] [log] [blame]
Klement Sekera2bce0332017-02-09 06:03:46 +01001/*
2 * Copyright (c) 2011-2016 Cisco and/or its affiliates.
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 */
Klement Sekerab16bfe32017-02-28 11:56:48 +010015/**
16 * @file
17 * @brief BFD UDP transport layer implementation
18 */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020019#include <vppinfra/types.h>
20#include <vlibmemory/api.h>
21#include <vlib/vlib.h>
22#include <vlib/buffer.h>
23#include <vnet/ip/format.h>
24#include <vnet/ethernet/packet.h>
Florin Corasb040f982020-10-20 14:59:43 -070025#include <vnet/udp/udp_local.h>
Dave Barach68b0fb02017-02-28 15:15:56 -050026#include <vnet/udp/udp_packet.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020027#include <vnet/ip/lookup.h>
28#include <vnet/ip/icmp46_packet.h>
29#include <vnet/ip/ip4.h>
30#include <vnet/ip/ip6.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020031#include <vnet/ip/ip6_packet.h>
Neale Rannscbe25aa2019-09-30 10:53:31 +000032#include <vnet/ip/ip6_link.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020033#include <vnet/adj/adj.h>
34#include <vnet/adj/adj_nbr.h>
Klement Sekera239790f2017-02-16 10:53:53 +010035#include <vnet/dpo/receive_dpo.h>
36#include <vnet/fib/fib_entry.h>
37#include <vnet/fib/fib_table.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020038#include <vnet/bfd/bfd_debug.h>
39#include <vnet/bfd/bfd_udp.h>
40#include <vnet/bfd/bfd_main.h>
41#include <vnet/bfd/bfd_api.h>
42
43typedef struct
44{
45 bfd_main_t *bfd_main;
46 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
47 * discriminator is unknown */
48 mhash_t bfd_session_idx_by_bfd_key;
Klement Sekera239790f2017-02-16 10:53:53 +010049 /* convenience variable */
50 vnet_main_t *vnet_main;
51 /* flag indicating whether echo_source_sw_if_index holds a valid value */
52 int echo_source_is_set;
53 /* loopback interface used to get echo source ip */
54 u32 echo_source_sw_if_index;
Klement Sekerae50e8562017-04-04 16:19:48 +020055 /* node index of "ip4-arp" node */
56 u32 ip4_arp_idx;
57 /* node index of "ip6-discover-neighbor" node */
58 u32 ip6_ndp_idx;
59 /* node index of "ip4-rewrite" node */
60 u32 ip4_rewrite_idx;
61 /* node index of "ip6-rewrite" node */
62 u32 ip6_rewrite_idx;
Matthew G Smith2f260142019-05-22 14:49:24 -050063 /* node index of "ip4-midchain" node */
64 u32 ip4_midchain_idx;
65 /* node index of "ip6-midchain" node */
66 u32 ip6_midchain_idx;
Damjan Marion07a38572018-01-21 06:44:18 -080067 /* log class */
68 vlib_log_class_t log_class;
Klement Sekera88aec652019-03-11 14:04:19 +010069 /* number of active udp4 sessions */
70 u32 udp4_sessions_count;
71 /* number of active udp6 sessions */
72 u32 udp6_sessions_count;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020073} bfd_udp_main_t;
74
Klement Sekerae4504c62016-12-08 10:16:41 +010075static vlib_node_registration_t bfd_udp4_input_node;
76static vlib_node_registration_t bfd_udp6_input_node;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010077static vlib_node_registration_t bfd_udp_echo4_input_node;
78static vlib_node_registration_t bfd_udp_echo6_input_node;
Klement Sekerae4504c62016-12-08 10:16:41 +010079
Klement Sekera0e3c0de2016-09-29 14:43:44 +020080bfd_udp_main_t bfd_udp_main;
81
Klement Sekera239790f2017-02-16 10:53:53 +010082vnet_api_error_t
83bfd_udp_set_echo_source (u32 sw_if_index)
84{
85 vnet_sw_interface_t *sw_if =
Dave Barach3940de32019-07-23 16:28:36 -040086 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera239790f2017-02-16 10:53:53 +010087 if (sw_if)
88 {
89 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
90 bfd_udp_main.echo_source_is_set = 1;
91 return 0;
92 }
93 return VNET_API_ERROR_BFD_ENOENT;
94}
95
96vnet_api_error_t
97bfd_udp_del_echo_source (u32 sw_if_index)
98{
99 bfd_udp_main.echo_source_sw_if_index = ~0;
100 bfd_udp_main.echo_source_is_set = 0;
101 return 0;
102}
103
104int
105bfd_udp_is_echo_available (bfd_transport_e transport)
106{
107 if (!bfd_udp_main.echo_source_is_set)
108 {
Klement Sekera73884482017-02-23 09:26:30 +0100109 BFD_DBG ("UDP echo source not set - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100110 return 0;
111 }
112 /*
113 * for the echo to work, we need a loopback interface with at least one
114 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
115 * pick an unused address from that subnet
116 */
117 vnet_sw_interface_t *sw_if =
Dave Barach3940de32019-07-23 16:28:36 -0400118 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
119 bfd_udp_main.echo_source_sw_if_index);
Klement Sekera239790f2017-02-16 10:53:53 +0100120 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
121 {
122 if (BFD_TRANSPORT_UDP4 == transport)
123 {
124 ip4_main_t *im = &ip4_main;
125 ip_interface_address_t *ia = NULL;
126 /* *INDENT-OFF* */
127 foreach_ip_interface_address (&im->lookup_main, ia,
128 bfd_udp_main.echo_source_sw_if_index,
129 0 /* honor unnumbered */, ({
130 if (ia->address_length <= 31)
131 {
132 return 1;
133 }
134 }));
135 /* *INDENT-ON* */
136 }
137 else if (BFD_TRANSPORT_UDP6 == transport)
138 {
139 ip6_main_t *im = &ip6_main;
140 ip_interface_address_t *ia = NULL;
141 /* *INDENT-OFF* */
142 foreach_ip_interface_address (&im->lookup_main, ia,
143 bfd_udp_main.echo_source_sw_if_index,
144 0 /* honor unnumbered */, ({
145 if (ia->address_length <= 127)
146 {
147 return 1;
148 }
149 }));
150 /* *INDENT-ON* */
151 }
152 }
Klement Sekera73884482017-02-23 09:26:30 +0100153 BFD_DBG ("No usable IP address for UDP echo - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100154 return 0;
155}
156
Klement Sekera2bce0332017-02-09 06:03:46 +0100157static u16
158bfd_udp_bs_idx_to_sport (u32 bs_idx)
159{
160 /* The source port MUST be in the range 49152 through 65535. The same UDP
161 * source port number MUST be used for all BFD Control packets associated
162 * with a particular session. The source port number SHOULD be unique among
163 * all BFD sessions on the system. If more than 16384 BFD sessions are
164 * simultaneously active, UDP source port numbers MAY be reused on
165 * multiple sessions, but the number of distinct uses of the same UDP
166 * source port number SHOULD be minimized.
167 */
168 return 49152 + bs_idx % (65535 - 49152 + 1);
169}
170
Klement Sekera239790f2017-02-16 10:53:53 +0100171int
172bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
173{
174 if (!bfd_udp_main.echo_source_is_set)
175 {
176 BFD_ERR ("cannot find ip4 address, echo source not set");
177 return 0;
178 }
179 ip_interface_address_t *ia = NULL;
180 ip4_main_t *im = &ip4_main;
181
182 /* *INDENT-OFF* */
183 foreach_ip_interface_address (
184 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
185 0 /* honor unnumbered */, ({
186 ip4_address_t *x =
187 ip_interface_address_get_address (&im->lookup_main, ia);
188 if (ia->address_length <= 31)
189 {
190 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200191 /*
192 * flip the last bit to get a different address, might be network,
193 * we don't care ...
194 */
195 addr->as_u32 ^= 1;
Klement Sekera239790f2017-02-16 10:53:53 +0100196 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
197 return 1;
198 }
199 }));
200 /* *INDENT-ON* */
201 BFD_ERR ("cannot find ip4 address, no usable address found");
202 return 0;
203}
204
205int
206bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
207{
208 if (!bfd_udp_main.echo_source_is_set)
209 {
210 BFD_ERR ("cannot find ip6 address, echo source not set");
211 return 0;
212 }
213 ip_interface_address_t *ia = NULL;
214 ip6_main_t *im = &ip6_main;
215
216 /* *INDENT-OFF* */
217 foreach_ip_interface_address (
218 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
219 0 /* honor unnumbered */, ({
220 ip6_address_t *x =
221 ip_interface_address_get_address (&im->lookup_main, ia);
222 if (ia->address_length <= 127)
223 {
224 *addr = *x;
225 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
Klement Sekera239790f2017-02-16 10:53:53 +0100226 return 1;
227 }
228 }));
229 /* *INDENT-ON* */
230 BFD_ERR ("cannot find ip6 address, no usable address found");
231 return 0;
232}
233
Klement Sekera73884482017-02-23 09:26:30 +0100234void
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200235bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
236 int *have_usable_ip4, ip4_address_t * ip4,
237 int *have_usable_ip6, ip6_address_t * ip6)
Klement Sekera73884482017-02-23 09:26:30 +0100238{
239 if (bfd_udp_main.echo_source_is_set)
240 {
241 *is_set = 1;
242 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
243 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
244 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
245 }
246 else
247 {
248 *is_set = 0;
249 }
250}
251
Klement Sekera239790f2017-02-16 10:53:53 +0100252int
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200253bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
254 int is_echo)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200255{
Klement Sekera2bce0332017-02-09 06:03:46 +0100256 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100257 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200258 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200259
Damjan Marion213b5aa2017-07-13 21:19:27 +0200260 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200261 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
262 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200263 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
264 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
Klement Sekera402ed312017-01-18 09:44:36 +0100265 typedef struct
266 {
267 ip4_header_t ip4;
268 udp_header_t udp;
269 } ip4_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100270 ip4_udp_headers *headers = NULL;
Klement Sekera402ed312017-01-18 09:44:36 +0100271 vlib_buffer_advance (b, -sizeof (*headers));
272 headers = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400273 clib_memset (headers, 0, sizeof (*headers));
Klement Sekera402ed312017-01-18 09:44:36 +0100274 headers->ip4.ip_version_and_header_length = 0x45;
275 headers->ip4.ttl = 255;
276 headers->ip4.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100277 headers->udp.src_port =
278 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100279 if (is_echo)
280 {
281 int rv;
282 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
283 {
284 return rv;
285 }
286 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
287 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
288 }
289 else
290 {
291 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
292 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
293 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
294 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100295
296 /* fix ip length, checksum and udp length */
297 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
298
Klement Sekera402ed312017-01-18 09:44:36 +0100299 headers->ip4.length = clib_host_to_net_u16 (ip_length);
300 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100301
Klement Sekera402ed312017-01-18 09:44:36 +0100302 const u16 udp_length = ip_length - (sizeof (headers->ip4));
303 headers->udp.length = clib_host_to_net_u16 (udp_length);
Klement Sekera239790f2017-02-16 10:53:53 +0100304 return 1;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100305}
306
Klement Sekera239790f2017-02-16 10:53:53 +0100307int
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200308bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
309 int is_echo)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100310{
Klement Sekera2bce0332017-02-09 06:03:46 +0100311 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100312 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200313 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100314
Damjan Marion213b5aa2017-07-13 21:19:27 +0200315 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100316 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
317 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200318 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
319 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100320 typedef struct
321 {
322 ip6_header_t ip6;
323 udp_header_t udp;
324 } ip6_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100325 ip6_udp_headers *headers = NULL;
326 vlib_buffer_advance (b, -sizeof (*headers));
327 headers = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400328 clib_memset (headers, 0, sizeof (*headers));
Klement Sekera402ed312017-01-18 09:44:36 +0100329 headers->ip6.ip_version_traffic_class_and_flow_label =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100330 clib_host_to_net_u32 (0x6 << 28);
Klement Sekera402ed312017-01-18 09:44:36 +0100331 headers->ip6.hop_limit = 255;
332 headers->ip6.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100333 headers->udp.src_port =
334 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100335 if (is_echo)
336 {
337 int rv;
338 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
339 {
340 return rv;
341 }
Dave Barach178cf492018-11-13 16:34:13 -0500342 clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
343 sizeof (headers->ip6.dst_address));
Klement Sekera239790f2017-02-16 10:53:53 +0100344
345 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
346 }
347 else
348 {
Dave Barach178cf492018-11-13 16:34:13 -0500349 clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
350 sizeof (headers->ip6.src_address));
351 clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
352 sizeof (headers->ip6.dst_address));
Klement Sekera239790f2017-02-16 10:53:53 +0100353 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
354 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100355
356 /* fix ip payload length and udp length */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100357 const u16 udp_length =
Klement Sekera402ed312017-01-18 09:44:36 +0100358 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
359 headers->udp.length = clib_host_to_net_u16 (udp_length);
360 headers->ip6.payload_length = headers->udp.length;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100361
362 /* IPv6 UDP checksum is mandatory */
363 int bogus = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100364 headers->udp.checksum =
365 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100366 ASSERT (bogus == 0);
Klement Sekera402ed312017-01-18 09:44:36 +0100367 if (headers->udp.checksum == 0)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100368 {
Klement Sekera402ed312017-01-18 09:44:36 +0100369 headers->udp.checksum = 0xffff;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100370 }
Klement Sekera239790f2017-02-16 10:53:53 +0100371 return 1;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200372}
373
Klement Sekerae50e8562017-04-04 16:19:48 +0200374static void
375bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
376{
377 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
378 u32 *to_next = vlib_frame_vector_args (f);
379 to_next[0] = bi;
380 f->n_vectors = 1;
381 vlib_put_frame_to_node (vm, next_node, f);
382}
383
384int
385bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
386{
Matthew G Smith2f260142019-05-22 14:49:24 -0500387 vnet_main_t *vnm = vnet_get_main ();
Klement Sekerae50e8562017-04-04 16:19:48 +0200388 const bfd_udp_session_t *bus = &bs->udp;
389 ip_adjacency_t *adj = adj_get (bus->adj_index);
Matthew G Smith2f260142019-05-22 14:49:24 -0500390
391 /* don't try to send the buffer if the interface is not up */
392 if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
393 return 0;
394
Klement Sekerae50e8562017-04-04 16:19:48 +0200395 switch (adj->lookup_next_index)
396 {
397 case IP_LOOKUP_NEXT_ARP:
398 switch (bs->transport)
399 {
400 case BFD_TRANSPORT_UDP4:
401 *next_node = bfd_udp_main.ip4_arp_idx;
402 return 1;
403 case BFD_TRANSPORT_UDP6:
404 *next_node = bfd_udp_main.ip6_ndp_idx;
405 return 1;
406 }
407 break;
408 case IP_LOOKUP_NEXT_REWRITE:
409 switch (bs->transport)
410 {
411 case BFD_TRANSPORT_UDP4:
412 *next_node = bfd_udp_main.ip4_rewrite_idx;
413 return 1;
414 case BFD_TRANSPORT_UDP6:
415 *next_node = bfd_udp_main.ip6_rewrite_idx;
416 return 1;
417 }
418 break;
Matthew G Smith2f260142019-05-22 14:49:24 -0500419 case IP_LOOKUP_NEXT_MIDCHAIN:
420 switch (bs->transport)
421 {
422 case BFD_TRANSPORT_UDP4:
423 *next_node = bfd_udp_main.ip4_midchain_idx;
424 return 1;
425 case BFD_TRANSPORT_UDP6:
426 *next_node = bfd_udp_main.ip6_midchain_idx;
427 return 1;
428 }
429 break;
Klement Sekerae50e8562017-04-04 16:19:48 +0200430 default:
431 /* drop */
432 break;
433 }
434 return 0;
435}
436
437int
438bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
439{
440 u32 next_node;
441 int rv = bfd_udp_calc_next_node (bs, &next_node);
442 if (rv)
443 {
444 bfd_create_frame_to_next_node (vm, bi, next_node);
445 }
446 return rv;
447}
448
449int
450bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
451{
452 u32 next_node;
453 int rv = bfd_udp_calc_next_node (bs, &next_node);
454 if (rv)
455 {
456 bfd_create_frame_to_next_node (vm, bi, next_node);
457 }
458 return 1;
459}
460
Klement Sekerac5fccc02017-01-18 09:56:00 +0100461static bfd_session_t *
462bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200463{
464 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
465 if (p)
466 {
467 return bfd_find_session_by_idx (bum->bfd_main, *p);
468 }
469 return 0;
470}
471
Klement Sekerab17dd962017-01-09 07:43:48 +0100472static void
473bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
474 const ip46_address_t * local_addr,
475 const ip46_address_t * peer_addr)
476{
Dave Barachb7b92992018-10-17 10:38:51 -0400477 clib_memset (key, 0, sizeof (*key));
Klement Sekerab17dd962017-01-09 07:43:48 +0100478 key->sw_if_index = sw_if_index;
479 key->local_addr.as_u64[0] = local_addr->as_u64[0];
480 key->local_addr.as_u64[1] = local_addr->as_u64[1];
481 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
482 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
483}
484
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200485static vnet_api_error_t
Klement Sekera88aec652019-03-11 14:04:19 +0100486bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
487 u32 sw_if_index, u32 desired_min_tx_usec,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100488 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100489 const ip46_address_t * local_addr,
490 const ip46_address_t * peer_addr,
Klement Sekerab17dd962017-01-09 07:43:48 +0100491 bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200492{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200493 /* get a pool entry and if we end up not needing it, give it back */
Klement Sekera239790f2017-02-16 10:53:53 +0100494 bfd_transport_e t = BFD_TRANSPORT_UDP4;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200495 if (!ip46_address_is_ip4 (local_addr))
496 {
497 t = BFD_TRANSPORT_UDP6;
498 }
499 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
Klement Sekera239790f2017-02-16 10:53:53 +0100500 if (!bs)
501 {
Klement Sekera239790f2017-02-16 10:53:53 +0100502 return VNET_API_ERROR_BFD_EAGAIN;
503 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200504 bfd_udp_session_t *bus = &bs->udp;
Dave Barachb7b92992018-10-17 10:38:51 -0400505 clib_memset (bus, 0, sizeof (*bus));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200506 bfd_udp_key_t *key = &bus->key;
Klement Sekerab17dd962017-01-09 07:43:48 +0100507 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200508 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
509 if (tmp)
510 {
Damjan Marion07a38572018-01-21 06:44:18 -0800511 vlib_log_err (bum->log_class,
512 "duplicate bfd-udp session, existing bs_idx=%d",
Klement Sekerab17dd962017-01-09 07:43:48 +0100513 tmp->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200514 bfd_put_session (bum->bfd_main, bs);
515 return VNET_API_ERROR_BFD_EEXIST;
516 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200517 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
518 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100519 bs->bs_idx, key->sw_if_index, format_ip46_address,
520 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
521 &key->peer_addr, IP46_TYPE_ANY);
Damjan Marion07a38572018-01-21 06:44:18 -0800522 vlib_log_info (bum->log_class, "create BFD session: %U",
523 format_bfd_session, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200524 if (BFD_TRANSPORT_UDP4 == t)
525 {
526 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100527 &key->peer_addr,
528 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200529 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100530 "returns %d", format_ip46_address, &key->peer_addr,
531 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera88aec652019-03-11 14:04:19 +0100532 ++bum->udp4_sessions_count;
533 if (1 == bum->udp4_sessions_count)
534 {
535 udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
536 bfd_udp4_input_node.index, 1);
537 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
538 bfd_udp_echo4_input_node.index, 1);
539 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200540 }
541 else
542 {
543 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100544 &key->peer_addr,
545 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200546 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100547 "returns %d", format_ip46_address, &key->peer_addr,
548 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera88aec652019-03-11 14:04:19 +0100549 ++bum->udp6_sessions_count;
550 if (1 == bum->udp6_sessions_count)
551 {
552 udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
553 bfd_udp6_input_node.index, 0);
554 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
555 bfd_udp_echo6_input_node.index, 0);
556 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200557 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100558 *bs_out = bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100559 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
560 required_min_rx_usec, detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200561}
562
563static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100564bfd_udp_validate_api_input (u32 sw_if_index,
565 const ip46_address_t * local_addr,
566 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200567{
Damjan Marion07a38572018-01-21 06:44:18 -0800568 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200569 vnet_sw_interface_t *sw_if =
Dave Barach3940de32019-07-23 16:28:36 -0400570 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200571 u8 local_ip_valid = 0;
572 ip_interface_address_t *ia = NULL;
573 if (!sw_if)
574 {
Damjan Marion07a38572018-01-21 06:44:18 -0800575 vlib_log_err (bum->log_class,
576 "got NULL sw_if when getting interface by index %u",
577 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200578 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
579 }
580 if (ip46_address_is_ip4 (local_addr))
581 {
582 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100583 {
Damjan Marion07a38572018-01-21 06:44:18 -0800584 vlib_log_err (bum->log_class,
585 "IP family mismatch (local is ipv4, peer is ipv6)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100586 return VNET_API_ERROR_INVALID_ARGUMENT;
587 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200588 ip4_main_t *im = &ip4_main;
589
590 /* *INDENT-OFF* */
591 foreach_ip_interface_address (
592 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
593 ip4_address_t *x =
594 ip_interface_address_get_address (&im->lookup_main, ia);
595 if (x->as_u32 == local_addr->ip4.as_u32)
596 {
597 /* valid address for this interface */
598 local_ip_valid = 1;
599 break;
600 }
601 }));
602 /* *INDENT-ON* */
603 }
604 else
605 {
606 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100607 {
Damjan Marion07a38572018-01-21 06:44:18 -0800608 vlib_log_err (bum->log_class,
609 "IP family mismatch (local is ipv6, peer is ipv4)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100610 return VNET_API_ERROR_INVALID_ARGUMENT;
611 }
Alexander Chernavin845f51f2019-10-31 10:38:43 -0400612
613 if (ip6_address_is_link_local_unicast (&local_addr->ip6))
614 {
Neale Rannscbe25aa2019-09-30 10:53:31 +0000615 const ip6_address_t *ll_addr;
616 ll_addr = ip6_get_link_local_address (sw_if_index);
617 if (ip6_address_is_equal (ll_addr, &local_addr->ip6))
Alexander Chernavin845f51f2019-10-31 10:38:43 -0400618 {
619 /* valid address for this interface */
620 local_ip_valid = 1;
621 }
622 }
623 else
624 {
625 ip6_main_t *im = &ip6_main;
626 /* *INDENT-OFF* */
627 foreach_ip_interface_address (
628 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
629 ip6_address_t *x =
630 ip_interface_address_get_address (&im->lookup_main, ia);
631 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
632 local_addr->ip6.as_u64[1] == x->as_u64[1])
633 {
634 /* valid address for this interface */
635 local_ip_valid = 1;
636 break;
637 }
638 }));
639 /* *INDENT-ON* */
640 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200641 }
642
643 if (!local_ip_valid)
644 {
Damjan Marion07a38572018-01-21 06:44:18 -0800645 vlib_log_err (bum->log_class,
646 "local address %U not found on interface with index %u",
Jon Loeliger6732aea2018-05-05 16:11:31 -0500647 format_ip46_address, local_addr, IP46_TYPE_ANY,
Damjan Marion07a38572018-01-21 06:44:18 -0800648 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200649 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
650 }
651
652 return 0;
653}
654
Klement Sekerab17dd962017-01-09 07:43:48 +0100655static vnet_api_error_t
656bfd_udp_find_session_by_api_input (u32 sw_if_index,
657 const ip46_address_t * local_addr,
658 const ip46_address_t * peer_addr,
659 bfd_session_t ** bs_out)
660{
661 vnet_api_error_t rv =
662 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
663 if (!rv)
664 {
665 bfd_udp_main_t *bum = &bfd_udp_main;
666 bfd_udp_key_t key;
667 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
668 bfd_session_t *bs = bfd_lookup_session (bum, &key);
669 if (bs)
670 {
671 *bs_out = bs;
672 }
673 else
674 {
Damjan Marion07a38572018-01-21 06:44:18 -0800675 vlib_log_err (bum->log_class,
676 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
677 sw_if_index, format_ip46_address, local_addr,
678 IP46_TYPE_ANY, format_ip46_address, peer_addr,
679 IP46_TYPE_ANY);
Klement Sekerab17dd962017-01-09 07:43:48 +0100680 return VNET_API_ERROR_BFD_ENOENT;
681 }
682 }
683 return rv;
684}
685
686static vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100687bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
688 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerab17dd962017-01-09 07:43:48 +0100689 const ip46_address_t * local_addr,
690 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200691{
Damjan Marion07a38572018-01-21 06:44:18 -0800692 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200693 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100694 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200695 if (rv)
696 {
697 return rv;
698 }
699 if (detect_mult < 1)
700 {
Damjan Marion07a38572018-01-21 06:44:18 -0800701 vlib_log_err (bum->log_class, "detect_mult < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200702 return VNET_API_ERROR_INVALID_ARGUMENT;
703 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100704 if (desired_min_tx_usec < 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200705 {
Damjan Marion07a38572018-01-21 06:44:18 -0800706 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200707 return VNET_API_ERROR_INVALID_ARGUMENT;
708 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100709 return 0;
710}
711
712static void
Klement Sekera88aec652019-03-11 14:04:19 +0100713bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
Klement Sekerab17dd962017-01-09 07:43:48 +0100714{
715 bfd_udp_main_t *bum = &bfd_udp_main;
716 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
717 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
718 adj_unlock (bs->udp.adj_index);
Klement Sekera88aec652019-03-11 14:04:19 +0100719 switch (bs->transport)
720 {
721 case BFD_TRANSPORT_UDP4:
722 --bum->udp4_sessions_count;
723 if (!bum->udp4_sessions_count)
724 {
725 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
726 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
727 }
728 break;
729 case BFD_TRANSPORT_UDP6:
730 --bum->udp6_sessions_count;
731 if (!bum->udp6_sessions_count)
732 {
733 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
734 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
735 }
736 break;
737 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100738 bfd_put_session (bum->bfd_main, bs);
739}
740
741vnet_api_error_t
742bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100743 const ip46_address_t * peer_addr,
744 u32 desired_min_tx_usec, u32 required_min_rx_usec,
745 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
746 u8 bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100747{
Dave Barach1e3417f2018-07-25 08:30:27 -0400748 bfd_main_t *bm = &bfd_main;
749 bfd_lock (bm);
750
Klement Sekeraa57a9702017-02-02 06:58:07 +0100751 vnet_api_error_t rv =
752 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
753 required_min_rx_usec, detect_mult,
754 local_addr, peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +0100755 bfd_session_t *bs = NULL;
756 if (!rv)
757 {
758 rv =
Klement Sekera88aec652019-03-11 14:04:19 +0100759 bfd_udp_add_session_internal (vlib_get_main (), &bfd_udp_main,
760 sw_if_index, desired_min_tx_usec,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100761 required_min_rx_usec, detect_mult,
762 local_addr, peer_addr, &bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100763 }
764 if (!rv && is_authenticated)
765 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100766 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
767 0 /* is not delayed */ );
Klement Sekerab17dd962017-01-09 07:43:48 +0100768 if (rv)
769 {
Klement Sekera88aec652019-03-11 14:04:19 +0100770 bfd_udp_del_session_internal (vlib_get_main (), bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100771 }
772 }
773 if (!rv)
774 {
775 bfd_session_start (bfd_udp_main.bfd_main, bs);
776 }
777
Dave Barach1e3417f2018-07-25 08:30:27 -0400778 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100779 return rv;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200780}
781
Klement Sekerac5fccc02017-01-18 09:56:00 +0100782vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100783bfd_udp_mod_session (u32 sw_if_index,
784 const ip46_address_t * local_addr,
785 const ip46_address_t * peer_addr,
786 u32 desired_min_tx_usec,
787 u32 required_min_rx_usec, u8 detect_mult)
788{
789 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400790 bfd_main_t *bm = &bfd_main;
791 vnet_api_error_t error;
792 bfd_lock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100793 vnet_api_error_t rv =
794 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
795 &bs);
796 if (rv)
797 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400798 bfd_unlock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100799 return rv;
800 }
801
Dave Barach1e3417f2018-07-25 08:30:27 -0400802 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
803 desired_min_tx_usec, required_min_rx_usec,
804 detect_mult);
805 bfd_unlock (bm);
806 return error;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100807}
808
809vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100810bfd_udp_del_session (u32 sw_if_index,
811 const ip46_address_t * local_addr,
812 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200813{
Klement Sekerab17dd962017-01-09 07:43:48 +0100814 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400815 bfd_main_t *bm = &bfd_main;
816 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200817 vnet_api_error_t rv =
Klement Sekerab17dd962017-01-09 07:43:48 +0100818 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
819 &bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200820 if (rv)
821 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400822 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200823 return rv;
824 }
Klement Sekera88aec652019-03-11 14:04:19 +0100825 bfd_udp_del_session_internal (vlib_get_main (), bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400826 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100827 return 0;
828}
829
830vnet_api_error_t
Klement Sekeraa3167442020-02-10 11:49:52 +0000831bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
Klement Sekerab17dd962017-01-09 07:43:48 +0100832 const ip46_address_t * local_addr,
833 const ip46_address_t * peer_addr, u8 admin_up_down)
834{
835 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400836 bfd_main_t *bm = &bfd_main;
837 bfd_lock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100838 vnet_api_error_t rv =
839 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
840 &bs);
841 if (rv)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200842 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400843 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100844 return rv;
845 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000846 bfd_session_set_flags (vm, bs, admin_up_down);
Dave Barach1e3417f2018-07-25 08:30:27 -0400847 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100848 return 0;
849}
850
851vnet_api_error_t
Klement Sekerab17dd962017-01-09 07:43:48 +0100852bfd_udp_auth_activate (u32 sw_if_index,
853 const ip46_address_t * local_addr,
854 const ip46_address_t * peer_addr,
855 u32 conf_key_id, u8 key_id, u8 is_delayed)
856{
Dave Barach1e3417f2018-07-25 08:30:27 -0400857 bfd_main_t *bm = &bfd_main;
858 bfd_lock (bm);
859 vnet_api_error_t error;
860
Klement Sekerab17dd962017-01-09 07:43:48 +0100861 bfd_session_t *bs = NULL;
862 vnet_api_error_t rv =
863 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
864 &bs);
865 if (rv)
866 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400867 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100868 return rv;
869 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400870 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
871 bfd_unlock (bm);
872 return error;
Klement Sekerab17dd962017-01-09 07:43:48 +0100873}
874
875vnet_api_error_t
876bfd_udp_auth_deactivate (u32 sw_if_index,
877 const ip46_address_t * local_addr,
878 const ip46_address_t * peer_addr, u8 is_delayed)
879{
Dave Barach1e3417f2018-07-25 08:30:27 -0400880 bfd_main_t *bm = &bfd_main;
881 vnet_api_error_t error;
882 bfd_lock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100883 bfd_session_t *bs = NULL;
884 vnet_api_error_t rv =
885 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
886 &bs);
887 if (rv)
888 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400889 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100890 return rv;
891 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400892 error = bfd_auth_deactivate (bs, is_delayed);
893 bfd_unlock (bm);
894 return error;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200895}
896
Klement Sekerac5fccc02017-01-18 09:56:00 +0100897typedef enum
898{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200899 BFD_UDP_INPUT_NEXT_NORMAL,
Klement Sekerae50e8562017-04-04 16:19:48 +0200900 BFD_UDP_INPUT_NEXT_REPLY_ARP,
901 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
Matthew G Smith2f260142019-05-22 14:49:24 -0500902 BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200903 BFD_UDP_INPUT_N_NEXT,
904} bfd_udp_input_next_t;
905
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100906/* Packet counters - BFD control frames */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200907#define foreach_bfd_udp_error(F) \
908 F (NONE, "good bfd packets (processed)") \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100909 F (BAD, "invalid bfd packets")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200910
911#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
912foreach_bfd_udp_error (F);
913#undef F
914
915static char *bfd_udp_error_strings[] = {
916#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
917 foreach_bfd_udp_error (F)
918#undef F
919};
920
Klement Sekerac5fccc02017-01-18 09:56:00 +0100921typedef enum
922{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200923#define F(sym, str) BFD_UDP_ERROR_##sym,
924 foreach_bfd_udp_error (F)
925#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100926 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200927} bfd_udp_error_t;
928
Matthew G Smith2f260142019-05-22 14:49:24 -0500929typedef enum
930{
931 BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
932 BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
933 BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
934 BFD_UDP_ECHO_INPUT_N_NEXT,
935} bfd_udp_echo_input_next_t;
936
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100937/* Packet counters - BFD ECHO packets */
938#define foreach_bfd_udp_echo_error(F) \
939 F (NONE, "good bfd echo packets (processed)") \
940 F (BAD, "invalid bfd echo packets")
941
942#define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
943foreach_bfd_udp_echo_error (F);
944#undef F
945
946static char *bfd_udp_echo_error_strings[] = {
947#define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
948 foreach_bfd_udp_echo_error (F)
949#undef F
950};
951
952typedef enum
953{
954#define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
955 foreach_bfd_udp_echo_error (F)
956#undef F
957 BFD_UDP_ECHO_N_ERROR,
958} bfd_udp_echo_error_t;
959
Klement Sekerac5fccc02017-01-18 09:56:00 +0100960static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100961bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
962 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200963{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100964 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +0200965 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera0c1519b2016-12-08 05:03:32 +0100966 if (start < 0 && start < sizeof (b->pre_data))
967 {
968 BFD_ERR ("Start of ip header is before pre_data, ignoring");
969 *ip4 = NULL;
970 *udp = NULL;
971 return;
972 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100973 *ip4 = (ip4_header_t *) (b->data + start);
974 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100975 {
976 BFD_ERR ("Start of ip header is beyond current data, ignoring");
977 *ip4 = NULL;
978 *udp = NULL;
979 return;
980 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100981 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200982}
983
Klement Sekerac5fccc02017-01-18 09:56:00 +0100984static bfd_udp_error_t
985bfd_udp4_verify_transport (const ip4_header_t * ip4,
986 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200987{
988 const bfd_udp_session_t *bus = &bs->udp;
989 const bfd_udp_key_t *key = &bus->key;
990 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
991 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100992 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100993 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
994 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200995 return BFD_UDP_ERROR_BAD;
996 }
997 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
998 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100999 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001000 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
1001 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001002 return BFD_UDP_ERROR_BAD;
1003 }
1004 const u8 expected_ttl = 255;
1005 if (ip4->ttl != expected_ttl)
1006 {
Klement Sekera46a87ad2017-01-02 08:22:23 +01001007 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +01001008 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001009 return BFD_UDP_ERROR_BAD;
1010 }
Klement Sekera6f966492017-02-08 07:42:08 +01001011 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001012 {
Klement Sekera46a87ad2017-01-02 08:22:23 +01001013 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001014 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001015 }
1016 return BFD_UDP_ERROR_NONE;
1017}
1018
1019typedef struct
1020{
1021 u32 bs_idx;
1022 bfd_pkt_t pkt;
1023} bfd_rpc_update_t;
1024
Klement Sekerac5fccc02017-01-18 09:56:00 +01001025static void
Klement Sekeraa3167442020-02-10 11:49:52 +00001026bfd_rpc_update_session (vlib_main_t * vm, u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001027{
Dave Barach1e3417f2018-07-25 08:30:27 -04001028 bfd_main_t *bm = &bfd_main;
1029 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001030 bfd_consume_pkt (vm, bm, pkt, bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -04001031 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001032}
1033
Klement Sekerac5fccc02017-01-18 09:56:00 +01001034static bfd_udp_error_t
1035bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1036 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001037{
1038 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1039 if (sizeof (*pkt) > b->current_length)
1040 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001041 BFD_ERR
1042 ("Payload size %d too small to hold bfd packet of minimum size %d",
1043 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001044 return BFD_UDP_ERROR_BAD;
1045 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001046 ip4_header_t *ip4;
1047 udp_header_t *udp;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001048 bfd_udp4_find_headers (b, &ip4, &udp);
1049 if (!ip4 || !udp)
1050 {
1051 BFD_ERR ("Couldn't find ip4 or udp header");
1052 return BFD_UDP_ERROR_BAD;
1053 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001054 const u32 udp_payload_length = udp->length - sizeof (*udp);
1055 if (pkt->head.length > udp_payload_length)
1056 {
1057 BFD_ERR
1058 ("BFD packet length is larger than udp payload length (%u > %u)",
1059 pkt->head.length, udp_payload_length);
1060 return BFD_UDP_ERROR_BAD;
1061 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001062 if (!bfd_verify_pkt_common (pkt))
1063 {
1064 return BFD_UDP_ERROR_BAD;
1065 }
1066 bfd_session_t *bs = NULL;
1067 if (pkt->your_disc)
1068 {
1069 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001070 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001071 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1072 }
1073 else
1074 {
1075 bfd_udp_key_t key;
Dave Barachb7b92992018-10-17 10:38:51 -04001076 clib_memset (&key, 0, sizeof (key));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001077 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1078 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1079 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1080 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +01001081 "peer=%U)",
1082 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1083 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001084 bs = bfd_lookup_session (&bfd_udp_main, &key);
1085 }
1086 if (!bs)
1087 {
1088 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1089 return BFD_UDP_ERROR_BAD;
1090 }
Klement Sekera637b9c42016-12-08 05:19:14 +01001091 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekeraa3167442020-02-10 11:49:52 +00001092 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001093 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001094 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001095 return BFD_UDP_ERROR_BAD;
1096 }
1097 bfd_udp_error_t err;
1098 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1099 {
1100 return err;
1101 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001102 bfd_rpc_update_session (vm, bs->bs_idx, pkt);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001103 *bs_out = bs;
1104 return BFD_UDP_ERROR_NONE;
1105}
1106
Klement Sekerac5fccc02017-01-18 09:56:00 +01001107static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001108bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1109 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001110{
Klement Sekera46a87ad2017-01-02 08:22:23 +01001111 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +02001112 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001113 if (start < 0 && start < sizeof (b->pre_data))
1114 {
1115 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1116 *ip6 = NULL;
1117 *udp = NULL;
1118 return;
1119 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001120 *ip6 = (ip6_header_t *) (b->data + start);
1121 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001122 {
1123 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1124 *ip6 = NULL;
1125 *udp = NULL;
1126 return;
1127 }
Klement Sekera402ed312017-01-18 09:44:36 +01001128 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1129 {
1130 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
Klement Sekerab17dd962017-01-09 07:43:48 +01001131 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
Klement Sekera402ed312017-01-18 09:44:36 +01001132 *ip6 = NULL;
1133 *udp = NULL;
1134 return;
1135 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001136 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001137}
1138
Klement Sekerac5fccc02017-01-18 09:56:00 +01001139static bfd_udp_error_t
1140bfd_udp6_verify_transport (const ip6_header_t * ip6,
1141 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001142{
1143 const bfd_udp_session_t *bus = &bs->udp;
1144 const bfd_udp_key_t *key = &bus->key;
1145 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1146 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1147 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001148 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1149 format_ip6_address, ip6, format_ip6_address,
1150 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001151 return BFD_UDP_ERROR_BAD;
1152 }
1153 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1154 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1155 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001156 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1157 format_ip6_address, ip6, format_ip6_address,
1158 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001159 return BFD_UDP_ERROR_BAD;
1160 }
1161 const u8 expected_hop_limit = 255;
1162 if (ip6->hop_limit != expected_hop_limit)
1163 {
1164 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001165 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001166 return BFD_UDP_ERROR_BAD;
1167 }
Klement Sekera6f966492017-02-08 07:42:08 +01001168 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001169 {
1170 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001171 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001172 }
1173 return BFD_UDP_ERROR_NONE;
1174}
1175
Klement Sekerac5fccc02017-01-18 09:56:00 +01001176static bfd_udp_error_t
1177bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1178 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001179{
1180 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1181 if (sizeof (*pkt) > b->current_length)
1182 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001183 BFD_ERR
1184 ("Payload size %d too small to hold bfd packet of minimum size %d",
1185 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001186 return BFD_UDP_ERROR_BAD;
1187 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001188 ip6_header_t *ip6;
1189 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001190 bfd_udp6_find_headers (b, &ip6, &udp);
1191 if (!ip6 || !udp)
1192 {
1193 BFD_ERR ("Couldn't find ip6 or udp header");
1194 return BFD_UDP_ERROR_BAD;
1195 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001196 const u32 udp_payload_length = udp->length - sizeof (*udp);
1197 if (pkt->head.length > udp_payload_length)
1198 {
1199 BFD_ERR
1200 ("BFD packet length is larger than udp payload length (%u > %u)",
1201 pkt->head.length, udp_payload_length);
1202 return BFD_UDP_ERROR_BAD;
1203 }
Klement Sekera46a87ad2017-01-02 08:22:23 +01001204 if (!bfd_verify_pkt_common (pkt))
1205 {
1206 return BFD_UDP_ERROR_BAD;
1207 }
1208 bfd_session_t *bs = NULL;
1209 if (pkt->your_disc)
1210 {
1211 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001212 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001213 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1214 }
1215 else
1216 {
1217 bfd_udp_key_t key;
Dave Barachb7b92992018-10-17 10:38:51 -04001218 clib_memset (&key, 0, sizeof (key));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001219 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1220 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1221 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1222 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1223 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1224 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerab17dd962017-01-09 07:43:48 +01001225 "peer=%U)",
1226 key.sw_if_index, format_ip6_address, &key.local_addr,
1227 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001228 bs = bfd_lookup_session (&bfd_udp_main, &key);
1229 }
1230 if (!bs)
1231 {
1232 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1233 return BFD_UDP_ERROR_BAD;
1234 }
1235 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekeraa3167442020-02-10 11:49:52 +00001236 if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001237 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001238 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera46a87ad2017-01-02 08:22:23 +01001239 return BFD_UDP_ERROR_BAD;
1240 }
1241 bfd_udp_error_t err;
1242 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1243 {
1244 return err;
1245 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001246 bfd_rpc_update_session (vm, bs->bs_idx, pkt);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001247 *bs_out = bs;
1248 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001249}
1250
1251/*
1252 * Process a frame of bfd packets
1253 * Expect 1 packet / frame
1254 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001255static uword
1256bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1257 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001258{
1259 u32 n_left_from, *from;
1260 bfd_input_trace_t *t0;
Dave Barach1e3417f2018-07-25 08:30:27 -04001261 bfd_main_t *bm = &bfd_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001262
Klement Sekerac5fccc02017-01-18 09:56:00 +01001263 from = vlib_frame_vector_args (f); /* array of buffer indices */
1264 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001265
1266 while (n_left_from > 0)
1267 {
1268 u32 bi0;
1269 vlib_buffer_t *b0;
1270 u32 next0, error0;
1271
1272 bi0 = from[0];
1273 b0 = vlib_get_buffer (vm, bi0);
1274
1275 bfd_session_t *bs = NULL;
1276
1277 /* If this pkt is traced, snapshot the data */
1278 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001279 {
1280 int len;
1281 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1282 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1283 : sizeof (t0->data);
1284 t0->len = len;
Dave Barach178cf492018-11-13 16:34:13 -05001285 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001286 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001287
1288 /* scan this bfd pkt. error0 is the counter index to bmp */
Dave Barach1e3417f2018-07-25 08:30:27 -04001289 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001290 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001291 {
1292 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1293 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001294 else
Klement Sekerac5fccc02017-01-18 09:56:00 +01001295 {
1296 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1297 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001298 b0->error = rt->errors[error0];
1299
1300 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1301 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001302 {
Klement Sekera402ed312017-01-18 09:44:36 +01001303 /*
1304 * if everything went fine, check for poll bit, if present, re-use
1305 * the buffer and based on (now updated) session parameters, send
1306 * the final packet back
1307 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001308 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1309 if (bfd_pkt_get_poll (pkt))
1310 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001311 b0->current_data = 0;
1312 b0->current_length = 0;
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001313 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1314 0);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001315 if (is_ipv6)
1316 {
1317 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1318 b0->error, 1);
1319 }
1320 else
1321 {
1322 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1323 b0->error, 1);
1324 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001325 const bfd_udp_session_t *bus = &bs->udp;
1326 ip_adjacency_t *adj = adj_get (bus->adj_index);
1327 switch (adj->lookup_next_index)
1328 {
1329 case IP_LOOKUP_NEXT_ARP:
1330 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1331 break;
1332 case IP_LOOKUP_NEXT_REWRITE:
1333 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1334 break;
Matthew G Smith2f260142019-05-22 14:49:24 -05001335 case IP_LOOKUP_NEXT_MIDCHAIN:
1336 next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1337 break;
Klement Sekerae50e8562017-04-04 16:19:48 +02001338 default:
1339 /* drop */
1340 break;
1341 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001342 }
1343 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001344 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001345 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1346
1347 from += 1;
1348 n_left_from -= 1;
1349 }
1350
1351 return f->n_vectors;
1352}
1353
Klement Sekerac5fccc02017-01-18 09:56:00 +01001354static uword
1355bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001356{
1357 return bfd_udp_input (vm, rt, f, 0);
1358}
1359
1360/*
1361 * bfd input graph node declaration
1362 */
1363/* *INDENT-OFF* */
1364VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1365 .function = bfd_udp4_input,
1366 .name = "bfd-udp4-input",
1367 .vector_size = sizeof (u32),
1368 .type = VLIB_NODE_TYPE_INTERNAL,
1369
1370 .n_errors = BFD_UDP_N_ERROR,
1371 .error_strings = bfd_udp_error_strings,
1372
1373 .format_trace = bfd_input_format_trace,
1374
1375 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1376 .next_nodes =
1377 {
1378 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001379 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1380 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Matthew G Smith2f260142019-05-22 14:49:24 -05001381 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001382 },
1383};
1384/* *INDENT-ON* */
1385
Klement Sekerac5fccc02017-01-18 09:56:00 +01001386static uword
1387bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001388{
1389 return bfd_udp_input (vm, rt, f, 1);
1390}
1391
1392/* *INDENT-OFF* */
1393VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1394 .function = bfd_udp6_input,
1395 .name = "bfd-udp6-input",
1396 .vector_size = sizeof (u32),
1397 .type = VLIB_NODE_TYPE_INTERNAL,
1398
1399 .n_errors = BFD_UDP_N_ERROR,
1400 .error_strings = bfd_udp_error_strings,
1401
1402 .format_trace = bfd_input_format_trace,
1403
1404 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1405 .next_nodes =
1406 {
1407 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001408 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1409 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Matthew G Smith2f260142019-05-22 14:49:24 -05001410 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001411 },
1412};
1413/* *INDENT-ON* */
1414
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001415/*
1416 * Process a frame of bfd echo packets
1417 * Expect 1 packet / frame
1418 */
1419static uword
1420bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1421 vlib_frame_t * f, int is_ipv6)
1422{
1423 u32 n_left_from, *from;
1424 bfd_input_trace_t *t0;
Dave Barach1e3417f2018-07-25 08:30:27 -04001425 bfd_main_t *bm = &bfd_main;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001426
1427 from = vlib_frame_vector_args (f); /* array of buffer indices */
1428 n_left_from = f->n_vectors; /* number of buffer indices */
1429
1430 while (n_left_from > 0)
1431 {
1432 u32 bi0;
1433 vlib_buffer_t *b0;
1434 u32 next0;
1435
1436 bi0 = from[0];
1437 b0 = vlib_get_buffer (vm, bi0);
1438
1439 /* If this pkt is traced, snapshot the data */
1440 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1441 {
1442 int len;
1443 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1444 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1445 : sizeof (t0->data);
1446 t0->len = len;
Dave Barach178cf492018-11-13 16:34:13 -05001447 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001448 }
1449
Dave Barach1e3417f2018-07-25 08:30:27 -04001450 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001451 if (bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0))
Klement Sekera239790f2017-02-16 10:53:53 +01001452 {
1453 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
Matthew G Smith2f260142019-05-22 14:49:24 -05001454 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
Klement Sekera239790f2017-02-16 10:53:53 +01001455 }
1456 else
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001457 {
1458 /* loop back the packet */
1459 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1460 if (is_ipv6)
1461 {
1462 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1463 b0->error, 1);
1464 }
1465 else
1466 {
1467 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1468 b0->error, 1);
1469 }
Matthew G Smith2f260142019-05-22 14:49:24 -05001470 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001471 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001472
Dave Barach1e3417f2018-07-25 08:30:27 -04001473 bfd_unlock (bm);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001474 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1475
1476 from += 1;
1477 n_left_from -= 1;
1478 }
1479
1480 return f->n_vectors;
1481}
1482
1483static uword
1484bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1485 vlib_frame_t * f)
1486{
1487 return bfd_udp_echo_input (vm, rt, f, 0);
1488}
1489
1490u8 *
1491bfd_echo_input_format_trace (u8 * s, va_list * args)
1492{
1493 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1494 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1495 const bfd_udp_echo_input_trace_t *t =
1496 va_arg (*args, bfd_udp_echo_input_trace_t *);
1497 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1498 {
1499 s = format (s, "BFD ECHO:\n");
1500 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1501 }
1502
1503 return s;
1504}
1505
1506/*
1507 * bfd input graph node declaration
1508 */
1509/* *INDENT-OFF* */
1510VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1511 .function = bfd_udp_echo4_input,
1512 .name = "bfd-udp-echo4-input",
1513 .vector_size = sizeof (u32),
1514 .type = VLIB_NODE_TYPE_INTERNAL,
1515
1516 .n_errors = BFD_UDP_ECHO_N_ERROR,
1517 .error_strings = bfd_udp_error_strings,
1518
1519 .format_trace = bfd_echo_input_format_trace,
1520
Matthew G Smith2f260142019-05-22 14:49:24 -05001521 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001522 .next_nodes =
1523 {
Matthew G Smith2f260142019-05-22 14:49:24 -05001524 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1525 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1526 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001527 },
1528};
1529/* *INDENT-ON* */
1530
1531static uword
1532bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1533 vlib_frame_t * f)
1534{
1535 return bfd_udp_echo_input (vm, rt, f, 1);
1536}
1537
1538/* *INDENT-OFF* */
1539VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1540 .function = bfd_udp_echo6_input,
1541 .name = "bfd-udp-echo6-input",
1542 .vector_size = sizeof (u32),
1543 .type = VLIB_NODE_TYPE_INTERNAL,
1544
1545 .n_errors = BFD_UDP_ECHO_N_ERROR,
1546 .error_strings = bfd_udp_echo_error_strings,
1547
1548 .format_trace = bfd_echo_input_format_trace,
1549
Matthew G Smith2f260142019-05-22 14:49:24 -05001550 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001551 .next_nodes =
1552 {
Matthew G Smith2f260142019-05-22 14:49:24 -05001553 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1554 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1555 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001556 },
1557};
1558
1559/* *INDENT-ON* */
1560
Klement Sekerac5fccc02017-01-18 09:56:00 +01001561static clib_error_t *
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001562bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001563{
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001564 bfd_session_t **to_be_freed = NULL;
Damjan Marion07a38572018-01-21 06:44:18 -08001565 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001566 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1567 is_create);
1568 if (!is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001569 {
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001570 bfd_session_t *bs;
Damjan Marionb2c31b62020-12-13 21:47:40 +01001571 pool_foreach (bs, bfd_udp_main.bfd_main->sessions)
1572 {
1573 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1574 bs->transport != BFD_TRANSPORT_UDP6)
1575 {
1576 continue;
1577 }
1578 if (bs->udp.key.sw_if_index != sw_if_index)
1579 {
1580 continue;
1581 }
1582 vec_add1 (to_be_freed, bs);
1583 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001584 }
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001585 bfd_session_t **bs;
1586 vec_foreach (bs, to_be_freed)
1587 {
Damjan Marion07a38572018-01-21 06:44:18 -08001588 vlib_log_notice (bum->log_class,
1589 "removal of sw_if_index=%u forces removal of bfd session "
1590 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
Klement Sekeraa3167442020-02-10 11:49:52 +00001591 bfd_session_set_flags (vlib_get_main (), *bs, 0);
Klement Sekera88aec652019-03-11 14:04:19 +01001592 bfd_udp_del_session_internal (vlib_get_main (), *bs);
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001593 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001594 return 0;
1595}
1596
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001597VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001598
1599/*
1600 * setup function
1601 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001602static clib_error_t *
1603bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001604{
Klement Sekera88aec652019-03-11 14:04:19 +01001605 bfd_udp_main.udp4_sessions_count = 0;
1606 bfd_udp_main.udp6_sessions_count = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001607 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +01001608 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001609 bfd_udp_main.bfd_main = &bfd_main;
Klement Sekera239790f2017-02-16 10:53:53 +01001610 bfd_udp_main.vnet_main = vnet_get_main ();
Klement Sekerae50e8562017-04-04 16:19:48 +02001611 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1612 ASSERT (node);
1613 bfd_udp_main.ip4_arp_idx = node->index;
1614 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1615 ASSERT (node);
1616 bfd_udp_main.ip6_ndp_idx = node->index;
1617 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1618 ASSERT (node);
1619 bfd_udp_main.ip4_rewrite_idx = node->index;
1620 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1621 ASSERT (node);
1622 bfd_udp_main.ip6_rewrite_idx = node->index;
Matthew G Smith2f260142019-05-22 14:49:24 -05001623 node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
1624 ASSERT (node);
1625 bfd_udp_main.ip4_midchain_idx = node->index;
1626 node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
1627 ASSERT (node);
1628 bfd_udp_main.ip6_midchain_idx = node->index;
Klement Sekerae50e8562017-04-04 16:19:48 +02001629
Damjan Marion07a38572018-01-21 06:44:18 -08001630 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1631 vlib_log_debug (bfd_udp_main.log_class, "initialized");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001632 return 0;
1633}
1634
1635VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001636
1637/*
1638 * fd.io coding-style-patch-verification: ON
1639 *
1640 * Local Variables:
1641 * eval: (c-set-style "gnu")
1642 * End:
1643 */