blob: b3eabc9cda9c932867bbbe7cf7cf17534bd77fca [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 Sekera0e3c0de2016-09-29 14:43:44 +020054} bfd_udp_main_t;
55
Klement Sekerae4504c62016-12-08 10:16:41 +010056static vlib_node_registration_t bfd_udp4_input_node;
57static vlib_node_registration_t bfd_udp6_input_node;
Klement Sekeraaeeac3b2017-02-14 07:11:52 +010058static vlib_node_registration_t bfd_udp_echo4_input_node;
59static vlib_node_registration_t bfd_udp_echo6_input_node;
Klement Sekerae4504c62016-12-08 10:16:41 +010060
Klement Sekera0e3c0de2016-09-29 14:43:44 +020061bfd_udp_main_t bfd_udp_main;
62
Klement Sekera239790f2017-02-16 10:53:53 +010063vnet_api_error_t
64bfd_udp_set_echo_source (u32 sw_if_index)
65{
66 vnet_sw_interface_t *sw_if =
Klement Sekera73884482017-02-23 09:26:30 +010067 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera239790f2017-02-16 10:53:53 +010068 if (sw_if)
69 {
70 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
71 bfd_udp_main.echo_source_is_set = 1;
72 return 0;
73 }
74 return VNET_API_ERROR_BFD_ENOENT;
75}
76
77vnet_api_error_t
78bfd_udp_del_echo_source (u32 sw_if_index)
79{
80 bfd_udp_main.echo_source_sw_if_index = ~0;
81 bfd_udp_main.echo_source_is_set = 0;
82 return 0;
83}
84
85int
86bfd_udp_is_echo_available (bfd_transport_e transport)
87{
88 if (!bfd_udp_main.echo_source_is_set)
89 {
Klement Sekera73884482017-02-23 09:26:30 +010090 BFD_DBG ("UDP echo source not set - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +010091 return 0;
92 }
93 /*
94 * for the echo to work, we need a loopback interface with at least one
95 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
96 * pick an unused address from that subnet
97 */
98 vnet_sw_interface_t *sw_if =
99 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main,
100 bfd_udp_main.echo_source_sw_if_index);
101 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
102 {
103 if (BFD_TRANSPORT_UDP4 == transport)
104 {
105 ip4_main_t *im = &ip4_main;
106 ip_interface_address_t *ia = NULL;
107 /* *INDENT-OFF* */
108 foreach_ip_interface_address (&im->lookup_main, ia,
109 bfd_udp_main.echo_source_sw_if_index,
110 0 /* honor unnumbered */, ({
111 if (ia->address_length <= 31)
112 {
113 return 1;
114 }
115 }));
116 /* *INDENT-ON* */
117 }
118 else if (BFD_TRANSPORT_UDP6 == transport)
119 {
120 ip6_main_t *im = &ip6_main;
121 ip_interface_address_t *ia = NULL;
122 /* *INDENT-OFF* */
123 foreach_ip_interface_address (&im->lookup_main, ia,
124 bfd_udp_main.echo_source_sw_if_index,
125 0 /* honor unnumbered */, ({
126 if (ia->address_length <= 127)
127 {
128 return 1;
129 }
130 }));
131 /* *INDENT-ON* */
132 }
133 }
Klement Sekera73884482017-02-23 09:26:30 +0100134 BFD_DBG ("No usable IP address for UDP echo - echo not available");
Klement Sekera239790f2017-02-16 10:53:53 +0100135 return 0;
136}
137
Klement Sekera2bce0332017-02-09 06:03:46 +0100138static u16
139bfd_udp_bs_idx_to_sport (u32 bs_idx)
140{
141 /* The source port MUST be in the range 49152 through 65535. The same UDP
142 * source port number MUST be used for all BFD Control packets associated
143 * with a particular session. The source port number SHOULD be unique among
144 * all BFD sessions on the system. If more than 16384 BFD sessions are
145 * simultaneously active, UDP source port numbers MAY be reused on
146 * multiple sessions, but the number of distinct uses of the same UDP
147 * source port number SHOULD be minimized.
148 */
149 return 49152 + bs_idx % (65535 - 49152 + 1);
150}
151
Klement Sekera239790f2017-02-16 10:53:53 +0100152int
153bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
154{
155 if (!bfd_udp_main.echo_source_is_set)
156 {
157 BFD_ERR ("cannot find ip4 address, echo source not set");
158 return 0;
159 }
160 ip_interface_address_t *ia = NULL;
161 ip4_main_t *im = &ip4_main;
162
163 /* *INDENT-OFF* */
164 foreach_ip_interface_address (
165 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
166 0 /* honor unnumbered */, ({
167 ip4_address_t *x =
168 ip_interface_address_get_address (&im->lookup_main, ia);
169 if (ia->address_length <= 31)
170 {
171 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
172 /*
173 * flip the last bit to get a different address, might be network,
174 * we don't care ...
175 */
176 addr->as_u32 ^= 1;
177 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
178 return 1;
179 }
180 }));
181 /* *INDENT-ON* */
182 BFD_ERR ("cannot find ip4 address, no usable address found");
183 return 0;
184}
185
186int
187bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
188{
189 if (!bfd_udp_main.echo_source_is_set)
190 {
191 BFD_ERR ("cannot find ip6 address, echo source not set");
192 return 0;
193 }
194 ip_interface_address_t *ia = NULL;
195 ip6_main_t *im = &ip6_main;
196
197 /* *INDENT-OFF* */
198 foreach_ip_interface_address (
199 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
200 0 /* honor unnumbered */, ({
201 ip6_address_t *x =
202 ip_interface_address_get_address (&im->lookup_main, ia);
203 if (ia->address_length <= 127)
204 {
205 *addr = *x;
206 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
Klement Sekera239790f2017-02-16 10:53:53 +0100207 return 1;
208 }
209 }));
210 /* *INDENT-ON* */
211 BFD_ERR ("cannot find ip6 address, no usable address found");
212 return 0;
213}
214
Klement Sekera73884482017-02-23 09:26:30 +0100215void
216bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index, int *have_usable_ip4,
217 ip4_address_t * ip4, int *have_usable_ip6,
218 ip6_address_t * ip6)
219{
220 if (bfd_udp_main.echo_source_is_set)
221 {
222 *is_set = 1;
223 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
224 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
225 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
226 }
227 else
228 {
229 *is_set = 0;
230 }
231}
232
Klement Sekera239790f2017-02-16 10:53:53 +0100233int
Klement Sekerac5fccc02017-01-18 09:56:00 +0100234bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekera239790f2017-02-16 10:53:53 +0100235 const bfd_session_t * bs, int is_echo)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200236{
Klement Sekera2bce0332017-02-09 06:03:46 +0100237 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100238 const bfd_udp_key_t *key = &bus->key;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200239
240 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200241 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
242 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekera402ed312017-01-18 09:44:36 +0100243 typedef struct
244 {
245 ip4_header_t ip4;
246 udp_header_t udp;
247 } ip4_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100248 ip4_udp_headers *headers = NULL;
Klement Sekera402ed312017-01-18 09:44:36 +0100249 vlib_buffer_advance (b, -sizeof (*headers));
250 headers = vlib_buffer_get_current (b);
251 memset (headers, 0, sizeof (*headers));
252 headers->ip4.ip_version_and_header_length = 0x45;
253 headers->ip4.ttl = 255;
254 headers->ip4.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100255 headers->udp.src_port =
256 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100257 if (is_echo)
258 {
259 int rv;
260 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
261 {
262 return rv;
263 }
264 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
265 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
266 }
267 else
268 {
269 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
270 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
271 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
272 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100273
274 /* fix ip length, checksum and udp length */
275 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
276
Klement Sekera402ed312017-01-18 09:44:36 +0100277 headers->ip4.length = clib_host_to_net_u16 (ip_length);
278 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100279
Klement Sekera402ed312017-01-18 09:44:36 +0100280 const u16 udp_length = ip_length - (sizeof (headers->ip4));
281 headers->udp.length = clib_host_to_net_u16 (udp_length);
Klement Sekera239790f2017-02-16 10:53:53 +0100282 return 1;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100283}
284
Klement Sekera239790f2017-02-16 10:53:53 +0100285int
Klement Sekerac5fccc02017-01-18 09:56:00 +0100286bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekera239790f2017-02-16 10:53:53 +0100287 const bfd_session_t * bs, int is_echo)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100288{
Klement Sekera2bce0332017-02-09 06:03:46 +0100289 const bfd_udp_session_t *bus = &bs->udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100290 const bfd_udp_key_t *key = &bus->key;
291
292 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
293 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
294 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekera402ed312017-01-18 09:44:36 +0100295 typedef struct
296 {
297 ip6_header_t ip6;
298 udp_header_t udp;
299 } ip6_udp_headers;
Klement Sekerab17dd962017-01-09 07:43:48 +0100300 ip6_udp_headers *headers = NULL;
301 vlib_buffer_advance (b, -sizeof (*headers));
302 headers = vlib_buffer_get_current (b);
Klement Sekera402ed312017-01-18 09:44:36 +0100303 memset (headers, 0, sizeof (*headers));
304 headers->ip6.ip_version_traffic_class_and_flow_label =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100305 clib_host_to_net_u32 (0x6 << 28);
Klement Sekera402ed312017-01-18 09:44:36 +0100306 headers->ip6.hop_limit = 255;
307 headers->ip6.protocol = IP_PROTOCOL_UDP;
Klement Sekera2bce0332017-02-09 06:03:46 +0100308 headers->udp.src_port =
309 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
Klement Sekera239790f2017-02-16 10:53:53 +0100310 if (is_echo)
311 {
312 int rv;
313 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
314 {
315 return rv;
316 }
317 clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6,
318 sizeof (headers->ip6.dst_address));
319
320 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
321 }
322 else
323 {
324 clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6,
325 sizeof (headers->ip6.src_address));
326 clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6,
327 sizeof (headers->ip6.dst_address));
328 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
329 }
Klement Sekera46a87ad2017-01-02 08:22:23 +0100330
331 /* fix ip payload length and udp length */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100332 const u16 udp_length =
Klement Sekera402ed312017-01-18 09:44:36 +0100333 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
334 headers->udp.length = clib_host_to_net_u16 (udp_length);
335 headers->ip6.payload_length = headers->udp.length;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100336
337 /* IPv6 UDP checksum is mandatory */
338 int bogus = 0;
Klement Sekera402ed312017-01-18 09:44:36 +0100339 headers->udp.checksum =
340 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100341 ASSERT (bogus == 0);
Klement Sekera402ed312017-01-18 09:44:36 +0100342 if (headers->udp.checksum == 0)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100343 {
Klement Sekera402ed312017-01-18 09:44:36 +0100344 headers->udp.checksum = 0xffff;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100345 }
Klement Sekera239790f2017-02-16 10:53:53 +0100346 return 1;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200347}
348
Klement Sekerac5fccc02017-01-18 09:56:00 +0100349static bfd_session_t *
350bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200351{
352 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
353 if (p)
354 {
355 return bfd_find_session_by_idx (bum->bfd_main, *p);
356 }
357 return 0;
358}
359
Klement Sekerab17dd962017-01-09 07:43:48 +0100360static void
361bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
362 const ip46_address_t * local_addr,
363 const ip46_address_t * peer_addr)
364{
365 memset (key, 0, sizeof (*key));
366 key->sw_if_index = sw_if_index;
367 key->local_addr.as_u64[0] = local_addr->as_u64[0];
368 key->local_addr.as_u64[1] = local_addr->as_u64[1];
369 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
370 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
371}
372
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200373static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100374bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100375 u32 desired_min_tx_usec,
376 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100377 const ip46_address_t * local_addr,
378 const ip46_address_t * peer_addr,
Klement Sekerab17dd962017-01-09 07:43:48 +0100379 bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200380{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200381 /* get a pool entry and if we end up not needing it, give it back */
Klement Sekera239790f2017-02-16 10:53:53 +0100382 bfd_transport_e t = BFD_TRANSPORT_UDP4;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200383 if (!ip46_address_is_ip4 (local_addr))
384 {
385 t = BFD_TRANSPORT_UDP6;
386 }
387 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
Klement Sekera239790f2017-02-16 10:53:53 +0100388 if (!bs)
389 {
390 bfd_put_session (bum->bfd_main, bs);
391 return VNET_API_ERROR_BFD_EAGAIN;
392 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200393 bfd_udp_session_t *bus = &bs->udp;
394 memset (bus, 0, sizeof (*bus));
395 bfd_udp_key_t *key = &bus->key;
Klement Sekerab17dd962017-01-09 07:43:48 +0100396 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200397 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
398 if (tmp)
399 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100400 clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
401 tmp->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200402 bfd_put_session (bum->bfd_main, bs);
403 return VNET_API_ERROR_BFD_EEXIST;
404 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200405 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
406 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100407 bs->bs_idx, key->sw_if_index, format_ip46_address,
408 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
409 &key->peer_addr, IP46_TYPE_ANY);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200410 if (BFD_TRANSPORT_UDP4 == t)
411 {
412 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100413 &key->peer_addr,
414 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200415 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100416 "returns %d", format_ip46_address, &key->peer_addr,
417 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200418 }
419 else
420 {
421 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100422 &key->peer_addr,
423 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200424 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100425 "returns %d", format_ip46_address, &key->peer_addr,
426 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200427 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100428 *bs_out = bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100429 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
430 required_min_rx_usec, detect_mult);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200431}
432
433static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100434bfd_udp_validate_api_input (u32 sw_if_index,
435 const ip46_address_t * local_addr,
436 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200437{
438 vnet_sw_interface_t *sw_if =
Klement Sekera239790f2017-02-16 10:53:53 +0100439 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200440 u8 local_ip_valid = 0;
441 ip_interface_address_t *ia = NULL;
442 if (!sw_if)
443 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100444 clib_warning ("got NULL sw_if");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200445 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
446 }
447 if (ip46_address_is_ip4 (local_addr))
448 {
449 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100450 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100451 clib_warning ("IP family mismatch");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100452 return VNET_API_ERROR_INVALID_ARGUMENT;
453 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200454 ip4_main_t *im = &ip4_main;
455
456 /* *INDENT-OFF* */
457 foreach_ip_interface_address (
458 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
459 ip4_address_t *x =
460 ip_interface_address_get_address (&im->lookup_main, ia);
461 if (x->as_u32 == local_addr->ip4.as_u32)
462 {
463 /* valid address for this interface */
464 local_ip_valid = 1;
465 break;
466 }
467 }));
468 /* *INDENT-ON* */
469 }
470 else
471 {
472 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100473 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100474 clib_warning ("IP family mismatch");
Klement Sekerac5fccc02017-01-18 09:56:00 +0100475 return VNET_API_ERROR_INVALID_ARGUMENT;
476 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200477 ip6_main_t *im = &ip6_main;
478 /* *INDENT-OFF* */
479 foreach_ip_interface_address (
480 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
481 ip6_address_t *x =
482 ip_interface_address_get_address (&im->lookup_main, ia);
483 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
Klement Sekerab17dd962017-01-09 07:43:48 +0100484 local_addr->ip6.as_u64[1] == x->as_u64[1])
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200485 {
486 /* valid address for this interface */
487 local_ip_valid = 1;
488 break;
489 }
490 }));
491 /* *INDENT-ON* */
492 }
493
494 if (!local_ip_valid)
495 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100496 clib_warning ("address not found on interface");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200497 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
498 }
499
500 return 0;
501}
502
Klement Sekerab17dd962017-01-09 07:43:48 +0100503static vnet_api_error_t
504bfd_udp_find_session_by_api_input (u32 sw_if_index,
505 const ip46_address_t * local_addr,
506 const ip46_address_t * peer_addr,
507 bfd_session_t ** bs_out)
508{
509 vnet_api_error_t rv =
510 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
511 if (!rv)
512 {
513 bfd_udp_main_t *bum = &bfd_udp_main;
514 bfd_udp_key_t key;
515 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
516 bfd_session_t *bs = bfd_lookup_session (bum, &key);
517 if (bs)
518 {
519 *bs_out = bs;
520 }
521 else
522 {
523 clib_warning
524 ("BFD session not found (sw_if_index=%u, local=%U, peer=%U",
525 sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY,
526 format_ip46_address, peer_addr, IP46_TYPE_ANY);
527 return VNET_API_ERROR_BFD_ENOENT;
528 }
529 }
530 return rv;
531}
532
533static vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100534bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
535 u32 required_min_rx_usec, u8 detect_mult,
Klement Sekerab17dd962017-01-09 07:43:48 +0100536 const ip46_address_t * local_addr,
537 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200538{
539 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100540 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200541 if (rv)
542 {
543 return rv;
544 }
545 if (detect_mult < 1)
546 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100547 clib_warning ("detect_mult < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200548 return VNET_API_ERROR_INVALID_ARGUMENT;
549 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100550 if (desired_min_tx_usec < 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200551 {
Klement Sekeraa57a9702017-02-02 06:58:07 +0100552 clib_warning ("desired_min_tx_usec < 1");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200553 return VNET_API_ERROR_INVALID_ARGUMENT;
554 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100555 return 0;
556}
557
558static void
559bfd_udp_del_session_internal (bfd_session_t * bs)
560{
561 bfd_udp_main_t *bum = &bfd_udp_main;
562 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
563 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
564 adj_unlock (bs->udp.adj_index);
565 bfd_put_session (bum->bfd_main, bs);
566}
567
568vnet_api_error_t
569bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100570 const ip46_address_t * peer_addr,
571 u32 desired_min_tx_usec, u32 required_min_rx_usec,
572 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
573 u8 bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100574{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100575 vnet_api_error_t rv =
576 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
577 required_min_rx_usec, detect_mult,
578 local_addr, peer_addr);
Klement Sekerab17dd962017-01-09 07:43:48 +0100579 bfd_session_t *bs = NULL;
580 if (!rv)
581 {
582 rv =
583 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
Klement Sekeraa57a9702017-02-02 06:58:07 +0100584 desired_min_tx_usec,
585 required_min_rx_usec, detect_mult,
586 local_addr, peer_addr, &bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100587 }
588 if (!rv && is_authenticated)
589 {
590#if WITH_LIBSSL > 0
591 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
592 0 /* is not delayed */ );
593#else
594 clib_warning ("SSL missing, cannot add authenticated BFD session");
595 rv = VNET_API_ERROR_BFD_NOTSUPP;
596#endif
597 if (rv)
598 {
599 bfd_udp_del_session_internal (bs);
600 }
601 }
602 if (!rv)
603 {
604 bfd_session_start (bfd_udp_main.bfd_main, bs);
605 }
606
607 return rv;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200608}
609
Klement Sekerac5fccc02017-01-18 09:56:00 +0100610vnet_api_error_t
Klement Sekeraa57a9702017-02-02 06:58:07 +0100611bfd_udp_mod_session (u32 sw_if_index,
612 const ip46_address_t * local_addr,
613 const ip46_address_t * peer_addr,
614 u32 desired_min_tx_usec,
615 u32 required_min_rx_usec, u8 detect_mult)
616{
617 bfd_session_t *bs = NULL;
618 vnet_api_error_t rv =
619 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
620 &bs);
621 if (rv)
622 {
623 return rv;
624 }
625
626 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
627 desired_min_tx_usec, required_min_rx_usec,
628 detect_mult);
629}
630
631vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100632bfd_udp_del_session (u32 sw_if_index,
633 const ip46_address_t * local_addr,
634 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200635{
Klement Sekerab17dd962017-01-09 07:43:48 +0100636 bfd_session_t *bs = NULL;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200637 vnet_api_error_t rv =
Klement Sekerab17dd962017-01-09 07:43:48 +0100638 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
639 &bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200640 if (rv)
641 {
642 return rv;
643 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100644 bfd_udp_del_session_internal (bs);
645 return 0;
646}
647
648vnet_api_error_t
649bfd_udp_session_set_flags (u32 sw_if_index,
650 const ip46_address_t * local_addr,
651 const ip46_address_t * peer_addr, u8 admin_up_down)
652{
653 bfd_session_t *bs = NULL;
654 vnet_api_error_t rv =
655 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
656 &bs);
657 if (rv)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200658 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100659 return rv;
660 }
661 bfd_session_set_flags (bs, admin_up_down);
662 return 0;
663}
664
665vnet_api_error_t
Klement Sekerab17dd962017-01-09 07:43:48 +0100666bfd_udp_auth_activate (u32 sw_if_index,
667 const ip46_address_t * local_addr,
668 const ip46_address_t * peer_addr,
669 u32 conf_key_id, u8 key_id, u8 is_delayed)
670{
671#if WITH_LIBSSL > 0
672 bfd_session_t *bs = NULL;
673 vnet_api_error_t rv =
674 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
675 &bs);
676 if (rv)
677 {
678 return rv;
679 }
680 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
681#else
682 clib_warning ("SSL missing, cannot activate BFD authentication");
683 return VNET_API_ERROR_BFD_NOTSUPP;
684#endif
685}
686
687vnet_api_error_t
688bfd_udp_auth_deactivate (u32 sw_if_index,
689 const ip46_address_t * local_addr,
690 const ip46_address_t * peer_addr, u8 is_delayed)
691{
692 bfd_session_t *bs = NULL;
693 vnet_api_error_t rv =
694 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
695 &bs);
696 if (rv)
697 {
698 return rv;
699 }
700 return bfd_auth_deactivate (bs, is_delayed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200701}
702
Klement Sekerac5fccc02017-01-18 09:56:00 +0100703typedef enum
704{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200705 BFD_UDP_INPUT_NEXT_NORMAL,
706 BFD_UDP_INPUT_NEXT_REPLY,
707 BFD_UDP_INPUT_N_NEXT,
708} bfd_udp_input_next_t;
709
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100710/* Packet counters - BFD control frames */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200711#define foreach_bfd_udp_error(F) \
712 F (NONE, "good bfd packets (processed)") \
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100713 F (BAD, "invalid bfd packets")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200714
715#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
716foreach_bfd_udp_error (F);
717#undef F
718
719static char *bfd_udp_error_strings[] = {
720#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
721 foreach_bfd_udp_error (F)
722#undef F
723};
724
Klement Sekerac5fccc02017-01-18 09:56:00 +0100725typedef enum
726{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200727#define F(sym, str) BFD_UDP_ERROR_##sym,
728 foreach_bfd_udp_error (F)
729#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100730 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200731} bfd_udp_error_t;
732
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100733/* Packet counters - BFD ECHO packets */
734#define foreach_bfd_udp_echo_error(F) \
735 F (NONE, "good bfd echo packets (processed)") \
736 F (BAD, "invalid bfd echo packets")
737
738#define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
739foreach_bfd_udp_echo_error (F);
740#undef F
741
742static char *bfd_udp_echo_error_strings[] = {
743#define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
744 foreach_bfd_udp_echo_error (F)
745#undef F
746};
747
748typedef enum
749{
750#define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
751 foreach_bfd_udp_echo_error (F)
752#undef F
753 BFD_UDP_ECHO_N_ERROR,
754} bfd_udp_echo_error_t;
755
Klement Sekerac5fccc02017-01-18 09:56:00 +0100756static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100757bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
758 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200759{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100760 /* sanity check first */
761 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
762 if (start < 0 && start < sizeof (b->pre_data))
763 {
764 BFD_ERR ("Start of ip header is before pre_data, ignoring");
765 *ip4 = NULL;
766 *udp = NULL;
767 return;
768 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100769 *ip4 = (ip4_header_t *) (b->data + start);
770 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100771 {
772 BFD_ERR ("Start of ip header is beyond current data, ignoring");
773 *ip4 = NULL;
774 *udp = NULL;
775 return;
776 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100777 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200778}
779
Klement Sekerac5fccc02017-01-18 09:56:00 +0100780static bfd_udp_error_t
781bfd_udp4_verify_transport (const ip4_header_t * ip4,
782 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200783{
784 const bfd_udp_session_t *bus = &bs->udp;
785 const bfd_udp_key_t *key = &bus->key;
786 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
787 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100788 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100789 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
790 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200791 return BFD_UDP_ERROR_BAD;
792 }
793 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
794 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100795 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100796 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
797 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200798 return BFD_UDP_ERROR_BAD;
799 }
800 const u8 expected_ttl = 255;
801 if (ip4->ttl != expected_ttl)
802 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100803 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100804 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200805 return BFD_UDP_ERROR_BAD;
806 }
Klement Sekera6f966492017-02-08 07:42:08 +0100807 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200808 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100809 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100810 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200811 }
812 return BFD_UDP_ERROR_NONE;
813}
814
815typedef struct
816{
817 u32 bs_idx;
818 bfd_pkt_t pkt;
819} bfd_rpc_update_t;
820
Klement Sekerac5fccc02017-01-18 09:56:00 +0100821static void
822bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200823{
824 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
825}
826
Klement Sekerac5fccc02017-01-18 09:56:00 +0100827static void
828bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200829{
830 /* packet length was already verified to be correct by the caller */
831 const u32 data_size = sizeof (bfd_rpc_update_t) -
Klement Sekerac5fccc02017-01-18 09:56:00 +0100832 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200833 u8 data[data_size];
Klement Sekerac5fccc02017-01-18 09:56:00 +0100834 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200835 update->bs_idx = bs_idx;
836 clib_memcpy (&update->pkt, pkt, pkt->head.length);
837 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
838}
839
Klement Sekerac5fccc02017-01-18 09:56:00 +0100840static bfd_udp_error_t
841bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
842 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200843{
844 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
845 if (sizeof (*pkt) > b->current_length)
846 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100847 BFD_ERR
848 ("Payload size %d too small to hold bfd packet of minimum size %d",
849 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200850 return BFD_UDP_ERROR_BAD;
851 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100852 ip4_header_t *ip4;
853 udp_header_t *udp;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200854 bfd_udp4_find_headers (b, &ip4, &udp);
855 if (!ip4 || !udp)
856 {
857 BFD_ERR ("Couldn't find ip4 or udp header");
858 return BFD_UDP_ERROR_BAD;
859 }
Klement Sekerab17dd962017-01-09 07:43:48 +0100860 const u32 udp_payload_length = udp->length - sizeof (*udp);
861 if (pkt->head.length > udp_payload_length)
862 {
863 BFD_ERR
864 ("BFD packet length is larger than udp payload length (%u > %u)",
865 pkt->head.length, udp_payload_length);
866 return BFD_UDP_ERROR_BAD;
867 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200868 if (!bfd_verify_pkt_common (pkt))
869 {
870 return BFD_UDP_ERROR_BAD;
871 }
872 bfd_session_t *bs = NULL;
873 if (pkt->your_disc)
874 {
875 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100876 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200877 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
878 }
879 else
880 {
881 bfd_udp_key_t key;
882 memset (&key, 0, sizeof (key));
883 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
884 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
885 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
886 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100887 "peer=%U)",
888 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
889 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200890 bs = bfd_lookup_session (&bfd_udp_main, &key);
891 }
892 if (!bs)
893 {
894 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
895 return BFD_UDP_ERROR_BAD;
896 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100897 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +0100898 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200899 {
Klement Sekerab17dd962017-01-09 07:43:48 +0100900 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200901 return BFD_UDP_ERROR_BAD;
902 }
903 bfd_udp_error_t err;
904 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
905 {
906 return err;
907 }
908 bfd_rpc_update_session (bs->bs_idx, pkt);
909 *bs_out = bs;
910 return BFD_UDP_ERROR_NONE;
911}
912
Klement Sekerac5fccc02017-01-18 09:56:00 +0100913static void
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100914bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
915 udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200916{
Klement Sekera46a87ad2017-01-02 08:22:23 +0100917 /* sanity check first */
918 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
919 if (start < 0 && start < sizeof (b->pre_data))
920 {
921 BFD_ERR ("Start of ip header is before pre_data, ignoring");
922 *ip6 = NULL;
923 *udp = NULL;
924 return;
925 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100926 *ip6 = (ip6_header_t *) (b->data + start);
927 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +0100928 {
929 BFD_ERR ("Start of ip header is beyond current data, ignoring");
930 *ip6 = NULL;
931 *udp = NULL;
932 return;
933 }
Klement Sekera402ed312017-01-18 09:44:36 +0100934 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
935 {
936 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
Klement Sekerab17dd962017-01-09 07:43:48 +0100937 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
Klement Sekera402ed312017-01-18 09:44:36 +0100938 *ip6 = NULL;
939 *udp = NULL;
940 return;
941 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100942 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100943}
944
Klement Sekerac5fccc02017-01-18 09:56:00 +0100945static bfd_udp_error_t
946bfd_udp6_verify_transport (const ip6_header_t * ip6,
947 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100948{
949 const bfd_udp_session_t *bus = &bs->udp;
950 const bfd_udp_key_t *key = &bus->key;
951 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
952 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
953 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100954 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
955 format_ip6_address, ip6, format_ip6_address,
956 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100957 return BFD_UDP_ERROR_BAD;
958 }
959 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
960 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
961 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100962 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
963 format_ip6_address, ip6, format_ip6_address,
964 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100965 return BFD_UDP_ERROR_BAD;
966 }
967 const u8 expected_hop_limit = 255;
968 if (ip6->hop_limit != expected_hop_limit)
969 {
970 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100971 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100972 return BFD_UDP_ERROR_BAD;
973 }
Klement Sekera6f966492017-02-08 07:42:08 +0100974 if (clib_net_to_host_u16 (udp->src_port) < 49152)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100975 {
976 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100977 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100978 }
979 return BFD_UDP_ERROR_NONE;
980}
981
Klement Sekerac5fccc02017-01-18 09:56:00 +0100982static bfd_udp_error_t
983bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
984 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100985{
986 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
987 if (sizeof (*pkt) > b->current_length)
988 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100989 BFD_ERR
990 ("Payload size %d too small to hold bfd packet of minimum size %d",
991 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100992 return BFD_UDP_ERROR_BAD;
993 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100994 ip6_header_t *ip6;
995 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100996 bfd_udp6_find_headers (b, &ip6, &udp);
997 if (!ip6 || !udp)
998 {
999 BFD_ERR ("Couldn't find ip6 or udp header");
1000 return BFD_UDP_ERROR_BAD;
1001 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001002 const u32 udp_payload_length = udp->length - sizeof (*udp);
1003 if (pkt->head.length > udp_payload_length)
1004 {
1005 BFD_ERR
1006 ("BFD packet length is larger than udp payload length (%u > %u)",
1007 pkt->head.length, udp_payload_length);
1008 return BFD_UDP_ERROR_BAD;
1009 }
Klement Sekera46a87ad2017-01-02 08:22:23 +01001010 if (!bfd_verify_pkt_common (pkt))
1011 {
1012 return BFD_UDP_ERROR_BAD;
1013 }
1014 bfd_session_t *bs = NULL;
1015 if (pkt->your_disc)
1016 {
1017 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +01001018 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001019 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1020 }
1021 else
1022 {
1023 bfd_udp_key_t key;
1024 memset (&key, 0, sizeof (key));
1025 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1026 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1027 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1028 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1029 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1030 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerab17dd962017-01-09 07:43:48 +01001031 "peer=%U)",
1032 key.sw_if_index, format_ip6_address, &key.local_addr,
1033 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001034 bs = bfd_lookup_session (&bfd_udp_main, &key);
1035 }
1036 if (!bs)
1037 {
1038 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1039 return BFD_UDP_ERROR_BAD;
1040 }
1041 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekerab17dd962017-01-09 07:43:48 +01001042 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
Klement Sekera46a87ad2017-01-02 08:22:23 +01001043 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001044 BFD_ERR ("Packet verification failed, dropping packet");
Klement Sekera46a87ad2017-01-02 08:22:23 +01001045 return BFD_UDP_ERROR_BAD;
1046 }
1047 bfd_udp_error_t err;
1048 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1049 {
1050 return err;
1051 }
1052 bfd_rpc_update_session (bs->bs_idx, pkt);
1053 *bs_out = bs;
1054 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001055}
1056
1057/*
1058 * Process a frame of bfd packets
1059 * Expect 1 packet / frame
1060 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001061static uword
1062bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1063 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001064{
1065 u32 n_left_from, *from;
1066 bfd_input_trace_t *t0;
1067
Klement Sekerac5fccc02017-01-18 09:56:00 +01001068 from = vlib_frame_vector_args (f); /* array of buffer indices */
1069 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001070
1071 while (n_left_from > 0)
1072 {
1073 u32 bi0;
1074 vlib_buffer_t *b0;
1075 u32 next0, error0;
1076
1077 bi0 = from[0];
1078 b0 = vlib_get_buffer (vm, bi0);
1079
1080 bfd_session_t *bs = NULL;
1081
1082 /* If this pkt is traced, snapshot the data */
1083 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001084 {
1085 int len;
1086 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1087 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1088 : sizeof (t0->data);
1089 t0->len = len;
1090 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1091 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001092
1093 /* scan this bfd pkt. error0 is the counter index to bmp */
1094 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001095 {
1096 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1097 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001098 else
Klement Sekerac5fccc02017-01-18 09:56:00 +01001099 {
1100 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1101 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001102 b0->error = rt->errors[error0];
1103
1104 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1105 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +01001106 {
Klement Sekera402ed312017-01-18 09:44:36 +01001107 /*
1108 * if everything went fine, check for poll bit, if present, re-use
1109 * the buffer and based on (now updated) session parameters, send
1110 * the final packet back
1111 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001112 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1113 if (bfd_pkt_get_poll (pkt))
1114 {
Klement Sekera239790f2017-02-16 10:53:53 +01001115 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main,
1116 bs);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001117 if (is_ipv6)
1118 {
1119 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1120 b0->error, 1);
1121 }
1122 else
1123 {
1124 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1125 b0->error, 1);
1126 }
1127 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1128 }
1129 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001130 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1131
1132 from += 1;
1133 n_left_from -= 1;
1134 }
1135
1136 return f->n_vectors;
1137}
1138
Klement Sekerac5fccc02017-01-18 09:56:00 +01001139static uword
1140bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001141{
1142 return bfd_udp_input (vm, rt, f, 0);
1143}
1144
1145/*
1146 * bfd input graph node declaration
1147 */
1148/* *INDENT-OFF* */
1149VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1150 .function = bfd_udp4_input,
1151 .name = "bfd-udp4-input",
1152 .vector_size = sizeof (u32),
1153 .type = VLIB_NODE_TYPE_INTERNAL,
1154
1155 .n_errors = BFD_UDP_N_ERROR,
1156 .error_strings = bfd_udp_error_strings,
1157
1158 .format_trace = bfd_input_format_trace,
1159
1160 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1161 .next_nodes =
1162 {
1163 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1164 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1165 },
1166};
1167/* *INDENT-ON* */
1168
Klement Sekerac5fccc02017-01-18 09:56:00 +01001169static uword
1170bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001171{
1172 return bfd_udp_input (vm, rt, f, 1);
1173}
1174
1175/* *INDENT-OFF* */
1176VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1177 .function = bfd_udp6_input,
1178 .name = "bfd-udp6-input",
1179 .vector_size = sizeof (u32),
1180 .type = VLIB_NODE_TYPE_INTERNAL,
1181
1182 .n_errors = BFD_UDP_N_ERROR,
1183 .error_strings = bfd_udp_error_strings,
1184
1185 .format_trace = bfd_input_format_trace,
1186
1187 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1188 .next_nodes =
1189 {
1190 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1191 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1192 },
1193};
1194/* *INDENT-ON* */
1195
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001196/*
1197 * Process a frame of bfd echo packets
1198 * Expect 1 packet / frame
1199 */
1200static uword
1201bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1202 vlib_frame_t * f, int is_ipv6)
1203{
1204 u32 n_left_from, *from;
1205 bfd_input_trace_t *t0;
1206
1207 from = vlib_frame_vector_args (f); /* array of buffer indices */
1208 n_left_from = f->n_vectors; /* number of buffer indices */
1209
1210 while (n_left_from > 0)
1211 {
1212 u32 bi0;
1213 vlib_buffer_t *b0;
1214 u32 next0;
1215
1216 bi0 = from[0];
1217 b0 = vlib_get_buffer (vm, bi0);
1218
1219 /* If this pkt is traced, snapshot the data */
1220 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1221 {
1222 int len;
1223 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1224 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1225 : sizeof (t0->data);
1226 t0->len = len;
1227 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1228 }
1229
Klement Sekera239790f2017-02-16 10:53:53 +01001230 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1231 {
1232 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1233 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1234 }
1235 else
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001236 {
1237 /* loop back the packet */
1238 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1239 if (is_ipv6)
1240 {
1241 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1242 b0->error, 1);
1243 }
1244 else
1245 {
1246 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1247 b0->error, 1);
1248 }
1249 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1250 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001251
1252 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1253
1254 from += 1;
1255 n_left_from -= 1;
1256 }
1257
1258 return f->n_vectors;
1259}
1260
1261static uword
1262bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1263 vlib_frame_t * f)
1264{
1265 return bfd_udp_echo_input (vm, rt, f, 0);
1266}
1267
1268u8 *
1269bfd_echo_input_format_trace (u8 * s, va_list * args)
1270{
1271 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1272 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1273 const bfd_udp_echo_input_trace_t *t =
1274 va_arg (*args, bfd_udp_echo_input_trace_t *);
1275 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1276 {
1277 s = format (s, "BFD ECHO:\n");
1278 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1279 }
1280
1281 return s;
1282}
1283
1284/*
1285 * bfd input graph node declaration
1286 */
1287/* *INDENT-OFF* */
1288VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1289 .function = bfd_udp_echo4_input,
1290 .name = "bfd-udp-echo4-input",
1291 .vector_size = sizeof (u32),
1292 .type = VLIB_NODE_TYPE_INTERNAL,
1293
1294 .n_errors = BFD_UDP_ECHO_N_ERROR,
1295 .error_strings = bfd_udp_error_strings,
1296
1297 .format_trace = bfd_echo_input_format_trace,
1298
1299 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1300 .next_nodes =
1301 {
1302 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1303 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1304 },
1305};
1306/* *INDENT-ON* */
1307
1308static uword
1309bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1310 vlib_frame_t * f)
1311{
1312 return bfd_udp_echo_input (vm, rt, f, 1);
1313}
1314
1315/* *INDENT-OFF* */
1316VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1317 .function = bfd_udp_echo6_input,
1318 .name = "bfd-udp-echo6-input",
1319 .vector_size = sizeof (u32),
1320 .type = VLIB_NODE_TYPE_INTERNAL,
1321
1322 .n_errors = BFD_UDP_ECHO_N_ERROR,
1323 .error_strings = bfd_udp_echo_error_strings,
1324
1325 .format_trace = bfd_echo_input_format_trace,
1326
1327 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1328 .next_nodes =
1329 {
1330 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1331 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1332 },
1333};
1334
1335/* *INDENT-ON* */
1336
Klement Sekerac5fccc02017-01-18 09:56:00 +01001337static clib_error_t *
1338bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001339{
1340 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1341 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1342 {
1343 /* TODO */
1344 }
1345 return 0;
1346}
1347
1348VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1349
Klement Sekerac5fccc02017-01-18 09:56:00 +01001350static clib_error_t *
1351bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001352{
1353 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1354 {
1355 /* TODO */
1356 }
1357 return 0;
1358}
1359
1360VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1361
1362/*
1363 * setup function
1364 */
Klement Sekerac5fccc02017-01-18 09:56:00 +01001365static clib_error_t *
1366bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001367{
1368 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +01001369 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001370 bfd_udp_main.bfd_main = &bfd_main;
Klement Sekera239790f2017-02-16 10:53:53 +01001371 bfd_udp_main.vnet_main = vnet_get_main ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001372 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1373 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01001374 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1375 bfd_udp_echo4_input_node.index, 1);
1376 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1377 bfd_udp_echo6_input_node.index, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001378 return 0;
1379}
1380
1381VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +01001382
1383/*
1384 * fd.io coding-style-patch-verification: ON
1385 *
1386 * Local Variables:
1387 * eval: (c-set-style "gnu")
1388 * End:
1389 */