blob: fa696aa200651757ff7c807254a3e7d0e7074ff6 [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);
264 memset (headers, 0, sizeof (*headers));
265 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);
Klement Sekera402ed312017-01-18 09:44:36 +0100319 memset (headers, 0, sizeof (*headers));
320 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 }
333 clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6,
334 sizeof (headers->ip6.dst_address));
335
336 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
337 }
338 else
339 {
340 clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6,
341 sizeof (headers->ip6.src_address));
342 clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6,
343 sizeof (headers->ip6.dst_address));
344 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{
451 memset (key, 0, sizeof (*key));
452 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 {
476 bfd_put_session (bum->bfd_main, bs);
477 return VNET_API_ERROR_BFD_EAGAIN;
478 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200479 bfd_udp_session_t *bus = &bs->udp;
480 memset (bus, 0, sizeof (*bus));
481 bfd_udp_key_t *key = &bus->key;
Klement Sekerab17dd962017-01-09 07:43:48 +0100482 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200483 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
484 if (tmp)
485 {
Damjan Marion07a38572018-01-21 06:44:18 -0800486 vlib_log_err (bum->log_class,
487 "duplicate bfd-udp session, existing bs_idx=%d",
Klement Sekerab17dd962017-01-09 07:43:48 +0100488 tmp->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200489 bfd_put_session (bum->bfd_main, bs);
490 return VNET_API_ERROR_BFD_EEXIST;
491 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200492 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
493 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100494 bs->bs_idx, key->sw_if_index, format_ip46_address,
495 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
496 &key->peer_addr, IP46_TYPE_ANY);
Damjan Marion07a38572018-01-21 06:44:18 -0800497 vlib_log_info (bum->log_class, "create BFD session: %U",
498 format_bfd_session, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200499 if (BFD_TRANSPORT_UDP4 == t)
500 {
501 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100502 &key->peer_addr,
503 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200504 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100505 "returns %d", format_ip46_address, &key->peer_addr,
506 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200507 }
508 else
509 {
510 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100511 &key->peer_addr,
512 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200513 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100514 "returns %d", format_ip46_address, &key->peer_addr,
515 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200516 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100517 *bs_out = bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100518 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
519 required_min_rx_usec, detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200520}
521
522static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100523bfd_udp_validate_api_input (u32 sw_if_index,
524 const ip46_address_t * local_addr,
525 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200526{
Damjan Marion07a38572018-01-21 06:44:18 -0800527 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200528 vnet_sw_interface_t *sw_if =
Klement Sekera239790f2017-02-16 10:53:53 +0100529 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200530 u8 local_ip_valid = 0;
531 ip_interface_address_t *ia = NULL;
532 if (!sw_if)
533 {
Damjan Marion07a38572018-01-21 06:44:18 -0800534 vlib_log_err (bum->log_class,
535 "got NULL sw_if when getting interface by index %u",
536 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200537 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
538 }
539 if (ip46_address_is_ip4 (local_addr))
540 {
541 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100542 {
Damjan Marion07a38572018-01-21 06:44:18 -0800543 vlib_log_err (bum->log_class,
544 "IP family mismatch (local is ipv4, peer is ipv6)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100545 return VNET_API_ERROR_INVALID_ARGUMENT;
546 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200547 ip4_main_t *im = &ip4_main;
548
549 /* *INDENT-OFF* */
550 foreach_ip_interface_address (
551 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
552 ip4_address_t *x =
553 ip_interface_address_get_address (&im->lookup_main, ia);
554 if (x->as_u32 == local_addr->ip4.as_u32)
555 {
556 /* valid address for this interface */
557 local_ip_valid = 1;
558 break;
559 }
560 }));
561 /* *INDENT-ON* */
562 }
563 else
564 {
565 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100566 {
Damjan Marion07a38572018-01-21 06:44:18 -0800567 vlib_log_err (bum->log_class,
568 "IP family mismatch (local is ipv6, peer is ipv4)");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100569 return VNET_API_ERROR_INVALID_ARGUMENT;
570 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200571 ip6_main_t *im = &ip6_main;
572 /* *INDENT-OFF* */
573 foreach_ip_interface_address (
574 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
575 ip6_address_t *x =
576 ip_interface_address_get_address (&im->lookup_main, ia);
577 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
Klement Sekerab17dd962017-01-09 07:43:48 +0100578 local_addr->ip6.as_u64[1] == x->as_u64[1])
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200579 {
580 /* valid address for this interface */
581 local_ip_valid = 1;
582 break;
583 }
584 }));
585 /* *INDENT-ON* */
586 }
587
588 if (!local_ip_valid)
589 {
Damjan Marion07a38572018-01-21 06:44:18 -0800590 vlib_log_err (bum->log_class,
591 "local address %U not found on interface with index %u",
Jon Loeliger6732aea2018-05-05 16:11:31 -0500592 format_ip46_address, local_addr, IP46_TYPE_ANY,
Damjan Marion07a38572018-01-21 06:44:18 -0800593 sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200594 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
595 }
596
597 return 0;
598}
599
Klement Sekerab17dd962017-01-09 07:43:48 +0100600static vnet_api_error_t
601bfd_udp_find_session_by_api_input (u32 sw_if_index,
602 const ip46_address_t * local_addr,
603 const ip46_address_t * peer_addr,
604 bfd_session_t ** bs_out)
605{
606 vnet_api_error_t rv =
607 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
608 if (!rv)
609 {
610 bfd_udp_main_t *bum = &bfd_udp_main;
611 bfd_udp_key_t key;
612 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
613 bfd_session_t *bs = bfd_lookup_session (bum, &key);
614 if (bs)
615 {
616 *bs_out = bs;
617 }
618 else
619 {
Damjan Marion07a38572018-01-21 06:44:18 -0800620 vlib_log_err (bum->log_class,
621 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
622 sw_if_index, format_ip46_address, local_addr,
623 IP46_TYPE_ANY, format_ip46_address, peer_addr,
624 IP46_TYPE_ANY);
Klement Sekerab17dd962017-01-09 07:43:48 +0100625 return VNET_API_ERROR_BFD_ENOENT;
626 }
627 }
628 return rv;
629}
630
631static vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100632bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
633 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerab17dd962017-01-09 07:43:48 +0100634 const ip46_address_t * local_addr,
635 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200636{
Damjan Marion07a38572018-01-21 06:44:18 -0800637 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200638 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100639 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200640 if (rv)
641 {
642 return rv;
643 }
644 if (detect_mult < 1)
645 {
Damjan Marion07a38572018-01-21 06:44:18 -0800646 vlib_log_err (bum->log_class, "detect_mult < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200647 return VNET_API_ERROR_INVALID_ARGUMENT;
648 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100649 if (desired_min_tx_usec < 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200650 {
Damjan Marion07a38572018-01-21 06:44:18 -0800651 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200652 return VNET_API_ERROR_INVALID_ARGUMENT;
653 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100654 return 0;
655}
656
657static void
658bfd_udp_del_session_internal (bfd_session_t * bs)
659{
660 bfd_udp_main_t *bum = &bfd_udp_main;
661 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
662 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
663 adj_unlock (bs->udp.adj_index);
664 bfd_put_session (bum->bfd_main, bs);
665}
666
667vnet_api_error_t
668bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100669 const ip46_address_t * peer_addr,
670 u32 desired_min_tx_usec, u32 required_min_rx_usec,
671 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
672 u8 bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100673{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100674 vnet_api_error_t rv =
675 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
676 required_min_rx_usec, detect_mult,
677 local_addr, peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +0100678 bfd_session_t *bs = NULL;
679 if (!rv)
680 {
681 rv =
682 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100683 desired_min_tx_usec,
684 required_min_rx_usec, detect_mult,
685 local_addr, peer_addr, &bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100686 }
687 if (!rv && is_authenticated)
688 {
689#if WITH_LIBSSL > 0
690 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
691 0 /* is not delayed */ );
692#else
Damjan Marion07a38572018-01-21 06:44:18 -0800693 vlib_log_err (bfd_udp_main.log_class,
694 "SSL missing, cannot add authenticated BFD session");
Klement Sekerab17dd962017-01-09 07:43:48 +0100695 rv = VNET_API_ERROR_BFD_NOTSUPP;
696#endif
697 if (rv)
698 {
699 bfd_udp_del_session_internal (bs);
700 }
701 }
702 if (!rv)
703 {
704 bfd_session_start (bfd_udp_main.bfd_main, bs);
705 }
706
707 return rv;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200708}
709
Klement Sekerac5fccc02017-01-18 09:56:00 +0100710vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100711bfd_udp_mod_session (u32 sw_if_index,
712 const ip46_address_t * local_addr,
713 const ip46_address_t * peer_addr,
714 u32 desired_min_tx_usec,
715 u32 required_min_rx_usec, u8 detect_mult)
716{
717 bfd_session_t *bs = NULL;
718 vnet_api_error_t rv =
719 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
720 &bs);
721 if (rv)
722 {
723 return rv;
724 }
725
726 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
727 desired_min_tx_usec, required_min_rx_usec,
728 detect_mult);
729}
730
731vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100732bfd_udp_del_session (u32 sw_if_index,
733 const ip46_address_t * local_addr,
734 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200735{
Klement Sekerab17dd962017-01-09 07:43:48 +0100736 bfd_session_t *bs = NULL;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200737 vnet_api_error_t rv =
Klement Sekerab17dd962017-01-09 07:43:48 +0100738 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
739 &bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200740 if (rv)
741 {
742 return rv;
743 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100744 bfd_udp_del_session_internal (bs);
745 return 0;
746}
747
748vnet_api_error_t
749bfd_udp_session_set_flags (u32 sw_if_index,
750 const ip46_address_t * local_addr,
751 const ip46_address_t * peer_addr, u8 admin_up_down)
752{
753 bfd_session_t *bs = NULL;
754 vnet_api_error_t rv =
755 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
756 &bs);
757 if (rv)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200758 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100759 return rv;
760 }
761 bfd_session_set_flags (bs, admin_up_down);
762 return 0;
763}
764
765vnet_api_error_t
Klement Sekerab17dd962017-01-09 07:43:48 +0100766bfd_udp_auth_activate (u32 sw_if_index,
767 const ip46_address_t * local_addr,
768 const ip46_address_t * peer_addr,
769 u32 conf_key_id, u8 key_id, u8 is_delayed)
770{
771#if WITH_LIBSSL > 0
772 bfd_session_t *bs = NULL;
773 vnet_api_error_t rv =
774 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
775 &bs);
776 if (rv)
777 {
778 return rv;
779 }
780 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
781#else
Damjan Marion07a38572018-01-21 06:44:18 -0800782 vlib_log_err (bfd_udp_main->log_class,
783 "SSL missing, cannot activate BFD authentication");
Klement Sekerab17dd962017-01-09 07:43:48 +0100784 return VNET_API_ERROR_BFD_NOTSUPP;
785#endif
786}
787
788vnet_api_error_t
789bfd_udp_auth_deactivate (u32 sw_if_index,
790 const ip46_address_t * local_addr,
791 const ip46_address_t * peer_addr, u8 is_delayed)
792{
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 {
799 return rv;
800 }
801 return bfd_auth_deactivate (bs, is_delayed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200802}
803
Klement Sekerac5fccc02017-01-18 09:56:00 +0100804typedef enum
805{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200806 BFD_UDP_INPUT_NEXT_NORMAL,
Klement Sekerae50e8562017-04-04 16:19:48 +0200807 BFD_UDP_INPUT_NEXT_REPLY_ARP,
808 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200809 BFD_UDP_INPUT_N_NEXT,
810} bfd_udp_input_next_t;
811
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100812/* Packet counters - BFD control frames */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200813#define foreach_bfd_udp_error(F) \
814 F (NONE, "good bfd packets (processed)") \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100815 F (BAD, "invalid bfd packets")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200816
817#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
818foreach_bfd_udp_error (F);
819#undef F
820
821static char *bfd_udp_error_strings[] = {
822#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
823 foreach_bfd_udp_error (F)
824#undef F
825};
826
Klement Sekerac5fccc02017-01-18 09:56:00 +0100827typedef enum
828{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200829#define F(sym, str) BFD_UDP_ERROR_##sym,
830 foreach_bfd_udp_error (F)
831#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100832 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200833} bfd_udp_error_t;
834
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100835/* Packet counters - BFD ECHO packets */
836#define foreach_bfd_udp_echo_error(F) \
837 F (NONE, "good bfd echo packets (processed)") \
838 F (BAD, "invalid bfd echo packets")
839
840#define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
841foreach_bfd_udp_echo_error (F);
842#undef F
843
844static char *bfd_udp_echo_error_strings[] = {
845#define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
846 foreach_bfd_udp_echo_error (F)
847#undef F
848};
849
850typedef enum
851{
852#define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
853 foreach_bfd_udp_echo_error (F)
854#undef F
855 BFD_UDP_ECHO_N_ERROR,
856} bfd_udp_echo_error_t;
857
Klement Sekerac5fccc02017-01-18 09:56:00 +0100858static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100859bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
860 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200861{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100862 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +0200863 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera0c1519b2016-12-08 05:03:32 +0100864 if (start < 0 && start < sizeof (b->pre_data))
865 {
866 BFD_ERR ("Start of ip header is before pre_data, ignoring");
867 *ip4 = NULL;
868 *udp = NULL;
869 return;
870 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100871 *ip4 = (ip4_header_t *) (b->data + start);
872 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100873 {
874 BFD_ERR ("Start of ip header is beyond current data, ignoring");
875 *ip4 = NULL;
876 *udp = NULL;
877 return;
878 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100879 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200880}
881
Klement Sekerac5fccc02017-01-18 09:56:00 +0100882static bfd_udp_error_t
883bfd_udp4_verify_transport (const ip4_header_t * ip4,
884 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200885{
886 const bfd_udp_session_t *bus = &bs->udp;
887 const bfd_udp_key_t *key = &bus->key;
888 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
889 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100890 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100891 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
892 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200893 return BFD_UDP_ERROR_BAD;
894 }
895 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
896 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100897 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100898 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
899 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200900 return BFD_UDP_ERROR_BAD;
901 }
902 const u8 expected_ttl = 255;
903 if (ip4->ttl != expected_ttl)
904 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100905 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100906 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200907 return BFD_UDP_ERROR_BAD;
908 }
Klement Sekera6f966492017-02-08 07:42:08 +0100909 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200910 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100911 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100912 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200913 }
914 return BFD_UDP_ERROR_NONE;
915}
916
917typedef struct
918{
919 u32 bs_idx;
920 bfd_pkt_t pkt;
921} bfd_rpc_update_t;
922
Klement Sekerac5fccc02017-01-18 09:56:00 +0100923static void
924bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200925{
926 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
927}
928
Klement Sekerac5fccc02017-01-18 09:56:00 +0100929static void
930bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200931{
932 /* packet length was already verified to be correct by the caller */
933 const u32 data_size = sizeof (bfd_rpc_update_t) -
Klement Sekerac5fccc02017-01-18 09:56:00 +0100934 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200935 u8 data[data_size];
Klement Sekerac5fccc02017-01-18 09:56:00 +0100936 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200937 update->bs_idx = bs_idx;
938 clib_memcpy (&update->pkt, pkt, pkt->head.length);
939 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
940}
941
Klement Sekerac5fccc02017-01-18 09:56:00 +0100942static bfd_udp_error_t
943bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
944 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200945{
946 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
947 if (sizeof (*pkt) > b->current_length)
948 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100949 BFD_ERR
950 ("Payload size %d too small to hold bfd packet of minimum size %d",
951 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200952 return BFD_UDP_ERROR_BAD;
953 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100954 ip4_header_t *ip4;
955 udp_header_t *udp;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200956 bfd_udp4_find_headers (b, &ip4, &udp);
957 if (!ip4 || !udp)
958 {
959 BFD_ERR ("Couldn't find ip4 or udp header");
960 return BFD_UDP_ERROR_BAD;
961 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100962 const u32 udp_payload_length = udp->length - sizeof (*udp);
963 if (pkt->head.length > udp_payload_length)
964 {
965 BFD_ERR
966 ("BFD packet length is larger than udp payload length (%u > %u)",
967 pkt->head.length, udp_payload_length);
968 return BFD_UDP_ERROR_BAD;
969 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200970 if (!bfd_verify_pkt_common (pkt))
971 {
972 return BFD_UDP_ERROR_BAD;
973 }
974 bfd_session_t *bs = NULL;
975 if (pkt->your_disc)
976 {
977 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100978 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200979 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
980 }
981 else
982 {
983 bfd_udp_key_t key;
984 memset (&key, 0, sizeof (key));
985 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
986 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
987 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
988 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100989 "peer=%U)",
990 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
991 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200992 bs = bfd_lookup_session (&bfd_udp_main, &key);
993 }
994 if (!bs)
995 {
996 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
997 return BFD_UDP_ERROR_BAD;
998 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100999 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001000 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001001 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001002 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001003 return BFD_UDP_ERROR_BAD;
1004 }
1005 bfd_udp_error_t err;
1006 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1007 {
1008 return err;
1009 }
1010 bfd_rpc_update_session (bs->bs_idx, pkt);
1011 *bs_out = bs;
1012 return BFD_UDP_ERROR_NONE;
1013}
1014
Klement Sekerac5fccc02017-01-18 09:56:00 +01001015static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001016bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1017 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001018{
Klement Sekera46a87ad2017-01-02 08:22:23 +01001019 /* sanity check first */
Damjan Marion072401e2017-07-13 18:53:27 +02001020 const i32 start = vnet_buffer (b)->l3_hdr_offset;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001021 if (start < 0 && start < sizeof (b->pre_data))
1022 {
1023 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1024 *ip6 = NULL;
1025 *udp = NULL;
1026 return;
1027 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001028 *ip6 = (ip6_header_t *) (b->data + start);
1029 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001030 {
1031 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1032 *ip6 = NULL;
1033 *udp = NULL;
1034 return;
1035 }
Klement Sekera402ed312017-01-18 09:44:36 +01001036 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1037 {
1038 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
Klement Sekerab17dd962017-01-09 07:43:48 +01001039 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
Klement Sekera402ed312017-01-18 09:44:36 +01001040 *ip6 = NULL;
1041 *udp = NULL;
1042 return;
1043 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001044 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001045}
1046
Klement Sekerac5fccc02017-01-18 09:56:00 +01001047static bfd_udp_error_t
1048bfd_udp6_verify_transport (const ip6_header_t * ip6,
1049 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001050{
1051 const bfd_udp_session_t *bus = &bs->udp;
1052 const bfd_udp_key_t *key = &bus->key;
1053 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1054 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1055 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001056 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1057 format_ip6_address, ip6, format_ip6_address,
1058 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001059 return BFD_UDP_ERROR_BAD;
1060 }
1061 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1062 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1063 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001064 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1065 format_ip6_address, ip6, format_ip6_address,
1066 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001067 return BFD_UDP_ERROR_BAD;
1068 }
1069 const u8 expected_hop_limit = 255;
1070 if (ip6->hop_limit != expected_hop_limit)
1071 {
1072 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001073 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001074 return BFD_UDP_ERROR_BAD;
1075 }
Klement Sekera6f966492017-02-08 07:42:08 +01001076 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001077 {
1078 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001079 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001080 }
1081 return BFD_UDP_ERROR_NONE;
1082}
1083
Klement Sekerac5fccc02017-01-18 09:56:00 +01001084static bfd_udp_error_t
1085bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1086 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001087{
1088 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1089 if (sizeof (*pkt) > b->current_length)
1090 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001091 BFD_ERR
1092 ("Payload size %d too small to hold bfd packet of minimum size %d",
1093 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001094 return BFD_UDP_ERROR_BAD;
1095 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001096 ip6_header_t *ip6;
1097 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001098 bfd_udp6_find_headers (b, &ip6, &udp);
1099 if (!ip6 || !udp)
1100 {
1101 BFD_ERR ("Couldn't find ip6 or udp header");
1102 return BFD_UDP_ERROR_BAD;
1103 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001104 const u32 udp_payload_length = udp->length - sizeof (*udp);
1105 if (pkt->head.length > udp_payload_length)
1106 {
1107 BFD_ERR
1108 ("BFD packet length is larger than udp payload length (%u > %u)",
1109 pkt->head.length, udp_payload_length);
1110 return BFD_UDP_ERROR_BAD;
1111 }
Klement Sekera46a87ad2017-01-02 08:22:23 +01001112 if (!bfd_verify_pkt_common (pkt))
1113 {
1114 return BFD_UDP_ERROR_BAD;
1115 }
1116 bfd_session_t *bs = NULL;
1117 if (pkt->your_disc)
1118 {
1119 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001120 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001121 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1122 }
1123 else
1124 {
1125 bfd_udp_key_t key;
1126 memset (&key, 0, sizeof (key));
1127 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1128 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1129 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1130 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1131 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1132 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerab17dd962017-01-09 07:43:48 +01001133 "peer=%U)",
1134 key.sw_if_index, format_ip6_address, &key.local_addr,
1135 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001136 bs = bfd_lookup_session (&bfd_udp_main, &key);
1137 }
1138 if (!bs)
1139 {
1140 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1141 return BFD_UDP_ERROR_BAD;
1142 }
1143 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001144 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001145 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001146 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera46a87ad2017-01-02 08:22:23 +01001147 return BFD_UDP_ERROR_BAD;
1148 }
1149 bfd_udp_error_t err;
1150 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1151 {
1152 return err;
1153 }
1154 bfd_rpc_update_session (bs->bs_idx, pkt);
1155 *bs_out = bs;
1156 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001157}
1158
1159/*
1160 * Process a frame of bfd packets
1161 * Expect 1 packet / frame
1162 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001163static uword
1164bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1165 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001166{
1167 u32 n_left_from, *from;
1168 bfd_input_trace_t *t0;
1169
Klement Sekerac5fccc02017-01-18 09:56:00 +01001170 from = vlib_frame_vector_args (f); /* array of buffer indices */
1171 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001172
1173 while (n_left_from > 0)
1174 {
1175 u32 bi0;
1176 vlib_buffer_t *b0;
1177 u32 next0, error0;
1178
1179 bi0 = from[0];
1180 b0 = vlib_get_buffer (vm, bi0);
1181
1182 bfd_session_t *bs = NULL;
1183
1184 /* If this pkt is traced, snapshot the data */
1185 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001186 {
1187 int len;
1188 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1189 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1190 : sizeof (t0->data);
1191 t0->len = len;
1192 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1193 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001194
1195 /* scan this bfd pkt. error0 is the counter index to bmp */
1196 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001197 {
1198 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1199 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001200 else
Klement Sekerac5fccc02017-01-18 09:56:00 +01001201 {
1202 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1203 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001204 b0->error = rt->errors[error0];
1205
1206 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1207 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001208 {
Klement Sekera402ed312017-01-18 09:44:36 +01001209 /*
1210 * if everything went fine, check for poll bit, if present, re-use
1211 * the buffer and based on (now updated) session parameters, send
1212 * the final packet back
1213 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001214 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1215 if (bfd_pkt_get_poll (pkt))
1216 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001217 b0->current_data = 0;
1218 b0->current_length = 0;
1219 memset (vnet_buffer (b0), 0, sizeof (*vnet_buffer (b0)));
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001220 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1221 0);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001222 if (is_ipv6)
1223 {
1224 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1225 b0->error, 1);
1226 }
1227 else
1228 {
1229 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1230 b0->error, 1);
1231 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001232 const bfd_udp_session_t *bus = &bs->udp;
1233 ip_adjacency_t *adj = adj_get (bus->adj_index);
1234 switch (adj->lookup_next_index)
1235 {
1236 case IP_LOOKUP_NEXT_ARP:
1237 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1238 break;
1239 case IP_LOOKUP_NEXT_REWRITE:
1240 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1241 break;
1242 default:
1243 /* drop */
1244 break;
1245 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001246 }
1247 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001248 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1249
1250 from += 1;
1251 n_left_from -= 1;
1252 }
1253
1254 return f->n_vectors;
1255}
1256
Klement Sekerac5fccc02017-01-18 09:56:00 +01001257static uword
1258bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001259{
1260 return bfd_udp_input (vm, rt, f, 0);
1261}
1262
1263/*
1264 * bfd input graph node declaration
1265 */
1266/* *INDENT-OFF* */
1267VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1268 .function = bfd_udp4_input,
1269 .name = "bfd-udp4-input",
1270 .vector_size = sizeof (u32),
1271 .type = VLIB_NODE_TYPE_INTERNAL,
1272
1273 .n_errors = BFD_UDP_N_ERROR,
1274 .error_strings = bfd_udp_error_strings,
1275
1276 .format_trace = bfd_input_format_trace,
1277
1278 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1279 .next_nodes =
1280 {
1281 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001282 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1283 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001284 },
1285};
1286/* *INDENT-ON* */
1287
Klement Sekerac5fccc02017-01-18 09:56:00 +01001288static uword
1289bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001290{
1291 return bfd_udp_input (vm, rt, f, 1);
1292}
1293
1294/* *INDENT-OFF* */
1295VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1296 .function = bfd_udp6_input,
1297 .name = "bfd-udp6-input",
1298 .vector_size = sizeof (u32),
1299 .type = VLIB_NODE_TYPE_INTERNAL,
1300
1301 .n_errors = BFD_UDP_N_ERROR,
1302 .error_strings = bfd_udp_error_strings,
1303
1304 .format_trace = bfd_input_format_trace,
1305
1306 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1307 .next_nodes =
1308 {
1309 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001310 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1311 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001312 },
1313};
1314/* *INDENT-ON* */
1315
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001316/*
1317 * Process a frame of bfd echo packets
1318 * Expect 1 packet / frame
1319 */
1320static uword
1321bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1322 vlib_frame_t * f, int is_ipv6)
1323{
1324 u32 n_left_from, *from;
1325 bfd_input_trace_t *t0;
1326
1327 from = vlib_frame_vector_args (f); /* array of buffer indices */
1328 n_left_from = f->n_vectors; /* number of buffer indices */
1329
1330 while (n_left_from > 0)
1331 {
1332 u32 bi0;
1333 vlib_buffer_t *b0;
1334 u32 next0;
1335
1336 bi0 = from[0];
1337 b0 = vlib_get_buffer (vm, bi0);
1338
1339 /* If this pkt is traced, snapshot the data */
1340 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1341 {
1342 int len;
1343 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1344 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1345 : sizeof (t0->data);
1346 t0->len = len;
1347 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1348 }
1349
Klement Sekera239790f2017-02-16 10:53:53 +01001350 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1351 {
1352 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1353 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1354 }
1355 else
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001356 {
1357 /* loop back the packet */
1358 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1359 if (is_ipv6)
1360 {
1361 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1362 b0->error, 1);
1363 }
1364 else
1365 {
1366 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1367 b0->error, 1);
1368 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001369 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001370 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001371
1372 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1373
1374 from += 1;
1375 n_left_from -= 1;
1376 }
1377
1378 return f->n_vectors;
1379}
1380
1381static uword
1382bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1383 vlib_frame_t * f)
1384{
1385 return bfd_udp_echo_input (vm, rt, f, 0);
1386}
1387
1388u8 *
1389bfd_echo_input_format_trace (u8 * s, va_list * args)
1390{
1391 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1392 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1393 const bfd_udp_echo_input_trace_t *t =
1394 va_arg (*args, bfd_udp_echo_input_trace_t *);
1395 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1396 {
1397 s = format (s, "BFD ECHO:\n");
1398 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1399 }
1400
1401 return s;
1402}
1403
1404/*
1405 * bfd input graph node declaration
1406 */
1407/* *INDENT-OFF* */
1408VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1409 .function = bfd_udp_echo4_input,
1410 .name = "bfd-udp-echo4-input",
1411 .vector_size = sizeof (u32),
1412 .type = VLIB_NODE_TYPE_INTERNAL,
1413
1414 .n_errors = BFD_UDP_ECHO_N_ERROR,
1415 .error_strings = bfd_udp_error_strings,
1416
1417 .format_trace = bfd_echo_input_format_trace,
1418
1419 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1420 .next_nodes =
1421 {
1422 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001423 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1424 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001425 },
1426};
1427/* *INDENT-ON* */
1428
1429static uword
1430bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1431 vlib_frame_t * f)
1432{
1433 return bfd_udp_echo_input (vm, rt, f, 1);
1434}
1435
1436/* *INDENT-OFF* */
1437VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1438 .function = bfd_udp_echo6_input,
1439 .name = "bfd-udp-echo6-input",
1440 .vector_size = sizeof (u32),
1441 .type = VLIB_NODE_TYPE_INTERNAL,
1442
1443 .n_errors = BFD_UDP_ECHO_N_ERROR,
1444 .error_strings = bfd_udp_echo_error_strings,
1445
1446 .format_trace = bfd_echo_input_format_trace,
1447
1448 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1449 .next_nodes =
1450 {
1451 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001452 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1453 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001454 },
1455};
1456
1457/* *INDENT-ON* */
1458
Klement Sekerac5fccc02017-01-18 09:56:00 +01001459static clib_error_t *
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001460bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001461{
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001462 bfd_session_t **to_be_freed = NULL;
Damjan Marion07a38572018-01-21 06:44:18 -08001463 bfd_udp_main_t *bum = &bfd_udp_main;
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001464 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1465 is_create);
1466 if (!is_create)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001467 {
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001468 bfd_session_t *bs;
1469 pool_foreach (bs, bfd_udp_main.bfd_main->sessions,
1470 {
1471 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1472 bs->transport != BFD_TRANSPORT_UDP6)
1473 {
1474 continue;}
1475 if (bs->udp.key.sw_if_index != sw_if_index)
1476 {
1477 continue;}
1478 vec_add1 (to_be_freed, bs);}
1479 );
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001480 }
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001481 bfd_session_t **bs;
1482 vec_foreach (bs, to_be_freed)
1483 {
Damjan Marion07a38572018-01-21 06:44:18 -08001484 vlib_log_notice (bum->log_class,
1485 "removal of sw_if_index=%u forces removal of bfd session "
1486 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001487 bfd_session_set_flags (*bs, 0);
1488 bfd_udp_del_session_internal (*bs);
1489 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001490 return 0;
1491}
1492
Klement Sekeraf3bcdbf2017-05-02 07:38:01 +02001493VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001494
1495/*
1496 * setup function
1497 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001498static clib_error_t *
1499bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001500{
1501 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +01001502 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001503 bfd_udp_main.bfd_main = &bfd_main;
Klement Sekera239790f2017-02-16 10:53:53 +01001504 bfd_udp_main.vnet_main = vnet_get_main ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001505 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1506 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001507 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1508 bfd_udp_echo4_input_node.index, 1);
1509 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1510 bfd_udp_echo6_input_node.index, 0);
Klement Sekerae50e8562017-04-04 16:19:48 +02001511 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1512 ASSERT (node);
1513 bfd_udp_main.ip4_arp_idx = node->index;
1514 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1515 ASSERT (node);
1516 bfd_udp_main.ip6_ndp_idx = node->index;
1517 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1518 ASSERT (node);
1519 bfd_udp_main.ip4_rewrite_idx = node->index;
1520 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1521 ASSERT (node);
1522 bfd_udp_main.ip6_rewrite_idx = node->index;
1523
Damjan Marion07a38572018-01-21 06:44:18 -08001524 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1525 vlib_log_debug (bfd_udp_main.log_class, "initialized");
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001526 return 0;
1527}
1528
1529VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001530
1531/*
1532 * fd.io coding-style-patch-verification: ON
1533 *
1534 * Local Variables:
1535 * eval: (c-set-style "gnu")
1536 * End:
1537 */