blob: 1b3c20b852db01c60a2346877df1d3767cf004ed [file] [log] [blame]
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001#include <vppinfra/types.h>
2#include <vlibmemory/api.h>
3#include <vlib/vlib.h>
4#include <vlib/buffer.h>
5#include <vnet/ip/format.h>
6#include <vnet/ethernet/packet.h>
7#include <vnet/ip/udp_packet.h>
8#include <vnet/ip/lookup.h>
9#include <vnet/ip/icmp46_packet.h>
10#include <vnet/ip/ip4.h>
11#include <vnet/ip/ip6.h>
12#include <vnet/ip/udp.h>
13#include <vnet/ip/ip6_packet.h>
14#include <vnet/adj/adj.h>
15#include <vnet/adj/adj_nbr.h>
16#include <vnet/bfd/bfd_debug.h>
17#include <vnet/bfd/bfd_udp.h>
18#include <vnet/bfd/bfd_main.h>
19#include <vnet/bfd/bfd_api.h>
20
21typedef struct
22{
23 bfd_main_t *bfd_main;
24 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
25 * discriminator is unknown */
26 mhash_t bfd_session_idx_by_bfd_key;
27} bfd_udp_main_t;
28
Klement Sekerae4504c62016-12-08 10:16:41 +010029static vlib_node_registration_t bfd_udp4_input_node;
30static vlib_node_registration_t bfd_udp6_input_node;
31
Klement Sekera0e3c0de2016-09-29 14:43:44 +020032bfd_udp_main_t bfd_udp_main;
33
Klement Sekerac5fccc02017-01-18 09:56:00 +010034void
35bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b,
36 bfd_udp_session_t * bus)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020037{
38 udp_header_t *udp;
Klement Sekera46a87ad2017-01-02 08:22:23 +010039 const bfd_udp_key_t *key = &bus->key;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020040
41 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020042 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
43 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
Klement Sekera46a87ad2017-01-02 08:22:23 +010044 ip4_header_t *ip4;
45 const size_t headers_size = sizeof (*ip4) + sizeof (*udp);
46 vlib_buffer_advance (b, -headers_size);
47 ip4 = vlib_buffer_get_current (b);
Klement Sekerac5fccc02017-01-18 09:56:00 +010048 udp = (udp_header_t *) (ip4 + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +010049 memset (ip4, 0, headers_size);
50 ip4->ip_version_and_header_length = 0x45;
51 ip4->ttl = 255;
52 ip4->protocol = IP_PROTOCOL_UDP;
53 ip4->src_address.as_u32 = key->local_addr.ip4.as_u32;
54 ip4->dst_address.as_u32 = key->peer_addr.ip4.as_u32;
55
Klement Sekerac5fccc02017-01-18 09:56:00 +010056 udp->src_port = clib_host_to_net_u16 (50000); /* FIXME */
Klement Sekera46a87ad2017-01-02 08:22:23 +010057 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
58
59 /* fix ip length, checksum and udp length */
60 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
61
62 ip4->length = clib_host_to_net_u16 (ip_length);
63 ip4->checksum = ip4_header_checksum (ip4);
64
65 const u16 udp_length = ip_length - (sizeof (*ip4));
66 udp->length = clib_host_to_net_u16 (udp_length);
67}
68
Klement Sekerac5fccc02017-01-18 09:56:00 +010069void
70bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b,
71 bfd_udp_session_t * bus)
Klement Sekera46a87ad2017-01-02 08:22:23 +010072{
73 udp_header_t *udp;
74 const bfd_udp_key_t *key = &bus->key;
75
76 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
77 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
78 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
79 ip6_header_t *ip6;
80 const size_t headers_size = sizeof (*ip6) + sizeof (*udp);
81 vlib_buffer_advance (b, -headers_size);
82 ip6 = vlib_buffer_get_current (b);
Klement Sekerac5fccc02017-01-18 09:56:00 +010083 udp = (udp_header_t *) (ip6 + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +010084 memset (ip6, 0, headers_size);
85 ip6->ip_version_traffic_class_and_flow_label =
Klement Sekerac5fccc02017-01-18 09:56:00 +010086 clib_host_to_net_u32 (0x6 << 28);
Klement Sekera46a87ad2017-01-02 08:22:23 +010087 ip6->hop_limit = 255;
88 ip6->protocol = IP_PROTOCOL_UDP;
89 clib_memcpy (&ip6->src_address, &key->local_addr.ip6,
Klement Sekerac5fccc02017-01-18 09:56:00 +010090 sizeof (ip6->src_address));
Klement Sekera46a87ad2017-01-02 08:22:23 +010091 clib_memcpy (&ip6->dst_address, &key->peer_addr.ip6,
Klement Sekerac5fccc02017-01-18 09:56:00 +010092 sizeof (ip6->dst_address));
Klement Sekera46a87ad2017-01-02 08:22:23 +010093
Klement Sekerac5fccc02017-01-18 09:56:00 +010094 udp->src_port = clib_host_to_net_u16 (50000); /* FIXME */
Klement Sekera46a87ad2017-01-02 08:22:23 +010095 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
96
97 /* fix ip payload length and udp length */
Klement Sekerac5fccc02017-01-18 09:56:00 +010098 const u16 udp_length =
99 vlib_buffer_length_in_chain (vm, b) - (sizeof (*ip6));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100100 udp->length = clib_host_to_net_u16 (udp_length);
101 ip6->payload_length = udp->length;
102
103 /* IPv6 UDP checksum is mandatory */
104 int bogus = 0;
105 udp->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b, ip6, &bogus);
106 ASSERT (bogus == 0);
107 if (udp->checksum == 0)
108 {
109 udp->checksum = 0xffff;
110 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200111}
112
Klement Sekerac5fccc02017-01-18 09:56:00 +0100113static bfd_session_t *
114bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200115{
116 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
117 if (p)
118 {
119 return bfd_find_session_by_idx (bum->bfd_main, *p);
120 }
121 return 0;
122}
123
124static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100125bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
126 u32 desired_min_tx_us, u32 required_min_rx_us,
127 u8 detect_mult,
128 const ip46_address_t * local_addr,
129 const ip46_address_t * peer_addr,
130 u32 * bs_index)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200131{
132 vnet_sw_interface_t *sw_if =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100133 vnet_get_sw_interface (vnet_get_main (), sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200134 /* get a pool entry and if we end up not needing it, give it back */
135 bfd_transport_t t = BFD_TRANSPORT_UDP4;
136 if (!ip46_address_is_ip4 (local_addr))
137 {
138 t = BFD_TRANSPORT_UDP6;
139 }
140 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
141 bfd_udp_session_t *bus = &bs->udp;
142 memset (bus, 0, sizeof (*bus));
143 bfd_udp_key_t *key = &bus->key;
144 key->sw_if_index = sw_if->sw_if_index;
145 key->local_addr.as_u64[0] = local_addr->as_u64[0];
146 key->local_addr.as_u64[1] = local_addr->as_u64[1];
147 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
148 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
149 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
150 if (tmp)
151 {
152 BFD_ERR ("duplicate bfd-udp session, existing bs_idx=%d", tmp->bs_idx);
153 bfd_put_session (bum->bfd_main, bs);
154 return VNET_API_ERROR_BFD_EEXIST;
155 }
156 key->sw_if_index = sw_if->sw_if_index;
157 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
158 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100159 bs->bs_idx, key->sw_if_index, format_ip46_address,
160 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
161 &key->peer_addr, IP46_TYPE_ANY);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200162 if (BFD_TRANSPORT_UDP4 == t)
163 {
164 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100165 &key->peer_addr,
166 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200167 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100168 "returns %d", format_ip46_address, &key->peer_addr,
169 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200170 }
171 else
172 {
173 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100174 &key->peer_addr,
175 key->sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200176 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100177 "returns %d", format_ip46_address, &key->peer_addr,
178 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200179 }
180 bs->config_desired_min_tx_us = desired_min_tx_us;
181 bs->required_min_rx_us = required_min_rx_us;
Klement Sekerac5fccc02017-01-18 09:56:00 +0100182 bs->required_min_echo_rx_us = required_min_rx_us; /* FIXME */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200183 bs->local_detect_mult = detect_mult;
184 bfd_session_start (bum->bfd_main, bs);
Klement Sekera10db26f2017-01-11 08:16:53 +0100185 *bs_index = bs->bs_idx;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200186 return 0;
187}
188
189static vnet_api_error_t
Klement Sekerac5fccc02017-01-18 09:56:00 +0100190bfd_udp_validate_api_input (u32 sw_if_index,
191 const ip46_address_t * local_addr,
192 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200193{
194 vnet_sw_interface_t *sw_if =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100195 vnet_get_sw_interface (vnet_get_main (), sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200196 u8 local_ip_valid = 0;
197 ip_interface_address_t *ia = NULL;
198 if (!sw_if)
199 {
200 BFD_ERR ("got NULL sw_if");
201 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
202 }
203 if (ip46_address_is_ip4 (local_addr))
204 {
205 if (!ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100206 {
207 BFD_ERR ("IP family mismatch");
208 return VNET_API_ERROR_INVALID_ARGUMENT;
209 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200210 ip4_main_t *im = &ip4_main;
211
212 /* *INDENT-OFF* */
213 foreach_ip_interface_address (
214 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
215 ip4_address_t *x =
216 ip_interface_address_get_address (&im->lookup_main, ia);
217 if (x->as_u32 == local_addr->ip4.as_u32)
218 {
219 /* valid address for this interface */
220 local_ip_valid = 1;
221 break;
222 }
223 }));
224 /* *INDENT-ON* */
225 }
226 else
227 {
228 if (ip46_address_is_ip4 (peer_addr))
Klement Sekerac5fccc02017-01-18 09:56:00 +0100229 {
230 BFD_ERR ("IP family mismatch");
231 return VNET_API_ERROR_INVALID_ARGUMENT;
232 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200233 ip6_main_t *im = &ip6_main;
234 /* *INDENT-OFF* */
235 foreach_ip_interface_address (
236 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
237 ip6_address_t *x =
238 ip_interface_address_get_address (&im->lookup_main, ia);
239 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
240 local_addr->ip6.as_u64[1] == x->as_u64[1])
241 {
242 /* valid address for this interface */
243 local_ip_valid = 1;
244 break;
245 }
246 }));
247 /* *INDENT-ON* */
248 }
249
250 if (!local_ip_valid)
251 {
252 BFD_ERR ("address not found on interface");
253 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
254 }
255
256 return 0;
257}
258
Klement Sekerac5fccc02017-01-18 09:56:00 +0100259vnet_api_error_t
260bfd_udp_add_session (u32 sw_if_index, u32 desired_min_tx_us,
261 u32 required_min_rx_us, u8 detect_mult,
262 const ip46_address_t * local_addr,
263 const ip46_address_t * peer_addr, u32 * bs_index)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200264{
265 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100266 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200267 if (rv)
268 {
269 return rv;
270 }
271 if (detect_mult < 1)
272 {
273 BFD_ERR ("detect_mult < 1");
274 return VNET_API_ERROR_INVALID_ARGUMENT;
275 }
276 if (desired_min_tx_us < 1)
277 {
278 BFD_ERR ("desired_min_tx_us < 1");
279 return VNET_API_ERROR_INVALID_ARGUMENT;
280 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100281 return bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
282 desired_min_tx_us, required_min_rx_us,
283 detect_mult, local_addr, peer_addr,
284 bs_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200285}
286
Klement Sekerac5fccc02017-01-18 09:56:00 +0100287vnet_api_error_t
288bfd_udp_del_session (u32 sw_if_index,
289 const ip46_address_t * local_addr,
290 const ip46_address_t * peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200291{
292 vnet_api_error_t rv =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100293 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200294 if (rv)
295 {
296 return rv;
297 }
298 bfd_udp_main_t *bum = &bfd_udp_main;
299 vnet_sw_interface_t *sw_if =
Klement Sekerac5fccc02017-01-18 09:56:00 +0100300 vnet_get_sw_interface (vnet_get_main (), sw_if_index);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200301 bfd_udp_key_t key;
302 memset (&key, 0, sizeof (key));
303 key.sw_if_index = sw_if->sw_if_index;
304 key.local_addr.as_u64[0] = local_addr->as_u64[0];
305 key.local_addr.as_u64[1] = local_addr->as_u64[1];
306 key.peer_addr.as_u64[0] = peer_addr->as_u64[0];
307 key.peer_addr.as_u64[1] = peer_addr->as_u64[1];
308 bfd_session_t *tmp = bfd_lookup_session (bum, &key);
309 if (tmp)
310 {
311 BFD_DBG ("free bfd-udp session, bs_idx=%d", tmp->bs_idx);
312 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &key, NULL);
313 adj_unlock (tmp->udp.adj_index);
314 bfd_put_session (bum->bfd_main, tmp);
315 }
316 else
317 {
318 BFD_ERR ("no such session");
319 return VNET_API_ERROR_BFD_NOENT;
320 }
321 return 0;
322}
323
Klement Sekerac5fccc02017-01-18 09:56:00 +0100324typedef enum
325{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200326 BFD_UDP_INPUT_NEXT_NORMAL,
327 BFD_UDP_INPUT_NEXT_REPLY,
328 BFD_UDP_INPUT_N_NEXT,
329} bfd_udp_input_next_t;
330
331/* Packet counters */
332#define foreach_bfd_udp_error(F) \
333 F (NONE, "good bfd packets (processed)") \
334 F (BAD, "invalid bfd packets") \
335 F (DISABLED, "bfd packets received on disabled interfaces")
336
337#define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
338foreach_bfd_udp_error (F);
339#undef F
340
341static char *bfd_udp_error_strings[] = {
342#define F(sym, string) BFD_UDP_ERR_##sym##_STR,
343 foreach_bfd_udp_error (F)
344#undef F
345};
346
Klement Sekerac5fccc02017-01-18 09:56:00 +0100347typedef enum
348{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200349#define F(sym, str) BFD_UDP_ERROR_##sym,
350 foreach_bfd_udp_error (F)
351#undef F
Klement Sekerac5fccc02017-01-18 09:56:00 +0100352 BFD_UDP_N_ERROR,
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200353} bfd_udp_error_t;
354
Klement Sekerac5fccc02017-01-18 09:56:00 +0100355static void
356bfd_udp4_find_headers (vlib_buffer_t * b, const ip4_header_t ** ip4,
357 const udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200358{
Klement Sekera0c1519b2016-12-08 05:03:32 +0100359 /* sanity check first */
360 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
361 if (start < 0 && start < sizeof (b->pre_data))
362 {
363 BFD_ERR ("Start of ip header is before pre_data, ignoring");
364 *ip4 = NULL;
365 *udp = NULL;
366 return;
367 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100368 *ip4 = (ip4_header_t *) (b->data + start);
369 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera0c1519b2016-12-08 05:03:32 +0100370 {
371 BFD_ERR ("Start of ip header is beyond current data, ignoring");
372 *ip4 = NULL;
373 *udp = NULL;
374 return;
375 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100376 *udp = (udp_header_t *) ((*ip4) + 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200377}
378
Klement Sekerac5fccc02017-01-18 09:56:00 +0100379static bfd_udp_error_t
380bfd_udp4_verify_transport (const ip4_header_t * ip4,
381 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200382{
383 const bfd_udp_session_t *bus = &bs->udp;
384 const bfd_udp_key_t *key = &bus->key;
385 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
386 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100387 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100388 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
389 key->peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200390 return BFD_UDP_ERROR_BAD;
391 }
392 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
393 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100394 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100395 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
396 key->local_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200397 return BFD_UDP_ERROR_BAD;
398 }
399 const u8 expected_ttl = 255;
400 if (ip4->ttl != expected_ttl)
401 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100402 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
Klement Sekerac5fccc02017-01-18 09:56:00 +0100403 expected_ttl);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200404 return BFD_UDP_ERROR_BAD;
405 }
406 if (clib_net_to_host_u16 (udp->src_port) < 49152 ||
407 clib_net_to_host_u16 (udp->src_port) > 65535)
408 {
Klement Sekera46a87ad2017-01-02 08:22:23 +0100409 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100410 udp->src_port);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200411 }
412 return BFD_UDP_ERROR_NONE;
413}
414
415typedef struct
416{
417 u32 bs_idx;
418 bfd_pkt_t pkt;
419} bfd_rpc_update_t;
420
Klement Sekerac5fccc02017-01-18 09:56:00 +0100421static void
422bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200423{
424 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
425}
426
Klement Sekerac5fccc02017-01-18 09:56:00 +0100427static void
428bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200429{
430 /* packet length was already verified to be correct by the caller */
431 const u32 data_size = sizeof (bfd_rpc_update_t) -
Klement Sekerac5fccc02017-01-18 09:56:00 +0100432 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200433 u8 data[data_size];
Klement Sekerac5fccc02017-01-18 09:56:00 +0100434 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200435 update->bs_idx = bs_idx;
436 clib_memcpy (&update->pkt, pkt, pkt->head.length);
437 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
438}
439
Klement Sekerac5fccc02017-01-18 09:56:00 +0100440static bfd_udp_error_t
441bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
442 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200443{
444 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
445 if (sizeof (*pkt) > b->current_length)
446 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100447 BFD_ERR
448 ("Payload size %d too small to hold bfd packet of minimum size %d",
449 b->current_length, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200450 return BFD_UDP_ERROR_BAD;
451 }
452 const ip4_header_t *ip4;
453 const udp_header_t *udp;
454 bfd_udp4_find_headers (b, &ip4, &udp);
455 if (!ip4 || !udp)
456 {
457 BFD_ERR ("Couldn't find ip4 or udp header");
458 return BFD_UDP_ERROR_BAD;
459 }
460 if (!bfd_verify_pkt_common (pkt))
461 {
462 return BFD_UDP_ERROR_BAD;
463 }
464 bfd_session_t *bs = NULL;
465 if (pkt->your_disc)
466 {
467 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100468 pkt->your_disc);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200469 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
470 }
471 else
472 {
473 bfd_udp_key_t key;
474 memset (&key, 0, sizeof (key));
475 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
476 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
477 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
478 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100479 "peer=%U)",
480 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
481 format_ip4_address, key.peer_addr.ip4.as_u8);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200482 bs = bfd_lookup_session (&bfd_udp_main, &key);
483 }
484 if (!bs)
485 {
486 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
487 return BFD_UDP_ERROR_BAD;
488 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100489 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200490 if (!bfd_verify_pkt_session (pkt, b->current_length, bs))
491 {
492 return BFD_UDP_ERROR_BAD;
493 }
494 bfd_udp_error_t err;
495 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
496 {
497 return err;
498 }
499 bfd_rpc_update_session (bs->bs_idx, pkt);
500 *bs_out = bs;
501 return BFD_UDP_ERROR_NONE;
502}
503
Klement Sekerac5fccc02017-01-18 09:56:00 +0100504static void
505bfd_udp6_find_headers (vlib_buffer_t * b, const ip6_header_t ** ip6,
506 const udp_header_t ** udp)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200507{
Klement Sekera46a87ad2017-01-02 08:22:23 +0100508 /* sanity check first */
509 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
510 if (start < 0 && start < sizeof (b->pre_data))
511 {
512 BFD_ERR ("Start of ip header is before pre_data, ignoring");
513 *ip6 = NULL;
514 *udp = NULL;
515 return;
516 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100517 *ip6 = (ip6_header_t *) (b->data + start);
518 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
Klement Sekera46a87ad2017-01-02 08:22:23 +0100519 {
520 BFD_ERR ("Start of ip header is beyond current data, ignoring");
521 *ip6 = NULL;
522 *udp = NULL;
523 return;
524 }
Klement Sekerac5fccc02017-01-18 09:56:00 +0100525 *udp = (udp_header_t *) ((*ip6) + 1);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100526}
527
Klement Sekerac5fccc02017-01-18 09:56:00 +0100528static bfd_udp_error_t
529bfd_udp6_verify_transport (const ip6_header_t * ip6,
530 const udp_header_t * udp, const bfd_session_t * bs)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100531{
532 const bfd_udp_session_t *bus = &bs->udp;
533 const bfd_udp_key_t *key = &bus->key;
534 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
535 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
536 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100537 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
538 format_ip6_address, ip6, format_ip6_address,
539 &key->peer_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100540 return BFD_UDP_ERROR_BAD;
541 }
542 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
543 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
544 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100545 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
546 format_ip6_address, ip6, format_ip6_address,
547 &key->local_addr.ip6);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100548 return BFD_UDP_ERROR_BAD;
549 }
550 const u8 expected_hop_limit = 255;
551 if (ip6->hop_limit != expected_hop_limit)
552 {
553 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100554 ip6->hop_limit, expected_hop_limit);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100555 return BFD_UDP_ERROR_BAD;
556 }
557 if (clib_net_to_host_u16 (udp->src_port) < 49152 ||
558 clib_net_to_host_u16 (udp->src_port) > 65535)
559 {
560 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100561 udp->src_port);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100562 }
563 return BFD_UDP_ERROR_NONE;
564}
565
Klement Sekerac5fccc02017-01-18 09:56:00 +0100566static bfd_udp_error_t
567bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
568 vlib_buffer_t * b, bfd_session_t ** bs_out)
Klement Sekera46a87ad2017-01-02 08:22:23 +0100569{
570 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
571 if (sizeof (*pkt) > b->current_length)
572 {
Klement Sekerac5fccc02017-01-18 09:56:00 +0100573 BFD_ERR
574 ("Payload size %d too small to hold bfd packet of minimum size %d",
575 b->current_length, sizeof (*pkt));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100576 return BFD_UDP_ERROR_BAD;
577 }
578 const ip6_header_t *ip6;
579 const udp_header_t *udp;
580 bfd_udp6_find_headers (b, &ip6, &udp);
581 if (!ip6 || !udp)
582 {
583 BFD_ERR ("Couldn't find ip6 or udp header");
584 return BFD_UDP_ERROR_BAD;
585 }
586 if (!bfd_verify_pkt_common (pkt))
587 {
588 return BFD_UDP_ERROR_BAD;
589 }
590 bfd_session_t *bs = NULL;
591 if (pkt->your_disc)
592 {
593 BFD_DBG ("Looking up BFD session using discriminator %u",
Klement Sekerac5fccc02017-01-18 09:56:00 +0100594 pkt->your_disc);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100595 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
596 }
597 else
598 {
599 bfd_udp_key_t key;
600 memset (&key, 0, sizeof (key));
601 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
602 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
603 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
604 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
605 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
606 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
Klement Sekerac5fccc02017-01-18 09:56:00 +0100607 "peer=%U)",
608 key.sw_if_index, format_ip6_address, &key.local_addr,
609 format_ip6_address, &key.peer_addr);
Klement Sekera46a87ad2017-01-02 08:22:23 +0100610 bs = bfd_lookup_session (&bfd_udp_main, &key);
611 }
612 if (!bs)
613 {
614 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
615 return BFD_UDP_ERROR_BAD;
616 }
617 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
618 if (!bfd_verify_pkt_session (pkt, b->current_length, bs))
619 {
620 return BFD_UDP_ERROR_BAD;
621 }
622 bfd_udp_error_t err;
623 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
624 {
625 return err;
626 }
627 bfd_rpc_update_session (bs->bs_idx, pkt);
628 *bs_out = bs;
629 return BFD_UDP_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200630}
631
632/*
633 * Process a frame of bfd packets
634 * Expect 1 packet / frame
635 */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100636static uword
637bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
638 vlib_frame_t * f, int is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200639{
640 u32 n_left_from, *from;
641 bfd_input_trace_t *t0;
642
Klement Sekerac5fccc02017-01-18 09:56:00 +0100643 from = vlib_frame_vector_args (f); /* array of buffer indices */
644 n_left_from = f->n_vectors; /* number of buffer indices */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200645
646 while (n_left_from > 0)
647 {
648 u32 bi0;
649 vlib_buffer_t *b0;
650 u32 next0, error0;
651
652 bi0 = from[0];
653 b0 = vlib_get_buffer (vm, bi0);
654
655 bfd_session_t *bs = NULL;
656
657 /* If this pkt is traced, snapshot the data */
658 if (b0->flags & VLIB_BUFFER_IS_TRACED)
Klement Sekerac5fccc02017-01-18 09:56:00 +0100659 {
660 int len;
661 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
662 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
663 : sizeof (t0->data);
664 t0->len = len;
665 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
666 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200667
668 /* scan this bfd pkt. error0 is the counter index to bmp */
669 if (is_ipv6)
Klement Sekerac5fccc02017-01-18 09:56:00 +0100670 {
671 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
672 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200673 else
Klement Sekerac5fccc02017-01-18 09:56:00 +0100674 {
675 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
676 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200677 b0->error = rt->errors[error0];
678
679 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
680 if (BFD_UDP_ERROR_NONE == error0)
Klement Sekerac5fccc02017-01-18 09:56:00 +0100681 {
682 /* if everything went fine, check for poll bit, if present, re-use
683 the buffer and based on (now updated) session parameters, send the
684 final packet back */
685 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
686 if (bfd_pkt_get_poll (pkt))
687 {
688 bfd_send_final (vm, b0, bs);
689 if (is_ipv6)
690 {
691 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
692 b0->error, 1);
693 }
694 else
695 {
696 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
697 b0->error, 1);
698 }
699 next0 = BFD_UDP_INPUT_NEXT_REPLY;
700 }
701 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200702 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
703
704 from += 1;
705 n_left_from -= 1;
706 }
707
708 return f->n_vectors;
709}
710
Klement Sekerac5fccc02017-01-18 09:56:00 +0100711static uword
712bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200713{
714 return bfd_udp_input (vm, rt, f, 0);
715}
716
717/*
718 * bfd input graph node declaration
719 */
720/* *INDENT-OFF* */
721VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
722 .function = bfd_udp4_input,
723 .name = "bfd-udp4-input",
724 .vector_size = sizeof (u32),
725 .type = VLIB_NODE_TYPE_INTERNAL,
726
727 .n_errors = BFD_UDP_N_ERROR,
728 .error_strings = bfd_udp_error_strings,
729
730 .format_trace = bfd_input_format_trace,
731
732 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
733 .next_nodes =
734 {
735 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
736 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
737 },
738};
739/* *INDENT-ON* */
740
Klement Sekerac5fccc02017-01-18 09:56:00 +0100741static uword
742bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200743{
744 return bfd_udp_input (vm, rt, f, 1);
745}
746
747/* *INDENT-OFF* */
748VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
749 .function = bfd_udp6_input,
750 .name = "bfd-udp6-input",
751 .vector_size = sizeof (u32),
752 .type = VLIB_NODE_TYPE_INTERNAL,
753
754 .n_errors = BFD_UDP_N_ERROR,
755 .error_strings = bfd_udp_error_strings,
756
757 .format_trace = bfd_input_format_trace,
758
759 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
760 .next_nodes =
761 {
762 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
763 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
764 },
765};
766/* *INDENT-ON* */
767
Klement Sekerac5fccc02017-01-18 09:56:00 +0100768static clib_error_t *
769bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200770{
771 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
772 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
773 {
774 /* TODO */
775 }
776 return 0;
777}
778
779VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
780
Klement Sekerac5fccc02017-01-18 09:56:00 +0100781static clib_error_t *
782bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200783{
784 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
785 {
786 /* TODO */
787 }
788 return 0;
789}
790
791VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
792
793/*
794 * setup function
795 */
Klement Sekerac5fccc02017-01-18 09:56:00 +0100796static clib_error_t *
797bfd_udp_init (vlib_main_t * vm)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200798{
799 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
Klement Sekerac5fccc02017-01-18 09:56:00 +0100800 sizeof (bfd_udp_key_t));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200801 bfd_udp_main.bfd_main = &bfd_main;
802 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
803 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
804 return 0;
805}
806
807VLIB_INIT_FUNCTION (bfd_udp_init);
Klement Sekerac5fccc02017-01-18 09:56:00 +0100808
809/*
810 * fd.io coding-style-patch-verification: ON
811 *
812 * Local Variables:
813 * eval: (c-set-style "gnu")
814 * End:
815 */