blob: 041e5840e0b866408371146b8997dc6db7ad7bbf [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>
Dave Barach68b0fb02017-02-28 15:15:56 -050025#include <vnet/udp/udp_packet.h>
26#include <vnet/udp/udp.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>
32#include <vnet/adj/adj.h>
33#include <vnet/adj/adj_nbr.h>
Klement Sekera239790f2017-02-16 10:53:53 +010034#include <vnet/dpo/receive_dpo.h>
35#include <vnet/fib/fib_entry.h>
36#include <vnet/fib/fib_table.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020037#include <vnet/bfd/bfd_debug.h>
38#include <vnet/bfd/bfd_udp.h>
39#include <vnet/bfd/bfd_main.h>
40#include <vnet/bfd/bfd_api.h>
41
42typedef struct
43{
44 bfd_main_t *bfd_main;
45 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
46 * discriminator is unknown */
47 mhash_t bfd_session_idx_by_bfd_key;
Klement Sekera239790f2017-02-16 10:53:53 +010048 /* convenience variable */
49 vnet_main_t *vnet_main;
50 /* flag indicating whether echo_source_sw_if_index holds a valid value */
51 int echo_source_is_set;
52 /* loopback interface used to get echo source ip */
53 u32 echo_source_sw_if_index;
Klement Sekerae50e8562017-04-04 16:19:48 +020054 /* node index of "ip4-arp" node */
55 u32 ip4_arp_idx;
56 /* node index of "ip6-discover-neighbor" node */
57 u32 ip6_ndp_idx;
58 /* node index of "ip4-rewrite" node */
59 u32 ip4_rewrite_idx;
60 /* node index of "ip6-rewrite" node */
61 u32 ip6_rewrite_idx;
Damjan Marion07a38572018-01-21 06:44:18 -080062 /* log class */
63 vlib_log_class_t log_class;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020064} bfd_udp_main_t;
65
Klement Sekerae4504c62016-12-08 10:16:41 +010066static vlib_node_registration_t bfd_udp4_input_node;
67static vlib_node_registration_t bfd_udp6_input_node;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010068static vlib_node_registration_t bfd_udp_echo4_input_node;
69static vlib_node_registration_t bfd_udp_echo6_input_node;
Klement Sekerae4504c62016-12-08 10:16:41 +010070
Klement Sekera0e3c0de2016-09-29 14:43:44 +020071bfd_udp_main_t bfd_udp_main;
72
Klement Sekera239790f2017-02-16 10:53:53 +010073vnet_api_error_t
74bfd_udp_set_echo_source (u32 sw_if_index)
75{
76 vnet_sw_interface_t *sw_if =
Klement Sekera73884482017-02-23 09:26:30 +010077 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera239790f2017-02-16 10:53:53 +010078 if (sw_if)
79 {
80 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
81 bfd_udp_main.echo_source_is_set = 1;
82 return 0;
83 }
84 return VNET_API_ERROR_BFD_ENOENT;
85}
86
87vnet_api_error_t
88bfd_udp_del_echo_source (u32 sw_if_index)
89{
90 bfd_udp_main.echo_source_sw_if_index = ~0;
91 bfd_udp_main.echo_source_is_set = 0;
92 return 0;
93}
94
95int
96bfd_udp_is_echo_available (bfd_transport_e transport)
97{
98 if (!bfd_udp_main.echo_source_is_set)
99 {
Klement Sekera73884482017-02-23 09:26:30 +0100100 BFD_DBG ("UDP echo source not set - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100101 return 0;
102 }
103 /*
104 * for the echo to work, we need a loopback interface with at least one
105 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
106 * pick an unused address from that subnet
107 */
108 vnet_sw_interface_t *sw_if =
109 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main,
110 bfd_udp_main.echo_source_sw_if_index);
111 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
112 {
113 if (BFD_TRANSPORT_UDP4 == transport)
114 {
115 ip4_main_t *im = &ip4_main;
116 ip_interface_address_t *ia = NULL;
117 /* *INDENT-OFF* */
118 foreach_ip_interface_address (&im->lookup_main, ia,
119 bfd_udp_main.echo_source_sw_if_index,
120 0 /* honor unnumbered */, ({
121 if (ia->address_length <= 31)
122 {
123 return 1;
124 }
125 }));
126 /* *INDENT-ON* */
127 }
128 else if (BFD_TRANSPORT_UDP6 == transport)
129 {
130 ip6_main_t *im = &ip6_main;
131 ip_interface_address_t *ia = NULL;
132 /* *INDENT-OFF* */
133 foreach_ip_interface_address (&im->lookup_main, ia,
134 bfd_udp_main.echo_source_sw_if_index,
135 0 /* honor unnumbered */, ({
136 if (ia->address_length <= 127)
137 {
138 return 1;
139 }
140 }));
141 /* *INDENT-ON* */
142 }
143 }
Klement Sekera73884482017-02-23 09:26:30 +0100144 BFD_DBG ("No usable IP address for UDP echo - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100145 return 0;
146}
147
Klement Sekera2bce0332017-02-09 06:03:46 +0100148static u16
149bfd_udp_bs_idx_to_sport (u32 bs_idx)
150{
151 /* The source port MUST be in the range 49152 through 65535. The same UDP
152 * source port number MUST be used for all BFD Control packets associated
153 * with a particular session. The source port number SHOULD be unique among
154 * all BFD sessions on the system. If more than 16384 BFD sessions are
155 * simultaneously active, UDP source port numbers MAY be reused on
156 * multiple sessions, but the number of distinct uses of the same UDP
157 * source port number SHOULD be minimized.
158 */
159 return 49152 + bs_idx % (65535 - 49152 + 1);
160}
161
Klement Sekera239790f2017-02-16 10:53:53 +0100162int
163bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
164{
165 if (!bfd_udp_main.echo_source_is_set)
166 {
167 BFD_ERR ("cannot find ip4 address, echo source not set");
168 return 0;
169 }
170 ip_interface_address_t *ia = NULL;
171 ip4_main_t *im = &ip4_main;
172
173 /* *INDENT-OFF* */
174 foreach_ip_interface_address (
175 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
176 0 /* honor unnumbered */, ({
177 ip4_address_t *x =
178 ip_interface_address_get_address (&im->lookup_main, ia);
179 if (ia->address_length <= 31)
180 {
181 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200182 /*
183 * flip the last bit to get a different address, might be network,
184 * we don't care ...
185 */
186 addr->as_u32 ^= 1;
Klement Sekera239790f2017-02-16 10:53:53 +0100187 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
188 return 1;
189 }
190 }));
191 /* *INDENT-ON* */
192 BFD_ERR ("cannot find ip4 address, no usable address found");
193 return 0;
194}
195
196int
197bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
198{
199 if (!bfd_udp_main.echo_source_is_set)
200 {
201 BFD_ERR ("cannot find ip6 address, echo source not set");
202 return 0;
203 }
204 ip_interface_address_t *ia = NULL;
205 ip6_main_t *im = &ip6_main;
206
207 /* *INDENT-OFF* */
208 foreach_ip_interface_address (
209 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
210 0 /* honor unnumbered */, ({
211 ip6_address_t *x =
212 ip_interface_address_get_address (&im->lookup_main, ia);
213 if (ia->address_length <= 127)
214 {
215 *addr = *x;
216 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
Klement Sekera239790f2017-02-16 10:53:53 +0100217 return 1;
218 }
219 }));
220 /* *INDENT-ON* */
221 BFD_ERR ("cannot find ip6 address, no usable address found");
222 return 0;
223}
224
Klement Sekera73884482017-02-23 09:26:30 +0100225void
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200226bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
227 int *have_usable_ip4, ip4_address_t * ip4,
228 int *have_usable_ip6, ip6_address_t * ip6)
Klement Sekera73884482017-02-23 09:26:30 +0100229{
230 if (bfd_udp_main.echo_source_is_set)
231 {
232 *is_set = 1;
233 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
234 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
235 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
236 }
237 else
238 {
239 *is_set = 0;
240 }
241}
242
Klement Sekera239790f2017-02-16 10:53:53 +0100243int
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200244bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
245 int is_echo)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200246{
Klement Sekera2bce0332017-02-09 06:03:46 +0100247 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100248 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200249 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200250
Damjan Marion213b5aa2017-07-13 21:19:27 +0200251 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200252 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
253 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200254 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
255 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
Klement Sekera402ed312017-01-18 09:44:36 +0100256 typedef struct
257 {
258 ip4_header_t ip4;
259 udp_header_t udp;
260 } ip4_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100261 ip4_udp_headers *headers = NULL;
Klement Sekera402ed312017-01-18 09:44:36 +0100262 vlib_buffer_advance (b, -sizeof (*headers));
263 headers = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400264 clib_memset (headers, 0, sizeof (*headers));
Klement Sekera402ed312017-01-18 09:44:36 +0100265 headers->ip4.ip_version_and_header_length = 0x45;
266 headers->ip4.ttl = 255;
267 headers->ip4.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100268 headers->udp.src_port =
269 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100270 if (is_echo)
271 {
272 int rv;
273 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
274 {
275 return rv;
276 }
277 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
278 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
279 }
280 else
281 {
282 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
283 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
284 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
285 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100286
287 /* fix ip length, checksum and udp length */
288 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
289
Klement Sekera402ed312017-01-18 09:44:36 +0100290 headers->ip4.length = clib_host_to_net_u16 (ip_length);
291 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100292
Klement Sekera402ed312017-01-18 09:44:36 +0100293 const u16 udp_length = ip_length - (sizeof (headers->ip4));
294 headers->udp.length = clib_host_to_net_u16 (udp_length);
Klement Sekera239790f2017-02-16 10:53:53 +0100295 return 1;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100296}
297
Klement Sekera239790f2017-02-16 10:53:53 +0100298int
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +0200299bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
300 int is_echo)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100301{
Klement Sekera2bce0332017-02-09 06:03:46 +0100302 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100303 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200304 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100305
Damjan Marion213b5aa2017-07-13 21:19:27 +0200306 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100307 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
308 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200309 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
310 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100311 typedef struct
312 {
313 ip6_header_t ip6;
314 udp_header_t udp;
315 } ip6_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100316 ip6_udp_headers *headers = NULL;
317 vlib_buffer_advance (b, -sizeof (*headers));
318 headers = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400319 clib_memset (headers, 0, sizeof (*headers));
Klement Sekera402ed312017-01-18 09:44:36 +0100320 headers->ip6.ip_version_traffic_class_and_flow_label =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100321 clib_host_to_net_u32 (0x6 << 28);
Klement Sekera402ed312017-01-18 09:44:36 +0100322 headers->ip6.hop_limit = 255;
323 headers->ip6.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100324 headers->udp.src_port =
325 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100326 if (is_echo)
327 {
328 int rv;
329 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
330 {
331 return rv;
332 }
Dave Barach178cf492018-11-13 16:34:13 -0500333 clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
334 sizeof (headers->ip6.dst_address));
Klement Sekera239790f2017-02-16 10:53:53 +0100335
336 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
337 }
338 else
339 {
Dave Barach178cf492018-11-13 16:34:13 -0500340 clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
341 sizeof (headers->ip6.src_address));
342 clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
343 sizeof (headers->ip6.dst_address));
Klement Sekera239790f2017-02-16 10:53:53 +0100344 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
345 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100346
347 /* fix ip payload length and udp length */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100348 const u16 udp_length =
Klement Sekera402ed312017-01-18 09:44:36 +0100349 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
350 headers->udp.length = clib_host_to_net_u16 (udp_length);
351 headers->ip6.payload_length = headers->udp.length;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100352
353 /* IPv6 UDP checksum is mandatory */
354 int bogus = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100355 headers->udp.checksum =
356 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100357 ASSERT (bogus == 0);
Klement Sekera402ed312017-01-18 09:44:36 +0100358 if (headers->udp.checksum == 0)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100359 {
Klement Sekera402ed312017-01-18 09:44:36 +0100360 headers->udp.checksum = 0xffff;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100361 }
Klement Sekera239790f2017-02-16 10:53:53 +0100362 return 1;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200363}
364
Klement Sekerae50e8562017-04-04 16:19:48 +0200365static void
366bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
367{
368 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
369 u32 *to_next = vlib_frame_vector_args (f);
370 to_next[0] = bi;
371 f->n_vectors = 1;
372 vlib_put_frame_to_node (vm, next_node, f);
373}
374
375int
376bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
377{
378 const bfd_udp_session_t *bus = &bs->udp;
379 ip_adjacency_t *adj = adj_get (bus->adj_index);
380 switch (adj->lookup_next_index)
381 {
382 case IP_LOOKUP_NEXT_ARP:
383 switch (bs->transport)
384 {
385 case BFD_TRANSPORT_UDP4:
386 *next_node = bfd_udp_main.ip4_arp_idx;
387 return 1;
388 case BFD_TRANSPORT_UDP6:
389 *next_node = bfd_udp_main.ip6_ndp_idx;
390 return 1;
391 }
392 break;
393 case IP_LOOKUP_NEXT_REWRITE:
394 switch (bs->transport)
395 {
396 case BFD_TRANSPORT_UDP4:
397 *next_node = bfd_udp_main.ip4_rewrite_idx;
398 return 1;
399 case BFD_TRANSPORT_UDP6:
400 *next_node = bfd_udp_main.ip6_rewrite_idx;
401 return 1;
402 }
403 break;
404 default:
405 /* drop */
406 break;
407 }
408 return 0;
409}
410
411int
412bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
413{
414 u32 next_node;
415 int rv = bfd_udp_calc_next_node (bs, &next_node);
416 if (rv)
417 {
418 bfd_create_frame_to_next_node (vm, bi, next_node);
419 }
420 return rv;
421}
422
423int
424bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
425{
426 u32 next_node;
427 int rv = bfd_udp_calc_next_node (bs, &next_node);
428 if (rv)
429 {
430 bfd_create_frame_to_next_node (vm, bi, next_node);
431 }
432 return 1;
433}
434
Klement Sekerac5fccc02017-01-18 09:56:00 +0100435static bfd_session_t *
436bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200437{
438 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
439 if (p)
440 {
441 return bfd_find_session_by_idx (bum->bfd_main, *p);
442 }
443 return 0;
444}
445
Klement Sekerab17dd962017-01-09 07:43:48 +0100446static void
447bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
448 const ip46_address_t * local_addr,
449 const ip46_address_t * peer_addr)
450{
Dave Barachb7b92992018-10-17 10:38:51 -0400451 clib_memset (key, 0, sizeof (*key));
Klement Sekerab17dd962017-01-09 07:43:48 +0100452 key->sw_if_index = sw_if_index;
453 key->local_addr.as_u64[0] = local_addr->as_u64[0];
454 key->local_addr.as_u64[1] = local_addr->as_u64[1];
455 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
456 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
457}
458
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200459static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100460bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100461 u32 desired_min_tx_usec,
462 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100463 const ip46_address_t * local_addr,
464 const ip46_address_t * peer_addr,
Klement Sekerab17dd962017-01-09 07:43:48 +0100465 bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200466{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200467 /* get a pool entry and if we end up not needing it, give it back */
Klement Sekera239790f2017-02-16 10:53:53 +0100468 bfd_transport_e t = BFD_TRANSPORT_UDP4;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200469 if (!ip46_address_is_ip4 (local_addr))
470 {
471 t = BFD_TRANSPORT_UDP6;
472 }
473 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
Klement Sekera239790f2017-02-16 10:53:53 +0100474 if (!bs)
475 {
Klement Sekera239790f2017-02-16 10:53:53 +0100476 return VNET_API_ERROR_BFD_EAGAIN;
477 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200478 bfd_udp_session_t *bus = &bs->udp;
Dave Barachb7b92992018-10-17 10:38:51 -0400479 clib_memset (bus, 0, sizeof (*bus));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200480 bfd_udp_key_t *key = &bus->key;
Klement Sekerab17dd962017-01-09 07:43:48 +0100481 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200482 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
483 if (tmp)
484 {
Damjan Marion07a38572018-01-21 06:44:18 -0800485 vlib_log_err (bum->log_class,
486 "duplicate bfd-udp session, existing bs_idx=%d",
Klement Sekerab17dd962017-01-09 07:43:48 +0100487 tmp->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200488 bfd_put_session (bum->bfd_main, bs);
489 return VNET_API_ERROR_BFD_EEXIST;
490 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200491 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
492 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100493 bs->bs_idx, key->sw_if_index, format_ip46_address,
494 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
495 &key->peer_addr, IP46_TYPE_ANY);
Damjan Marion07a38572018-01-21 06:44:18 -0800496 vlib_log_info (bum->log_class, "create BFD session: %U",
497 format_bfd_session, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200498 if (BFD_TRANSPORT_UDP4 == t)
499 {
500 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100501 &key->peer_addr,
502 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200503 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100504 "returns %d", format_ip46_address, &key->peer_addr,
505 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200506 }
507 else
508 {
509 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100510 &key->peer_addr,
511 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200512 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100513 "returns %d", format_ip46_address, &key->peer_addr,
514 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200515 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100516 *bs_out = bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100517 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
518 required_min_rx_usec, detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200519}
520
521static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100522bfd_udp_validate_api_input (u32 sw_if_index,
523 const ip46_address_t * local_addr,
524 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200525{
Damjan Marion07a38572018-01-21 06:44:18 -0800526 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200527 vnet_sw_interface_t *sw_if =
Klement Sekera239790f2017-02-16 10:53:53 +0100528 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200529 u8 local_ip_valid = 0;
530 ip_interface_address_t *ia = NULL;
531 if (!sw_if)
532 {
Damjan Marion07a38572018-01-21 06:44:18 -0800533 vlib_log_err (bum->log_class,
534 "got NULL sw_if when getting interface by index %u",
535 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200536 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
537 }
538 if (ip46_address_is_ip4 (local_addr))
539 {
540 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100541 {
Damjan Marion07a38572018-01-21 06:44:18 -0800542 vlib_log_err (bum->log_class,
543 "IP family mismatch (local is ipv4, peer is ipv6)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100544 return VNET_API_ERROR_INVALID_ARGUMENT;
545 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200546 ip4_main_t *im = &ip4_main;
547
548 /* *INDENT-OFF* */
549 foreach_ip_interface_address (
550 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
551 ip4_address_t *x =
552 ip_interface_address_get_address (&im->lookup_main, ia);
553 if (x->as_u32 == local_addr->ip4.as_u32)
554 {
555 /* valid address for this interface */
556 local_ip_valid = 1;
557 break;
558 }
559 }));
560 /* *INDENT-ON* */
561 }
562 else
563 {
564 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100565 {
Damjan Marion07a38572018-01-21 06:44:18 -0800566 vlib_log_err (bum->log_class,
567 "IP family mismatch (local is ipv6, peer is ipv4)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100568 return VNET_API_ERROR_INVALID_ARGUMENT;
569 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200570 ip6_main_t *im = &ip6_main;
571 /* *INDENT-OFF* */
572 foreach_ip_interface_address (
573 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
574 ip6_address_t *x =
575 ip_interface_address_get_address (&im->lookup_main, ia);
576 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
Klement Sekerab17dd962017-01-09 07:43:48 +0100577 local_addr->ip6.as_u64[1] == x->as_u64[1])
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200578 {
579 /* valid address for this interface */
580 local_ip_valid = 1;
581 break;
582 }
583 }));
584 /* *INDENT-ON* */
585 }
586
587 if (!local_ip_valid)
588 {
Damjan Marion07a38572018-01-21 06:44:18 -0800589 vlib_log_err (bum->log_class,
590 "local address %U not found on interface with index %u",
Jon Loeliger6732aea2018-05-05 16:11:31 -0500591 format_ip46_address, local_addr, IP46_TYPE_ANY,
Damjan Marion07a38572018-01-21 06:44:18 -0800592 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200593 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
594 }
595
596 return 0;
597}
598
Klement Sekerab17dd962017-01-09 07:43:48 +0100599static vnet_api_error_t
600bfd_udp_find_session_by_api_input (u32 sw_if_index,
601 const ip46_address_t * local_addr,
602 const ip46_address_t * peer_addr,
603 bfd_session_t ** bs_out)
604{
605 vnet_api_error_t rv =
606 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
607 if (!rv)
608 {
609 bfd_udp_main_t *bum = &bfd_udp_main;
610 bfd_udp_key_t key;
611 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
612 bfd_session_t *bs = bfd_lookup_session (bum, &key);
613 if (bs)
614 {
615 *bs_out = bs;
616 }
617 else
618 {
Damjan Marion07a38572018-01-21 06:44:18 -0800619 vlib_log_err (bum->log_class,
620 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
621 sw_if_index, format_ip46_address, local_addr,
622 IP46_TYPE_ANY, format_ip46_address, peer_addr,
623 IP46_TYPE_ANY);
Klement Sekerab17dd962017-01-09 07:43:48 +0100624 return VNET_API_ERROR_BFD_ENOENT;
625 }
626 }
627 return rv;
628}
629
630static vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100631bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
632 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerab17dd962017-01-09 07:43:48 +0100633 const ip46_address_t * local_addr,
634 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200635{
Damjan Marion07a38572018-01-21 06:44:18 -0800636 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200637 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100638 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200639 if (rv)
640 {
641 return rv;
642 }
643 if (detect_mult < 1)
644 {
Damjan Marion07a38572018-01-21 06:44:18 -0800645 vlib_log_err (bum->log_class, "detect_mult < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200646 return VNET_API_ERROR_INVALID_ARGUMENT;
647 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100648 if (desired_min_tx_usec < 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200649 {
Damjan Marion07a38572018-01-21 06:44:18 -0800650 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200651 return VNET_API_ERROR_INVALID_ARGUMENT;
652 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100653 return 0;
654}
655
656static void
657bfd_udp_del_session_internal (bfd_session_t * bs)
658{
659 bfd_udp_main_t *bum = &bfd_udp_main;
660 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
661 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
662 adj_unlock (bs->udp.adj_index);
663 bfd_put_session (bum->bfd_main, bs);
664}
665
666vnet_api_error_t
667bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100668 const ip46_address_t * peer_addr,
669 u32 desired_min_tx_usec, u32 required_min_rx_usec,
670 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
671 u8 bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100672{
Dave Barach1e3417f2018-07-25 08:30:27 -0400673 bfd_main_t *bm = &bfd_main;
674 bfd_lock (bm);
675
Klement Sekeraa57a9702017-02-02 06:58:07 +0100676 vnet_api_error_t rv =
677 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
678 required_min_rx_usec, detect_mult,
679 local_addr, peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +0100680 bfd_session_t *bs = NULL;
681 if (!rv)
682 {
683 rv =
684 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100685 desired_min_tx_usec,
686 required_min_rx_usec, detect_mult,
687 local_addr, peer_addr, &bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100688 }
689 if (!rv && is_authenticated)
690 {
691#if WITH_LIBSSL > 0
692 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
693 0 /* is not delayed */ );
694#else
Damjan Marion07a38572018-01-21 06:44:18 -0800695 vlib_log_err (bfd_udp_main.log_class,
696 "SSL missing, cannot add authenticated BFD session");
Klement Sekerab17dd962017-01-09 07:43:48 +0100697 rv = VNET_API_ERROR_BFD_NOTSUPP;
698#endif
699 if (rv)
700 {
701 bfd_udp_del_session_internal (bs);
702 }
703 }
704 if (!rv)
705 {
706 bfd_session_start (bfd_udp_main.bfd_main, bs);
707 }
708
Dave Barach1e3417f2018-07-25 08:30:27 -0400709 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100710 return rv;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200711}
712
Klement Sekerac5fccc02017-01-18 09:56:00 +0100713vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100714bfd_udp_mod_session (u32 sw_if_index,
715 const ip46_address_t * local_addr,
716 const ip46_address_t * peer_addr,
717 u32 desired_min_tx_usec,
718 u32 required_min_rx_usec, u8 detect_mult)
719{
720 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400721 bfd_main_t *bm = &bfd_main;
722 vnet_api_error_t error;
723 bfd_lock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100724 vnet_api_error_t rv =
725 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
726 &bs);
727 if (rv)
728 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400729 bfd_unlock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100730 return rv;
731 }
732
Dave Barach1e3417f2018-07-25 08:30:27 -0400733 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
734 desired_min_tx_usec, required_min_rx_usec,
735 detect_mult);
736 bfd_unlock (bm);
737 return error;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100738}
739
740vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100741bfd_udp_del_session (u32 sw_if_index,
742 const ip46_address_t * local_addr,
743 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200744{
Klement Sekerab17dd962017-01-09 07:43:48 +0100745 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400746 bfd_main_t *bm = &bfd_main;
747 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200748 vnet_api_error_t rv =
Klement Sekerab17dd962017-01-09 07:43:48 +0100749 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
750 &bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200751 if (rv)
752 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400753 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200754 return rv;
755 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100756 bfd_udp_del_session_internal (bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400757 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100758 return 0;
759}
760
761vnet_api_error_t
762bfd_udp_session_set_flags (u32 sw_if_index,
763 const ip46_address_t * local_addr,
764 const ip46_address_t * peer_addr, u8 admin_up_down)
765{
766 bfd_session_t *bs = NULL;
Dave Barach1e3417f2018-07-25 08:30:27 -0400767 bfd_main_t *bm = &bfd_main;
768 bfd_lock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100769 vnet_api_error_t rv =
770 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
771 &bs);
772 if (rv)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200773 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400774 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100775 return rv;
776 }
777 bfd_session_set_flags (bs, admin_up_down);
Dave Barach1e3417f2018-07-25 08:30:27 -0400778 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100779 return 0;
780}
781
782vnet_api_error_t
Klement Sekerab17dd962017-01-09 07:43:48 +0100783bfd_udp_auth_activate (u32 sw_if_index,
784 const ip46_address_t * local_addr,
785 const ip46_address_t * peer_addr,
786 u32 conf_key_id, u8 key_id, u8 is_delayed)
787{
Dave Barach1e3417f2018-07-25 08:30:27 -0400788 bfd_main_t *bm = &bfd_main;
789 bfd_lock (bm);
790 vnet_api_error_t error;
791
Klement Sekerab17dd962017-01-09 07:43:48 +0100792#if WITH_LIBSSL > 0
793 bfd_session_t *bs = NULL;
794 vnet_api_error_t rv =
795 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
796 &bs);
797 if (rv)
798 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400799 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100800 return rv;
801 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400802 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
803 bfd_unlock (bm);
804 return error;
Klement Sekerab17dd962017-01-09 07:43:48 +0100805#else
Damjan Marion07a38572018-01-21 06:44:18 -0800806 vlib_log_err (bfd_udp_main->log_class,
807 "SSL missing, cannot activate BFD authentication");
Dave Barach1e3417f2018-07-25 08:30:27 -0400808 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100809 return VNET_API_ERROR_BFD_NOTSUPP;
810#endif
811}
812
813vnet_api_error_t
814bfd_udp_auth_deactivate (u32 sw_if_index,
815 const ip46_address_t * local_addr,
816 const ip46_address_t * peer_addr, u8 is_delayed)
817{
Dave Barach1e3417f2018-07-25 08:30:27 -0400818 bfd_main_t *bm = &bfd_main;
819 vnet_api_error_t error;
820 bfd_lock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100821 bfd_session_t *bs = NULL;
822 vnet_api_error_t rv =
823 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
824 &bs);
825 if (rv)
826 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400827 bfd_unlock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +0100828 return rv;
829 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400830 error = bfd_auth_deactivate (bs, is_delayed);
831 bfd_unlock (bm);
832 return error;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200833}
834
Klement Sekerac5fccc02017-01-18 09:56:00 +0100835typedef enum
836{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200837 BFD_UDP_INPUT_NEXT_NORMAL,
Klement Sekerae50e8562017-04-04 16:19:48 +0200838 BFD_UDP_INPUT_NEXT_REPLY_ARP,
839 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200840 BFD_UDP_INPUT_N_NEXT,
841} bfd_udp_input_next_t;
842
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100843/* Packet counters - BFD control frames */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200844#define foreach_bfd_udp_error(F) \
845 F (NONE, "good bfd packets (processed)") \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100846 F (BAD, "invalid bfd packets")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200847
848#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
849foreach_bfd_udp_error (F);
850#undef F
851
852static char *bfd_udp_error_strings[] = {
853#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
854 foreach_bfd_udp_error (F)
855#undef F
856};
857
Klement Sekerac5fccc02017-01-18 09:56:00 +0100858typedef enum
859{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200860#define F(sym, str) BFD_UDP_ERROR_##sym,
861 foreach_bfd_udp_error (F)
862#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100863 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200864} bfd_udp_error_t;
865
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100866/* Packet counters - BFD ECHO packets */
867#define foreach_bfd_udp_echo_error(F) \
868 F (NONE, "good bfd echo packets (processed)") \
869 F (BAD, "invalid bfd echo packets")
870
871#define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
872foreach_bfd_udp_echo_error (F);
873#undef F
874
875static char *bfd_udp_echo_error_strings[] = {
876#define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
877 foreach_bfd_udp_echo_error (F)
878#undef F
879};
880
881typedef enum
882{
883#define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
884 foreach_bfd_udp_echo_error (F)
885#undef F
886 BFD_UDP_ECHO_N_ERROR,
887} bfd_udp_echo_error_t;
888
Klement Sekerac5fccc02017-01-18 09:56:00 +0100889static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100890bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
891 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200892{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100893 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +0200894 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera0c1519b2016-12-08 05:03:32 +0100895 if (start < 0 && start < sizeof (b->pre_data))
896 {
897 BFD_ERR ("Start of ip header is before pre_data, ignoring");
898 *ip4 = NULL;
899 *udp = NULL;
900 return;
901 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100902 *ip4 = (ip4_header_t *) (b->data + start);
903 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100904 {
905 BFD_ERR ("Start of ip header is beyond current data, ignoring");
906 *ip4 = NULL;
907 *udp = NULL;
908 return;
909 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100910 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200911}
912
Klement Sekerac5fccc02017-01-18 09:56:00 +0100913static bfd_udp_error_t
914bfd_udp4_verify_transport (const ip4_header_t * ip4,
915 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200916{
917 const bfd_udp_session_t *bus = &bs->udp;
918 const bfd_udp_key_t *key = &bus->key;
919 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
920 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100921 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100922 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
923 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200924 return BFD_UDP_ERROR_BAD;
925 }
926 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
927 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100928 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100929 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
930 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200931 return BFD_UDP_ERROR_BAD;
932 }
933 const u8 expected_ttl = 255;
934 if (ip4->ttl != expected_ttl)
935 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100936 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100937 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200938 return BFD_UDP_ERROR_BAD;
939 }
Klement Sekera6f966492017-02-08 07:42:08 +0100940 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200941 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100942 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100943 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200944 }
945 return BFD_UDP_ERROR_NONE;
946}
947
948typedef struct
949{
950 u32 bs_idx;
951 bfd_pkt_t pkt;
952} bfd_rpc_update_t;
953
Klement Sekerac5fccc02017-01-18 09:56:00 +0100954static void
Klement Sekerac5fccc02017-01-18 09:56:00 +0100955bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200956{
Dave Barach1e3417f2018-07-25 08:30:27 -0400957 bfd_main_t *bm = &bfd_main;
958 bfd_lock (bm);
959 bfd_consume_pkt (bm, pkt, bs_idx);
960 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200961}
962
Klement Sekerac5fccc02017-01-18 09:56:00 +0100963static bfd_udp_error_t
964bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
965 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200966{
967 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
968 if (sizeof (*pkt) > b->current_length)
969 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100970 BFD_ERR
971 ("Payload size %d too small to hold bfd packet of minimum size %d",
972 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200973 return BFD_UDP_ERROR_BAD;
974 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100975 ip4_header_t *ip4;
976 udp_header_t *udp;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200977 bfd_udp4_find_headers (b, &ip4, &udp);
978 if (!ip4 || !udp)
979 {
980 BFD_ERR ("Couldn't find ip4 or udp header");
981 return BFD_UDP_ERROR_BAD;
982 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100983 const u32 udp_payload_length = udp->length - sizeof (*udp);
984 if (pkt->head.length > udp_payload_length)
985 {
986 BFD_ERR
987 ("BFD packet length is larger than udp payload length (%u > %u)",
988 pkt->head.length, udp_payload_length);
989 return BFD_UDP_ERROR_BAD;
990 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200991 if (!bfd_verify_pkt_common (pkt))
992 {
993 return BFD_UDP_ERROR_BAD;
994 }
995 bfd_session_t *bs = NULL;
996 if (pkt->your_disc)
997 {
998 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100999 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001000 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1001 }
1002 else
1003 {
1004 bfd_udp_key_t key;
Dave Barachb7b92992018-10-17 10:38:51 -04001005 clib_memset (&key, 0, sizeof (key));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001006 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1007 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1008 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1009 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +01001010 "peer=%U)",
1011 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1012 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001013 bs = bfd_lookup_session (&bfd_udp_main, &key);
1014 }
1015 if (!bs)
1016 {
1017 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1018 return BFD_UDP_ERROR_BAD;
1019 }
Klement Sekera637b9c42016-12-08 05:19:14 +01001020 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001021 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001022 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001023 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001024 return BFD_UDP_ERROR_BAD;
1025 }
1026 bfd_udp_error_t err;
1027 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1028 {
1029 return err;
1030 }
1031 bfd_rpc_update_session (bs->bs_idx, pkt);
1032 *bs_out = bs;
1033 return BFD_UDP_ERROR_NONE;
1034}
1035
Klement Sekerac5fccc02017-01-18 09:56:00 +01001036static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001037bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1038 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001039{
Klement Sekera46a87ad2017-01-02 08:22:23 +01001040 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +02001041 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001042 if (start < 0 && start < sizeof (b->pre_data))
1043 {
1044 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1045 *ip6 = NULL;
1046 *udp = NULL;
1047 return;
1048 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001049 *ip6 = (ip6_header_t *) (b->data + start);
1050 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001051 {
1052 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1053 *ip6 = NULL;
1054 *udp = NULL;
1055 return;
1056 }
Klement Sekera402ed312017-01-18 09:44:36 +01001057 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1058 {
1059 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
Klement Sekerab17dd962017-01-09 07:43:48 +01001060 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
Klement Sekera402ed312017-01-18 09:44:36 +01001061 *ip6 = NULL;
1062 *udp = NULL;
1063 return;
1064 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001065 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001066}
1067
Klement Sekerac5fccc02017-01-18 09:56:00 +01001068static bfd_udp_error_t
1069bfd_udp6_verify_transport (const ip6_header_t * ip6,
1070 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001071{
1072 const bfd_udp_session_t *bus = &bs->udp;
1073 const bfd_udp_key_t *key = &bus->key;
1074 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1075 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1076 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001077 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1078 format_ip6_address, ip6, format_ip6_address,
1079 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001080 return BFD_UDP_ERROR_BAD;
1081 }
1082 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1083 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1084 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001085 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1086 format_ip6_address, ip6, format_ip6_address,
1087 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001088 return BFD_UDP_ERROR_BAD;
1089 }
1090 const u8 expected_hop_limit = 255;
1091 if (ip6->hop_limit != expected_hop_limit)
1092 {
1093 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001094 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001095 return BFD_UDP_ERROR_BAD;
1096 }
Klement Sekera6f966492017-02-08 07:42:08 +01001097 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001098 {
1099 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001100 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001101 }
1102 return BFD_UDP_ERROR_NONE;
1103}
1104
Klement Sekerac5fccc02017-01-18 09:56:00 +01001105static bfd_udp_error_t
1106bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1107 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001108{
1109 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1110 if (sizeof (*pkt) > b->current_length)
1111 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001112 BFD_ERR
1113 ("Payload size %d too small to hold bfd packet of minimum size %d",
1114 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001115 return BFD_UDP_ERROR_BAD;
1116 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001117 ip6_header_t *ip6;
1118 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001119 bfd_udp6_find_headers (b, &ip6, &udp);
1120 if (!ip6 || !udp)
1121 {
1122 BFD_ERR ("Couldn't find ip6 or udp header");
1123 return BFD_UDP_ERROR_BAD;
1124 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001125 const u32 udp_payload_length = udp->length - sizeof (*udp);
1126 if (pkt->head.length > udp_payload_length)
1127 {
1128 BFD_ERR
1129 ("BFD packet length is larger than udp payload length (%u > %u)",
1130 pkt->head.length, udp_payload_length);
1131 return BFD_UDP_ERROR_BAD;
1132 }
Klement Sekera46a87ad2017-01-02 08:22:23 +01001133 if (!bfd_verify_pkt_common (pkt))
1134 {
1135 return BFD_UDP_ERROR_BAD;
1136 }
1137 bfd_session_t *bs = NULL;
1138 if (pkt->your_disc)
1139 {
1140 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001141 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001142 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1143 }
1144 else
1145 {
1146 bfd_udp_key_t key;
Dave Barachb7b92992018-10-17 10:38:51 -04001147 clib_memset (&key, 0, sizeof (key));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001148 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1149 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1150 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1151 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1152 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1153 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerab17dd962017-01-09 07:43:48 +01001154 "peer=%U)",
1155 key.sw_if_index, format_ip6_address, &key.local_addr,
1156 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001157 bs = bfd_lookup_session (&bfd_udp_main, &key);
1158 }
1159 if (!bs)
1160 {
1161 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1162 return BFD_UDP_ERROR_BAD;
1163 }
1164 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001165 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001166 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001167 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera46a87ad2017-01-02 08:22:23 +01001168 return BFD_UDP_ERROR_BAD;
1169 }
1170 bfd_udp_error_t err;
1171 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1172 {
1173 return err;
1174 }
1175 bfd_rpc_update_session (bs->bs_idx, pkt);
1176 *bs_out = bs;
1177 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001178}
1179
1180/*
1181 * Process a frame of bfd packets
1182 * Expect 1 packet / frame
1183 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001184static uword
1185bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1186 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001187{
1188 u32 n_left_from, *from;
1189 bfd_input_trace_t *t0;
Dave Barach1e3417f2018-07-25 08:30:27 -04001190 bfd_main_t *bm = &bfd_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001191
Klement Sekerac5fccc02017-01-18 09:56:00 +01001192 from = vlib_frame_vector_args (f); /* array of buffer indices */
1193 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001194
1195 while (n_left_from > 0)
1196 {
1197 u32 bi0;
1198 vlib_buffer_t *b0;
1199 u32 next0, error0;
1200
1201 bi0 = from[0];
1202 b0 = vlib_get_buffer (vm, bi0);
1203
1204 bfd_session_t *bs = NULL;
1205
1206 /* If this pkt is traced, snapshot the data */
1207 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001208 {
1209 int len;
1210 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1211 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1212 : sizeof (t0->data);
1213 t0->len = len;
Dave Barach178cf492018-11-13 16:34:13 -05001214 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001215 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001216
1217 /* scan this bfd pkt. error0 is the counter index to bmp */
Dave Barach1e3417f2018-07-25 08:30:27 -04001218 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001219 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001220 {
1221 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1222 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001223 else
Klement Sekerac5fccc02017-01-18 09:56:00 +01001224 {
1225 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1226 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001227 b0->error = rt->errors[error0];
1228
1229 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1230 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001231 {
Klement Sekera402ed312017-01-18 09:44:36 +01001232 /*
1233 * if everything went fine, check for poll bit, if present, re-use
1234 * the buffer and based on (now updated) session parameters, send
1235 * the final packet back
1236 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001237 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1238 if (bfd_pkt_get_poll (pkt))
1239 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001240 b0->current_data = 0;
1241 b0->current_length = 0;
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001242 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1243 0);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001244 if (is_ipv6)
1245 {
1246 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1247 b0->error, 1);
1248 }
1249 else
1250 {
1251 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1252 b0->error, 1);
1253 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001254 const bfd_udp_session_t *bus = &bs->udp;
1255 ip_adjacency_t *adj = adj_get (bus->adj_index);
1256 switch (adj->lookup_next_index)
1257 {
1258 case IP_LOOKUP_NEXT_ARP:
1259 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1260 break;
1261 case IP_LOOKUP_NEXT_REWRITE:
1262 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1263 break;
1264 default:
1265 /* drop */
1266 break;
1267 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001268 }
1269 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001270 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001271 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1272
1273 from += 1;
1274 n_left_from -= 1;
1275 }
1276
1277 return f->n_vectors;
1278}
1279
Klement Sekerac5fccc02017-01-18 09:56:00 +01001280static uword
1281bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001282{
1283 return bfd_udp_input (vm, rt, f, 0);
1284}
1285
1286/*
1287 * bfd input graph node declaration
1288 */
1289/* *INDENT-OFF* */
1290VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1291 .function = bfd_udp4_input,
1292 .name = "bfd-udp4-input",
1293 .vector_size = sizeof (u32),
1294 .type = VLIB_NODE_TYPE_INTERNAL,
1295
1296 .n_errors = BFD_UDP_N_ERROR,
1297 .error_strings = bfd_udp_error_strings,
1298
1299 .format_trace = bfd_input_format_trace,
1300
1301 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1302 .next_nodes =
1303 {
1304 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001305 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1306 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001307 },
1308};
1309/* *INDENT-ON* */
1310
Klement Sekerac5fccc02017-01-18 09:56:00 +01001311static uword
1312bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001313{
1314 return bfd_udp_input (vm, rt, f, 1);
1315}
1316
1317/* *INDENT-OFF* */
1318VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1319 .function = bfd_udp6_input,
1320 .name = "bfd-udp6-input",
1321 .vector_size = sizeof (u32),
1322 .type = VLIB_NODE_TYPE_INTERNAL,
1323
1324 .n_errors = BFD_UDP_N_ERROR,
1325 .error_strings = bfd_udp_error_strings,
1326
1327 .format_trace = bfd_input_format_trace,
1328
1329 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1330 .next_nodes =
1331 {
1332 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001333 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1334 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001335 },
1336};
1337/* *INDENT-ON* */
1338
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001339/*
1340 * Process a frame of bfd echo packets
1341 * Expect 1 packet / frame
1342 */
1343static uword
1344bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1345 vlib_frame_t * f, int is_ipv6)
1346{
1347 u32 n_left_from, *from;
1348 bfd_input_trace_t *t0;
Dave Barach1e3417f2018-07-25 08:30:27 -04001349 bfd_main_t *bm = &bfd_main;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001350
1351 from = vlib_frame_vector_args (f); /* array of buffer indices */
1352 n_left_from = f->n_vectors; /* number of buffer indices */
1353
1354 while (n_left_from > 0)
1355 {
1356 u32 bi0;
1357 vlib_buffer_t *b0;
1358 u32 next0;
1359
1360 bi0 = from[0];
1361 b0 = vlib_get_buffer (vm, bi0);
1362
1363 /* If this pkt is traced, snapshot the data */
1364 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1365 {
1366 int len;
1367 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1368 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1369 : sizeof (t0->data);
1370 t0->len = len;
Dave Barach178cf492018-11-13 16:34:13 -05001371 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001372 }
1373
Dave Barach1e3417f2018-07-25 08:30:27 -04001374 bfd_lock (bm);
Klement Sekera239790f2017-02-16 10:53:53 +01001375 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1376 {
1377 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1378 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1379 }
1380 else
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001381 {
1382 /* loop back the packet */
1383 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1384 if (is_ipv6)
1385 {
1386 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1387 b0->error, 1);
1388 }
1389 else
1390 {
1391 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1392 b0->error, 1);
1393 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001394 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001395 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001396
Dave Barach1e3417f2018-07-25 08:30:27 -04001397 bfd_unlock (bm);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001398 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1399
1400 from += 1;
1401 n_left_from -= 1;
1402 }
1403
1404 return f->n_vectors;
1405}
1406
1407static uword
1408bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1409 vlib_frame_t * f)
1410{
1411 return bfd_udp_echo_input (vm, rt, f, 0);
1412}
1413
1414u8 *
1415bfd_echo_input_format_trace (u8 * s, va_list * args)
1416{
1417 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1418 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1419 const bfd_udp_echo_input_trace_t *t =
1420 va_arg (*args, bfd_udp_echo_input_trace_t *);
1421 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1422 {
1423 s = format (s, "BFD ECHO:\n");
1424 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1425 }
1426
1427 return s;
1428}
1429
1430/*
1431 * bfd input graph node declaration
1432 */
1433/* *INDENT-OFF* */
1434VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1435 .function = bfd_udp_echo4_input,
1436 .name = "bfd-udp-echo4-input",
1437 .vector_size = sizeof (u32),
1438 .type = VLIB_NODE_TYPE_INTERNAL,
1439
1440 .n_errors = BFD_UDP_ECHO_N_ERROR,
1441 .error_strings = bfd_udp_error_strings,
1442
1443 .format_trace = bfd_echo_input_format_trace,
1444
1445 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1446 .next_nodes =
1447 {
1448 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001449 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1450 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001451 },
1452};
1453/* *INDENT-ON* */
1454
1455static uword
1456bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1457 vlib_frame_t * f)
1458{
1459 return bfd_udp_echo_input (vm, rt, f, 1);
1460}
1461
1462/* *INDENT-OFF* */
1463VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1464 .function = bfd_udp_echo6_input,
1465 .name = "bfd-udp-echo6-input",
1466 .vector_size = sizeof (u32),
1467 .type = VLIB_NODE_TYPE_INTERNAL,
1468
1469 .n_errors = BFD_UDP_ECHO_N_ERROR,
1470 .error_strings = bfd_udp_echo_error_strings,
1471
1472 .format_trace = bfd_echo_input_format_trace,
1473
1474 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1475 .next_nodes =
1476 {
1477 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001478 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1479 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001480 },
1481};
1482
1483/* *INDENT-ON* */
1484
Klement Sekerac5fccc02017-01-18 09:56:00 +01001485static clib_error_t *
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001486bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001487{
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001488 bfd_session_t **to_be_freed = NULL;
Damjan Marion07a38572018-01-21 06:44:18 -08001489 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001490 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1491 is_create);
1492 if (!is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001493 {
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001494 bfd_session_t *bs;
1495 pool_foreach (bs, bfd_udp_main.bfd_main->sessions,
1496 {
1497 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1498 bs->transport != BFD_TRANSPORT_UDP6)
1499 {
1500 continue;}
1501 if (bs->udp.key.sw_if_index != sw_if_index)
1502 {
1503 continue;}
1504 vec_add1 (to_be_freed, bs);}
1505 );
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001506 }
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001507 bfd_session_t **bs;
1508 vec_foreach (bs, to_be_freed)
1509 {
Damjan Marion07a38572018-01-21 06:44:18 -08001510 vlib_log_notice (bum->log_class,
1511 "removal of sw_if_index=%u forces removal of bfd session "
1512 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001513 bfd_session_set_flags (*bs, 0);
1514 bfd_udp_del_session_internal (*bs);
1515 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001516 return 0;
1517}
1518
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001519VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001520
1521/*
1522 * setup function
1523 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001524static clib_error_t *
1525bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001526{
1527 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +01001528 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001529 bfd_udp_main.bfd_main = &bfd_main;
Klement Sekera239790f2017-02-16 10:53:53 +01001530 bfd_udp_main.vnet_main = vnet_get_main ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001531 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1532 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001533 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1534 bfd_udp_echo4_input_node.index, 1);
1535 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1536 bfd_udp_echo6_input_node.index, 0);
Klement Sekerae50e8562017-04-04 16:19:48 +02001537 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1538 ASSERT (node);
1539 bfd_udp_main.ip4_arp_idx = node->index;
1540 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1541 ASSERT (node);
1542 bfd_udp_main.ip6_ndp_idx = node->index;
1543 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1544 ASSERT (node);
1545 bfd_udp_main.ip4_rewrite_idx = node->index;
1546 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1547 ASSERT (node);
1548 bfd_udp_main.ip6_rewrite_idx = node->index;
1549
Damjan Marion07a38572018-01-21 06:44:18 -08001550 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1551 vlib_log_debug (bfd_udp_main.log_class, "initialized");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001552 return 0;
1553}
1554
1555VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001556
1557/*
1558 * fd.io coding-style-patch-verification: ON
1559 *
1560 * Local Variables:
1561 * eval: (c-set-style "gnu")
1562 * End:
1563 */