blob: ebee590b7950107f72ee590dc461a832c942d2bd [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;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020062} bfd_udp_main_t;
63
Klement Sekerae4504c62016-12-08 10:16:41 +010064static vlib_node_registration_t bfd_udp4_input_node;
65static vlib_node_registration_t bfd_udp6_input_node;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010066static vlib_node_registration_t bfd_udp_echo4_input_node;
67static vlib_node_registration_t bfd_udp_echo6_input_node;
Klement Sekerae4504c62016-12-08 10:16:41 +010068
Klement Sekera0e3c0de2016-09-29 14:43:44 +020069bfd_udp_main_t bfd_udp_main;
70
Klement Sekera239790f2017-02-16 10:53:53 +010071vnet_api_error_t
72bfd_udp_set_echo_source (u32 sw_if_index)
73{
74 vnet_sw_interface_t *sw_if =
Klement Sekera73884482017-02-23 09:26:30 +010075 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera239790f2017-02-16 10:53:53 +010076 if (sw_if)
77 {
78 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
79 bfd_udp_main.echo_source_is_set = 1;
80 return 0;
81 }
82 return VNET_API_ERROR_BFD_ENOENT;
83}
84
85vnet_api_error_t
86bfd_udp_del_echo_source (u32 sw_if_index)
87{
88 bfd_udp_main.echo_source_sw_if_index = ~0;
89 bfd_udp_main.echo_source_is_set = 0;
90 return 0;
91}
92
93int
94bfd_udp_is_echo_available (bfd_transport_e transport)
95{
96 if (!bfd_udp_main.echo_source_is_set)
97 {
Klement Sekera73884482017-02-23 09:26:30 +010098 BFD_DBG ("UDP echo source not set - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +010099 return 0;
100 }
101 /*
102 * for the echo to work, we need a loopback interface with at least one
103 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
104 * pick an unused address from that subnet
105 */
106 vnet_sw_interface_t *sw_if =
107 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main,
108 bfd_udp_main.echo_source_sw_if_index);
109 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
110 {
111 if (BFD_TRANSPORT_UDP4 == transport)
112 {
113 ip4_main_t *im = &ip4_main;
114 ip_interface_address_t *ia = NULL;
115 /* *INDENT-OFF* */
116 foreach_ip_interface_address (&im->lookup_main, ia,
117 bfd_udp_main.echo_source_sw_if_index,
118 0 /* honor unnumbered */, ({
119 if (ia->address_length <= 31)
120 {
121 return 1;
122 }
123 }));
124 /* *INDENT-ON* */
125 }
126 else if (BFD_TRANSPORT_UDP6 == transport)
127 {
128 ip6_main_t *im = &ip6_main;
129 ip_interface_address_t *ia = NULL;
130 /* *INDENT-OFF* */
131 foreach_ip_interface_address (&im->lookup_main, ia,
132 bfd_udp_main.echo_source_sw_if_index,
133 0 /* honor unnumbered */, ({
134 if (ia->address_length <= 127)
135 {
136 return 1;
137 }
138 }));
139 /* *INDENT-ON* */
140 }
141 }
Klement Sekera73884482017-02-23 09:26:30 +0100142 BFD_DBG ("No usable IP address for UDP echo - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100143 return 0;
144}
145
Klement Sekera2bce0332017-02-09 06:03:46 +0100146static u16
147bfd_udp_bs_idx_to_sport (u32 bs_idx)
148{
149 /* The source port MUST be in the range 49152 through 65535. The same UDP
150 * source port number MUST be used for all BFD Control packets associated
151 * with a particular session. The source port number SHOULD be unique among
152 * all BFD sessions on the system. If more than 16384 BFD sessions are
153 * simultaneously active, UDP source port numbers MAY be reused on
154 * multiple sessions, but the number of distinct uses of the same UDP
155 * source port number SHOULD be minimized.
156 */
157 return 49152 + bs_idx % (65535 - 49152 + 1);
158}
159
Klement Sekera239790f2017-02-16 10:53:53 +0100160int
161bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
162{
163 if (!bfd_udp_main.echo_source_is_set)
164 {
165 BFD_ERR ("cannot find ip4 address, echo source not set");
166 return 0;
167 }
168 ip_interface_address_t *ia = NULL;
169 ip4_main_t *im = &ip4_main;
170
171 /* *INDENT-OFF* */
172 foreach_ip_interface_address (
173 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
174 0 /* honor unnumbered */, ({
175 ip4_address_t *x =
176 ip_interface_address_get_address (&im->lookup_main, ia);
177 if (ia->address_length <= 31)
178 {
179 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
180 /*
181 * flip the last bit to get a different address, might be network,
182 * we don't care ...
183 */
184 addr->as_u32 ^= 1;
185 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
186 return 1;
187 }
188 }));
189 /* *INDENT-ON* */
190 BFD_ERR ("cannot find ip4 address, no usable address found");
191 return 0;
192}
193
194int
195bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
196{
197 if (!bfd_udp_main.echo_source_is_set)
198 {
199 BFD_ERR ("cannot find ip6 address, echo source not set");
200 return 0;
201 }
202 ip_interface_address_t *ia = NULL;
203 ip6_main_t *im = &ip6_main;
204
205 /* *INDENT-OFF* */
206 foreach_ip_interface_address (
207 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
208 0 /* honor unnumbered */, ({
209 ip6_address_t *x =
210 ip_interface_address_get_address (&im->lookup_main, ia);
211 if (ia->address_length <= 127)
212 {
213 *addr = *x;
214 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
Klement Sekera239790f2017-02-16 10:53:53 +0100215 return 1;
216 }
217 }));
218 /* *INDENT-ON* */
219 BFD_ERR ("cannot find ip6 address, no usable address found");
220 return 0;
221}
222
Klement Sekera73884482017-02-23 09:26:30 +0100223void
224bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index, int *have_usable_ip4,
225 ip4_address_t * ip4, int *have_usable_ip6,
226 ip6_address_t * ip6)
227{
228 if (bfd_udp_main.echo_source_is_set)
229 {
230 *is_set = 1;
231 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
232 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
233 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
234 }
235 else
236 {
237 *is_set = 0;
238 }
239}
240
Klement Sekera239790f2017-02-16 10:53:53 +0100241int
Klement Sekerae50e8562017-04-04 16:19:48 +0200242bfd_add_udp4_transport (vlib_main_t * vm, u32 bi,
Klement Sekera239790f2017-02-16 10:53:53 +0100243 const bfd_session_t * bs, int is_echo)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200244{
Klement Sekera2bce0332017-02-09 06:03:46 +0100245 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100246 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200247 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200248
249 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200250 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
251 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200252 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
253 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
Klement Sekera402ed312017-01-18 09:44:36 +0100254 typedef struct
255 {
256 ip4_header_t ip4;
257 udp_header_t udp;
258 } ip4_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100259 ip4_udp_headers *headers = NULL;
Klement Sekera402ed312017-01-18 09:44:36 +0100260 vlib_buffer_advance (b, -sizeof (*headers));
261 headers = vlib_buffer_get_current (b);
262 memset (headers, 0, sizeof (*headers));
263 headers->ip4.ip_version_and_header_length = 0x45;
264 headers->ip4.ttl = 255;
265 headers->ip4.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100266 headers->udp.src_port =
267 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100268 if (is_echo)
269 {
270 int rv;
271 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
272 {
273 return rv;
274 }
275 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
276 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
277 }
278 else
279 {
280 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
281 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
282 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
283 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100284
285 /* fix ip length, checksum and udp length */
286 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
287
Klement Sekera402ed312017-01-18 09:44:36 +0100288 headers->ip4.length = clib_host_to_net_u16 (ip_length);
289 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100290
Klement Sekera402ed312017-01-18 09:44:36 +0100291 const u16 udp_length = ip_length - (sizeof (headers->ip4));
292 headers->udp.length = clib_host_to_net_u16 (udp_length);
Klement Sekera239790f2017-02-16 10:53:53 +0100293 return 1;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100294}
295
Klement Sekera239790f2017-02-16 10:53:53 +0100296int
Klement Sekerae50e8562017-04-04 16:19:48 +0200297bfd_add_udp6_transport (vlib_main_t * vm, u32 bi,
Klement Sekera239790f2017-02-16 10:53:53 +0100298 const bfd_session_t * bs, int is_echo)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100299{
Klement Sekera2bce0332017-02-09 06:03:46 +0100300 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100301 const bfd_udp_key_t *key = &bus->key;
Klement Sekerae50e8562017-04-04 16:19:48 +0200302 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100303
304 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
305 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
306 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekerae50e8562017-04-04 16:19:48 +0200307 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
308 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100309 typedef struct
310 {
311 ip6_header_t ip6;
312 udp_header_t udp;
313 } ip6_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100314 ip6_udp_headers *headers = NULL;
315 vlib_buffer_advance (b, -sizeof (*headers));
316 headers = vlib_buffer_get_current (b);
Klement Sekera402ed312017-01-18 09:44:36 +0100317 memset (headers, 0, sizeof (*headers));
318 headers->ip6.ip_version_traffic_class_and_flow_label =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100319 clib_host_to_net_u32 (0x6 << 28);
Klement Sekera402ed312017-01-18 09:44:36 +0100320 headers->ip6.hop_limit = 255;
321 headers->ip6.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100322 headers->udp.src_port =
323 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100324 if (is_echo)
325 {
326 int rv;
327 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
328 {
329 return rv;
330 }
331 clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6,
332 sizeof (headers->ip6.dst_address));
333
334 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
335 }
336 else
337 {
338 clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6,
339 sizeof (headers->ip6.src_address));
340 clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6,
341 sizeof (headers->ip6.dst_address));
342 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
343 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100344
345 /* fix ip payload length and udp length */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100346 const u16 udp_length =
Klement Sekera402ed312017-01-18 09:44:36 +0100347 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
348 headers->udp.length = clib_host_to_net_u16 (udp_length);
349 headers->ip6.payload_length = headers->udp.length;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100350
351 /* IPv6 UDP checksum is mandatory */
352 int bogus = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100353 headers->udp.checksum =
354 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100355 ASSERT (bogus == 0);
Klement Sekera402ed312017-01-18 09:44:36 +0100356 if (headers->udp.checksum == 0)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100357 {
Klement Sekera402ed312017-01-18 09:44:36 +0100358 headers->udp.checksum = 0xffff;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100359 }
Klement Sekera239790f2017-02-16 10:53:53 +0100360 return 1;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200361}
362
Klement Sekerae50e8562017-04-04 16:19:48 +0200363static void
364bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
365{
366 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
367 u32 *to_next = vlib_frame_vector_args (f);
368 to_next[0] = bi;
369 f->n_vectors = 1;
370 vlib_put_frame_to_node (vm, next_node, f);
371}
372
373int
374bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
375{
376 const bfd_udp_session_t *bus = &bs->udp;
377 ip_adjacency_t *adj = adj_get (bus->adj_index);
378 switch (adj->lookup_next_index)
379 {
380 case IP_LOOKUP_NEXT_ARP:
381 switch (bs->transport)
382 {
383 case BFD_TRANSPORT_UDP4:
384 *next_node = bfd_udp_main.ip4_arp_idx;
385 return 1;
386 case BFD_TRANSPORT_UDP6:
387 *next_node = bfd_udp_main.ip6_ndp_idx;
388 return 1;
389 }
390 break;
391 case IP_LOOKUP_NEXT_REWRITE:
392 switch (bs->transport)
393 {
394 case BFD_TRANSPORT_UDP4:
395 *next_node = bfd_udp_main.ip4_rewrite_idx;
396 return 1;
397 case BFD_TRANSPORT_UDP6:
398 *next_node = bfd_udp_main.ip6_rewrite_idx;
399 return 1;
400 }
401 break;
402 default:
403 /* drop */
404 break;
405 }
406 return 0;
407}
408
409int
410bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
411{
412 u32 next_node;
413 int rv = bfd_udp_calc_next_node (bs, &next_node);
414 if (rv)
415 {
416 bfd_create_frame_to_next_node (vm, bi, next_node);
417 }
418 return rv;
419}
420
421int
422bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
423{
424 u32 next_node;
425 int rv = bfd_udp_calc_next_node (bs, &next_node);
426 if (rv)
427 {
428 bfd_create_frame_to_next_node (vm, bi, next_node);
429 }
430 return 1;
431}
432
Klement Sekerac5fccc02017-01-18 09:56:00 +0100433static bfd_session_t *
434bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200435{
436 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
437 if (p)
438 {
439 return bfd_find_session_by_idx (bum->bfd_main, *p);
440 }
441 return 0;
442}
443
Klement Sekerab17dd962017-01-09 07:43:48 +0100444static void
445bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
446 const ip46_address_t * local_addr,
447 const ip46_address_t * peer_addr)
448{
449 memset (key, 0, sizeof (*key));
450 key->sw_if_index = sw_if_index;
451 key->local_addr.as_u64[0] = local_addr->as_u64[0];
452 key->local_addr.as_u64[1] = local_addr->as_u64[1];
453 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
454 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
455}
456
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200457static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100458bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100459 u32 desired_min_tx_usec,
460 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100461 const ip46_address_t * local_addr,
462 const ip46_address_t * peer_addr,
Klement Sekerab17dd962017-01-09 07:43:48 +0100463 bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200464{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200465 /* get a pool entry and if we end up not needing it, give it back */
Klement Sekera239790f2017-02-16 10:53:53 +0100466 bfd_transport_e t = BFD_TRANSPORT_UDP4;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200467 if (!ip46_address_is_ip4 (local_addr))
468 {
469 t = BFD_TRANSPORT_UDP6;
470 }
471 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
Klement Sekera239790f2017-02-16 10:53:53 +0100472 if (!bs)
473 {
474 bfd_put_session (bum->bfd_main, bs);
475 return VNET_API_ERROR_BFD_EAGAIN;
476 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200477 bfd_udp_session_t *bus = &bs->udp;
478 memset (bus, 0, sizeof (*bus));
479 bfd_udp_key_t *key = &bus->key;
Klement Sekerab17dd962017-01-09 07:43:48 +0100480 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200481 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
482 if (tmp)
483 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100484 clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
485 tmp->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200486 bfd_put_session (bum->bfd_main, bs);
487 return VNET_API_ERROR_BFD_EEXIST;
488 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200489 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
490 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100491 bs->bs_idx, key->sw_if_index, format_ip46_address,
492 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
493 &key->peer_addr, IP46_TYPE_ANY);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200494 if (BFD_TRANSPORT_UDP4 == t)
495 {
496 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100497 &key->peer_addr,
498 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200499 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100500 "returns %d", format_ip46_address, &key->peer_addr,
501 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200502 }
503 else
504 {
505 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100506 &key->peer_addr,
507 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200508 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100509 "returns %d", format_ip46_address, &key->peer_addr,
510 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200511 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100512 *bs_out = bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100513 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
514 required_min_rx_usec, detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200515}
516
517static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100518bfd_udp_validate_api_input (u32 sw_if_index,
519 const ip46_address_t * local_addr,
520 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200521{
522 vnet_sw_interface_t *sw_if =
Klement Sekera239790f2017-02-16 10:53:53 +0100523 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200524 u8 local_ip_valid = 0;
525 ip_interface_address_t *ia = NULL;
526 if (!sw_if)
527 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100528 clib_warning ("got NULL sw_if");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200529 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
530 }
531 if (ip46_address_is_ip4 (local_addr))
532 {
533 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100534 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100535 clib_warning ("IP family mismatch");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100536 return VNET_API_ERROR_INVALID_ARGUMENT;
537 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200538 ip4_main_t *im = &ip4_main;
539
540 /* *INDENT-OFF* */
541 foreach_ip_interface_address (
542 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
543 ip4_address_t *x =
544 ip_interface_address_get_address (&im->lookup_main, ia);
545 if (x->as_u32 == local_addr->ip4.as_u32)
546 {
547 /* valid address for this interface */
548 local_ip_valid = 1;
549 break;
550 }
551 }));
552 /* *INDENT-ON* */
553 }
554 else
555 {
556 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100557 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100558 clib_warning ("IP family mismatch");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100559 return VNET_API_ERROR_INVALID_ARGUMENT;
560 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200561 ip6_main_t *im = &ip6_main;
562 /* *INDENT-OFF* */
563 foreach_ip_interface_address (
564 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
565 ip6_address_t *x =
566 ip_interface_address_get_address (&im->lookup_main, ia);
567 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
Klement Sekerab17dd962017-01-09 07:43:48 +0100568 local_addr->ip6.as_u64[1] == x->as_u64[1])
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200569 {
570 /* valid address for this interface */
571 local_ip_valid = 1;
572 break;
573 }
574 }));
575 /* *INDENT-ON* */
576 }
577
578 if (!local_ip_valid)
579 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100580 clib_warning ("address not found on interface");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200581 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
582 }
583
584 return 0;
585}
586
Klement Sekerab17dd962017-01-09 07:43:48 +0100587static vnet_api_error_t
588bfd_udp_find_session_by_api_input (u32 sw_if_index,
589 const ip46_address_t * local_addr,
590 const ip46_address_t * peer_addr,
591 bfd_session_t ** bs_out)
592{
593 vnet_api_error_t rv =
594 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
595 if (!rv)
596 {
597 bfd_udp_main_t *bum = &bfd_udp_main;
598 bfd_udp_key_t key;
599 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
600 bfd_session_t *bs = bfd_lookup_session (bum, &key);
601 if (bs)
602 {
603 *bs_out = bs;
604 }
605 else
606 {
607 clib_warning
608 ("BFD session not found (sw_if_index=%u, local=%U, peer=%U",
609 sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY,
610 format_ip46_address, peer_addr, IP46_TYPE_ANY);
611 return VNET_API_ERROR_BFD_ENOENT;
612 }
613 }
614 return rv;
615}
616
617static vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100618bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
619 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerab17dd962017-01-09 07:43:48 +0100620 const ip46_address_t * local_addr,
621 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200622{
623 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100624 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200625 if (rv)
626 {
627 return rv;
628 }
629 if (detect_mult < 1)
630 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100631 clib_warning ("detect_mult < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200632 return VNET_API_ERROR_INVALID_ARGUMENT;
633 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100634 if (desired_min_tx_usec < 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200635 {
Klement Sekeraa57a9702017-02-02 06:58:07 +0100636 clib_warning ("desired_min_tx_usec < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200637 return VNET_API_ERROR_INVALID_ARGUMENT;
638 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100639 return 0;
640}
641
642static void
643bfd_udp_del_session_internal (bfd_session_t * bs)
644{
645 bfd_udp_main_t *bum = &bfd_udp_main;
646 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
647 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
648 adj_unlock (bs->udp.adj_index);
649 bfd_put_session (bum->bfd_main, bs);
650}
651
652vnet_api_error_t
653bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100654 const ip46_address_t * peer_addr,
655 u32 desired_min_tx_usec, u32 required_min_rx_usec,
656 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
657 u8 bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100658{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100659 vnet_api_error_t rv =
660 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
661 required_min_rx_usec, detect_mult,
662 local_addr, peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +0100663 bfd_session_t *bs = NULL;
664 if (!rv)
665 {
666 rv =
667 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100668 desired_min_tx_usec,
669 required_min_rx_usec, detect_mult,
670 local_addr, peer_addr, &bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100671 }
672 if (!rv && is_authenticated)
673 {
674#if WITH_LIBSSL > 0
675 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
676 0 /* is not delayed */ );
677#else
678 clib_warning ("SSL missing, cannot add authenticated BFD session");
679 rv = VNET_API_ERROR_BFD_NOTSUPP;
680#endif
681 if (rv)
682 {
683 bfd_udp_del_session_internal (bs);
684 }
685 }
686 if (!rv)
687 {
688 bfd_session_start (bfd_udp_main.bfd_main, bs);
689 }
690
691 return rv;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200692}
693
Klement Sekerac5fccc02017-01-18 09:56:00 +0100694vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100695bfd_udp_mod_session (u32 sw_if_index,
696 const ip46_address_t * local_addr,
697 const ip46_address_t * peer_addr,
698 u32 desired_min_tx_usec,
699 u32 required_min_rx_usec, u8 detect_mult)
700{
701 bfd_session_t *bs = NULL;
702 vnet_api_error_t rv =
703 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
704 &bs);
705 if (rv)
706 {
707 return rv;
708 }
709
710 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
711 desired_min_tx_usec, required_min_rx_usec,
712 detect_mult);
713}
714
715vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100716bfd_udp_del_session (u32 sw_if_index,
717 const ip46_address_t * local_addr,
718 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200719{
Klement Sekerab17dd962017-01-09 07:43:48 +0100720 bfd_session_t *bs = NULL;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200721 vnet_api_error_t rv =
Klement Sekerab17dd962017-01-09 07:43:48 +0100722 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
723 &bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200724 if (rv)
725 {
726 return rv;
727 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100728 bfd_udp_del_session_internal (bs);
729 return 0;
730}
731
732vnet_api_error_t
733bfd_udp_session_set_flags (u32 sw_if_index,
734 const ip46_address_t * local_addr,
735 const ip46_address_t * peer_addr, u8 admin_up_down)
736{
737 bfd_session_t *bs = NULL;
738 vnet_api_error_t rv =
739 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
740 &bs);
741 if (rv)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200742 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100743 return rv;
744 }
745 bfd_session_set_flags (bs, admin_up_down);
746 return 0;
747}
748
749vnet_api_error_t
Klement Sekerab17dd962017-01-09 07:43:48 +0100750bfd_udp_auth_activate (u32 sw_if_index,
751 const ip46_address_t * local_addr,
752 const ip46_address_t * peer_addr,
753 u32 conf_key_id, u8 key_id, u8 is_delayed)
754{
755#if WITH_LIBSSL > 0
756 bfd_session_t *bs = NULL;
757 vnet_api_error_t rv =
758 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
759 &bs);
760 if (rv)
761 {
762 return rv;
763 }
764 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
765#else
766 clib_warning ("SSL missing, cannot activate BFD authentication");
767 return VNET_API_ERROR_BFD_NOTSUPP;
768#endif
769}
770
771vnet_api_error_t
772bfd_udp_auth_deactivate (u32 sw_if_index,
773 const ip46_address_t * local_addr,
774 const ip46_address_t * peer_addr, u8 is_delayed)
775{
776 bfd_session_t *bs = NULL;
777 vnet_api_error_t rv =
778 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
779 &bs);
780 if (rv)
781 {
782 return rv;
783 }
784 return bfd_auth_deactivate (bs, is_delayed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200785}
786
Klement Sekerac5fccc02017-01-18 09:56:00 +0100787typedef enum
788{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200789 BFD_UDP_INPUT_NEXT_NORMAL,
Klement Sekerae50e8562017-04-04 16:19:48 +0200790 BFD_UDP_INPUT_NEXT_REPLY_ARP,
791 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200792 BFD_UDP_INPUT_N_NEXT,
793} bfd_udp_input_next_t;
794
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100795/* Packet counters - BFD control frames */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200796#define foreach_bfd_udp_error(F) \
797 F (NONE, "good bfd packets (processed)") \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100798 F (BAD, "invalid bfd packets")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200799
800#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
801foreach_bfd_udp_error (F);
802#undef F
803
804static char *bfd_udp_error_strings[] = {
805#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
806 foreach_bfd_udp_error (F)
807#undef F
808};
809
Klement Sekerac5fccc02017-01-18 09:56:00 +0100810typedef enum
811{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200812#define F(sym, str) BFD_UDP_ERROR_##sym,
813 foreach_bfd_udp_error (F)
814#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100815 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200816} bfd_udp_error_t;
817
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100818/* Packet counters - BFD ECHO packets */
819#define foreach_bfd_udp_echo_error(F) \
820 F (NONE, "good bfd echo packets (processed)") \
821 F (BAD, "invalid bfd echo packets")
822
823#define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
824foreach_bfd_udp_echo_error (F);
825#undef F
826
827static char *bfd_udp_echo_error_strings[] = {
828#define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
829 foreach_bfd_udp_echo_error (F)
830#undef F
831};
832
833typedef enum
834{
835#define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
836 foreach_bfd_udp_echo_error (F)
837#undef F
838 BFD_UDP_ECHO_N_ERROR,
839} bfd_udp_echo_error_t;
840
Klement Sekerac5fccc02017-01-18 09:56:00 +0100841static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100842bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
843 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200844{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100845 /* sanity check first */
846 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
847 if (start < 0 && start < sizeof (b->pre_data))
848 {
849 BFD_ERR ("Start of ip header is before pre_data, ignoring");
850 *ip4 = NULL;
851 *udp = NULL;
852 return;
853 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100854 *ip4 = (ip4_header_t *) (b->data + start);
855 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100856 {
857 BFD_ERR ("Start of ip header is beyond current data, ignoring");
858 *ip4 = NULL;
859 *udp = NULL;
860 return;
861 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100862 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200863}
864
Klement Sekerac5fccc02017-01-18 09:56:00 +0100865static bfd_udp_error_t
866bfd_udp4_verify_transport (const ip4_header_t * ip4,
867 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200868{
869 const bfd_udp_session_t *bus = &bs->udp;
870 const bfd_udp_key_t *key = &bus->key;
871 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
872 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100873 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100874 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
875 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200876 return BFD_UDP_ERROR_BAD;
877 }
878 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
879 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100880 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100881 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
882 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200883 return BFD_UDP_ERROR_BAD;
884 }
885 const u8 expected_ttl = 255;
886 if (ip4->ttl != expected_ttl)
887 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100888 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100889 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200890 return BFD_UDP_ERROR_BAD;
891 }
Klement Sekera6f966492017-02-08 07:42:08 +0100892 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200893 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100894 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100895 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200896 }
897 return BFD_UDP_ERROR_NONE;
898}
899
900typedef struct
901{
902 u32 bs_idx;
903 bfd_pkt_t pkt;
904} bfd_rpc_update_t;
905
Klement Sekerac5fccc02017-01-18 09:56:00 +0100906static void
907bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200908{
909 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
910}
911
Klement Sekerac5fccc02017-01-18 09:56:00 +0100912static void
913bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200914{
915 /* packet length was already verified to be correct by the caller */
916 const u32 data_size = sizeof (bfd_rpc_update_t) -
Klement Sekerac5fccc02017-01-18 09:56:00 +0100917 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200918 u8 data[data_size];
Klement Sekerac5fccc02017-01-18 09:56:00 +0100919 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200920 update->bs_idx = bs_idx;
921 clib_memcpy (&update->pkt, pkt, pkt->head.length);
922 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
923}
924
Klement Sekerac5fccc02017-01-18 09:56:00 +0100925static bfd_udp_error_t
926bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
927 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200928{
929 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
930 if (sizeof (*pkt) > b->current_length)
931 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100932 BFD_ERR
933 ("Payload size %d too small to hold bfd packet of minimum size %d",
934 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200935 return BFD_UDP_ERROR_BAD;
936 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100937 ip4_header_t *ip4;
938 udp_header_t *udp;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200939 bfd_udp4_find_headers (b, &ip4, &udp);
940 if (!ip4 || !udp)
941 {
942 BFD_ERR ("Couldn't find ip4 or udp header");
943 return BFD_UDP_ERROR_BAD;
944 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100945 const u32 udp_payload_length = udp->length - sizeof (*udp);
946 if (pkt->head.length > udp_payload_length)
947 {
948 BFD_ERR
949 ("BFD packet length is larger than udp payload length (%u > %u)",
950 pkt->head.length, udp_payload_length);
951 return BFD_UDP_ERROR_BAD;
952 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200953 if (!bfd_verify_pkt_common (pkt))
954 {
955 return BFD_UDP_ERROR_BAD;
956 }
957 bfd_session_t *bs = NULL;
958 if (pkt->your_disc)
959 {
960 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100961 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200962 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
963 }
964 else
965 {
966 bfd_udp_key_t key;
967 memset (&key, 0, sizeof (key));
968 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
969 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
970 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
971 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100972 "peer=%U)",
973 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
974 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200975 bs = bfd_lookup_session (&bfd_udp_main, &key);
976 }
977 if (!bs)
978 {
979 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
980 return BFD_UDP_ERROR_BAD;
981 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100982 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +0100983 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200984 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100985 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200986 return BFD_UDP_ERROR_BAD;
987 }
988 bfd_udp_error_t err;
989 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
990 {
991 return err;
992 }
993 bfd_rpc_update_session (bs->bs_idx, pkt);
994 *bs_out = bs;
995 return BFD_UDP_ERROR_NONE;
996}
997
Klement Sekerac5fccc02017-01-18 09:56:00 +0100998static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100999bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1000 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001001{
Klement Sekera46a87ad2017-01-02 08:22:23 +01001002 /* sanity check first */
1003 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
1004 if (start < 0 && start < sizeof (b->pre_data))
1005 {
1006 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1007 *ip6 = NULL;
1008 *udp = NULL;
1009 return;
1010 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001011 *ip6 = (ip6_header_t *) (b->data + start);
1012 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001013 {
1014 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1015 *ip6 = NULL;
1016 *udp = NULL;
1017 return;
1018 }
Klement Sekera402ed312017-01-18 09:44:36 +01001019 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1020 {
1021 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
Klement Sekerab17dd962017-01-09 07:43:48 +01001022 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
Klement Sekera402ed312017-01-18 09:44:36 +01001023 *ip6 = NULL;
1024 *udp = NULL;
1025 return;
1026 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001027 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001028}
1029
Klement Sekerac5fccc02017-01-18 09:56:00 +01001030static bfd_udp_error_t
1031bfd_udp6_verify_transport (const ip6_header_t * ip6,
1032 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001033{
1034 const bfd_udp_session_t *bus = &bs->udp;
1035 const bfd_udp_key_t *key = &bus->key;
1036 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1037 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1038 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001039 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1040 format_ip6_address, ip6, format_ip6_address,
1041 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001042 return BFD_UDP_ERROR_BAD;
1043 }
1044 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1045 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1046 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001047 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1048 format_ip6_address, ip6, format_ip6_address,
1049 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001050 return BFD_UDP_ERROR_BAD;
1051 }
1052 const u8 expected_hop_limit = 255;
1053 if (ip6->hop_limit != expected_hop_limit)
1054 {
1055 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001056 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001057 return BFD_UDP_ERROR_BAD;
1058 }
Klement Sekera6f966492017-02-08 07:42:08 +01001059 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001060 {
1061 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001062 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001063 }
1064 return BFD_UDP_ERROR_NONE;
1065}
1066
Klement Sekerac5fccc02017-01-18 09:56:00 +01001067static bfd_udp_error_t
1068bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1069 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +01001070{
1071 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1072 if (sizeof (*pkt) > b->current_length)
1073 {
Klement Sekerac5fccc02017-01-18 09:56:00 +01001074 BFD_ERR
1075 ("Payload size %d too small to hold bfd packet of minimum size %d",
1076 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +01001077 return BFD_UDP_ERROR_BAD;
1078 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001079 ip6_header_t *ip6;
1080 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +01001081 bfd_udp6_find_headers (b, &ip6, &udp);
1082 if (!ip6 || !udp)
1083 {
1084 BFD_ERR ("Couldn't find ip6 or udp header");
1085 return BFD_UDP_ERROR_BAD;
1086 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001087 const u32 udp_payload_length = udp->length - sizeof (*udp);
1088 if (pkt->head.length > udp_payload_length)
1089 {
1090 BFD_ERR
1091 ("BFD packet length is larger than udp payload length (%u > %u)",
1092 pkt->head.length, udp_payload_length);
1093 return BFD_UDP_ERROR_BAD;
1094 }
Klement Sekera46a87ad2017-01-02 08:22:23 +01001095 if (!bfd_verify_pkt_common (pkt))
1096 {
1097 return BFD_UDP_ERROR_BAD;
1098 }
1099 bfd_session_t *bs = NULL;
1100 if (pkt->your_disc)
1101 {
1102 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001103 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001104 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1105 }
1106 else
1107 {
1108 bfd_udp_key_t key;
1109 memset (&key, 0, sizeof (key));
1110 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1111 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1112 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1113 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1114 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1115 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerab17dd962017-01-09 07:43:48 +01001116 "peer=%U)",
1117 key.sw_if_index, format_ip6_address, &key.local_addr,
1118 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001119 bs = bfd_lookup_session (&bfd_udp_main, &key);
1120 }
1121 if (!bs)
1122 {
1123 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1124 return BFD_UDP_ERROR_BAD;
1125 }
1126 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001127 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001128 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001129 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera46a87ad2017-01-02 08:22:23 +01001130 return BFD_UDP_ERROR_BAD;
1131 }
1132 bfd_udp_error_t err;
1133 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1134 {
1135 return err;
1136 }
1137 bfd_rpc_update_session (bs->bs_idx, pkt);
1138 *bs_out = bs;
1139 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001140}
1141
1142/*
1143 * Process a frame of bfd packets
1144 * Expect 1 packet / frame
1145 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001146static uword
1147bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1148 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001149{
1150 u32 n_left_from, *from;
1151 bfd_input_trace_t *t0;
1152
Klement Sekerac5fccc02017-01-18 09:56:00 +01001153 from = vlib_frame_vector_args (f); /* array of buffer indices */
1154 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001155
1156 while (n_left_from > 0)
1157 {
1158 u32 bi0;
1159 vlib_buffer_t *b0;
1160 u32 next0, error0;
1161
1162 bi0 = from[0];
1163 b0 = vlib_get_buffer (vm, bi0);
1164
1165 bfd_session_t *bs = NULL;
1166
1167 /* If this pkt is traced, snapshot the data */
1168 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001169 {
1170 int len;
1171 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1172 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1173 : sizeof (t0->data);
1174 t0->len = len;
1175 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1176 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001177
1178 /* scan this bfd pkt. error0 is the counter index to bmp */
1179 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001180 {
1181 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1182 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001183 else
Klement Sekerac5fccc02017-01-18 09:56:00 +01001184 {
1185 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1186 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001187 b0->error = rt->errors[error0];
1188
1189 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1190 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001191 {
Klement Sekera402ed312017-01-18 09:44:36 +01001192 /*
1193 * if everything went fine, check for poll bit, if present, re-use
1194 * the buffer and based on (now updated) session parameters, send
1195 * the final packet back
1196 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001197 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1198 if (bfd_pkt_get_poll (pkt))
1199 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001200 b0->current_data = 0;
1201 b0->current_length = 0;
1202 memset (vnet_buffer (b0), 0, sizeof (*vnet_buffer (b0)));
Klement Sekera239790f2017-02-16 10:53:53 +01001203 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main,
Klement Sekerae50e8562017-04-04 16:19:48 +02001204 bs, 0);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001205 if (is_ipv6)
1206 {
1207 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1208 b0->error, 1);
1209 }
1210 else
1211 {
1212 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1213 b0->error, 1);
1214 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001215 const bfd_udp_session_t *bus = &bs->udp;
1216 ip_adjacency_t *adj = adj_get (bus->adj_index);
1217 switch (adj->lookup_next_index)
1218 {
1219 case IP_LOOKUP_NEXT_ARP:
1220 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1221 break;
1222 case IP_LOOKUP_NEXT_REWRITE:
1223 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1224 break;
1225 default:
1226 /* drop */
1227 break;
1228 }
Klement Sekerac5fccc02017-01-18 09:56:00 +01001229 }
1230 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001231 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1232
1233 from += 1;
1234 n_left_from -= 1;
1235 }
1236
1237 return f->n_vectors;
1238}
1239
Klement Sekerac5fccc02017-01-18 09:56:00 +01001240static uword
1241bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001242{
1243 return bfd_udp_input (vm, rt, f, 0);
1244}
1245
1246/*
1247 * bfd input graph node declaration
1248 */
1249/* *INDENT-OFF* */
1250VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1251 .function = bfd_udp4_input,
1252 .name = "bfd-udp4-input",
1253 .vector_size = sizeof (u32),
1254 .type = VLIB_NODE_TYPE_INTERNAL,
1255
1256 .n_errors = BFD_UDP_N_ERROR,
1257 .error_strings = bfd_udp_error_strings,
1258
1259 .format_trace = bfd_input_format_trace,
1260
1261 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1262 .next_nodes =
1263 {
1264 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001265 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1266 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001267 },
1268};
1269/* *INDENT-ON* */
1270
Klement Sekerac5fccc02017-01-18 09:56:00 +01001271static uword
1272bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001273{
1274 return bfd_udp_input (vm, rt, f, 1);
1275}
1276
1277/* *INDENT-OFF* */
1278VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1279 .function = bfd_udp6_input,
1280 .name = "bfd-udp6-input",
1281 .vector_size = sizeof (u32),
1282 .type = VLIB_NODE_TYPE_INTERNAL,
1283
1284 .n_errors = BFD_UDP_N_ERROR,
1285 .error_strings = bfd_udp_error_strings,
1286
1287 .format_trace = bfd_input_format_trace,
1288
1289 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1290 .next_nodes =
1291 {
1292 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001293 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1294 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001295 },
1296};
1297/* *INDENT-ON* */
1298
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001299/*
1300 * Process a frame of bfd echo packets
1301 * Expect 1 packet / frame
1302 */
1303static uword
1304bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1305 vlib_frame_t * f, int is_ipv6)
1306{
1307 u32 n_left_from, *from;
1308 bfd_input_trace_t *t0;
1309
1310 from = vlib_frame_vector_args (f); /* array of buffer indices */
1311 n_left_from = f->n_vectors; /* number of buffer indices */
1312
1313 while (n_left_from > 0)
1314 {
1315 u32 bi0;
1316 vlib_buffer_t *b0;
1317 u32 next0;
1318
1319 bi0 = from[0];
1320 b0 = vlib_get_buffer (vm, bi0);
1321
1322 /* If this pkt is traced, snapshot the data */
1323 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1324 {
1325 int len;
1326 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1327 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1328 : sizeof (t0->data);
1329 t0->len = len;
1330 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1331 }
1332
Klement Sekera239790f2017-02-16 10:53:53 +01001333 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1334 {
1335 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1336 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1337 }
1338 else
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001339 {
1340 /* loop back the packet */
1341 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1342 if (is_ipv6)
1343 {
1344 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1345 b0->error, 1);
1346 }
1347 else
1348 {
1349 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1350 b0->error, 1);
1351 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001352 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001353 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001354
1355 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1356
1357 from += 1;
1358 n_left_from -= 1;
1359 }
1360
1361 return f->n_vectors;
1362}
1363
1364static uword
1365bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1366 vlib_frame_t * f)
1367{
1368 return bfd_udp_echo_input (vm, rt, f, 0);
1369}
1370
1371u8 *
1372bfd_echo_input_format_trace (u8 * s, va_list * args)
1373{
1374 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1375 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1376 const bfd_udp_echo_input_trace_t *t =
1377 va_arg (*args, bfd_udp_echo_input_trace_t *);
1378 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1379 {
1380 s = format (s, "BFD ECHO:\n");
1381 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1382 }
1383
1384 return s;
1385}
1386
1387/*
1388 * bfd input graph node declaration
1389 */
1390/* *INDENT-OFF* */
1391VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1392 .function = bfd_udp_echo4_input,
1393 .name = "bfd-udp-echo4-input",
1394 .vector_size = sizeof (u32),
1395 .type = VLIB_NODE_TYPE_INTERNAL,
1396
1397 .n_errors = BFD_UDP_ECHO_N_ERROR,
1398 .error_strings = bfd_udp_error_strings,
1399
1400 .format_trace = bfd_echo_input_format_trace,
1401
1402 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1403 .next_nodes =
1404 {
1405 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001406 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1407 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001408 },
1409};
1410/* *INDENT-ON* */
1411
1412static uword
1413bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1414 vlib_frame_t * f)
1415{
1416 return bfd_udp_echo_input (vm, rt, f, 1);
1417}
1418
1419/* *INDENT-OFF* */
1420VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1421 .function = bfd_udp_echo6_input,
1422 .name = "bfd-udp-echo6-input",
1423 .vector_size = sizeof (u32),
1424 .type = VLIB_NODE_TYPE_INTERNAL,
1425
1426 .n_errors = BFD_UDP_ECHO_N_ERROR,
1427 .error_strings = bfd_udp_echo_error_strings,
1428
1429 .format_trace = bfd_echo_input_format_trace,
1430
1431 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1432 .next_nodes =
1433 {
1434 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
Klement Sekerae50e8562017-04-04 16:19:48 +02001435 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1436 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001437 },
1438};
1439
1440/* *INDENT-ON* */
1441
Klement Sekerac5fccc02017-01-18 09:56:00 +01001442static clib_error_t *
1443bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001444{
1445 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1446 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1447 {
1448 /* TODO */
1449 }
1450 return 0;
1451}
1452
1453VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1454
Klement Sekerac5fccc02017-01-18 09:56:00 +01001455static clib_error_t *
1456bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001457{
1458 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1459 {
1460 /* TODO */
1461 }
1462 return 0;
1463}
1464
1465VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1466
1467/*
1468 * setup function
1469 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001470static clib_error_t *
1471bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001472{
1473 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +01001474 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001475 bfd_udp_main.bfd_main = &bfd_main;
Klement Sekera239790f2017-02-16 10:53:53 +01001476 bfd_udp_main.vnet_main = vnet_get_main ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001477 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1478 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001479 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1480 bfd_udp_echo4_input_node.index, 1);
1481 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1482 bfd_udp_echo6_input_node.index, 0);
Klement Sekerae50e8562017-04-04 16:19:48 +02001483 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1484 ASSERT (node);
1485 bfd_udp_main.ip4_arp_idx = node->index;
1486 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1487 ASSERT (node);
1488 bfd_udp_main.ip6_ndp_idx = node->index;
1489 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1490 ASSERT (node);
1491 bfd_udp_main.ip4_rewrite_idx = node->index;
1492 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1493 ASSERT (node);
1494 bfd_udp_main.ip6_rewrite_idx = node->index;
1495
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001496 return 0;
1497}
1498
1499VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001500
1501/*
1502 * fd.io coding-style-patch-verification: ON
1503 *
1504 * Local Variables:
1505 * eval: (c-set-style "gnu")
1506 * End:
1507 */