b3eabc9cda9c932867bbbe7cf7cf17534bd77fca
[vpp.git] / src / vnet / bfd / bfd_udp.c
1 /*
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  */
15 /**
16  * @file
17  * @brief BFD UDP transport layer implementation
18  */
19 #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>
25 #include <vnet/udp/udp_packet.h>
26 #include <vnet/udp/udp.h>
27 #include <vnet/ip/lookup.h>
28 #include <vnet/ip/icmp46_packet.h>
29 #include <vnet/ip/ip4.h>
30 #include <vnet/ip/ip6.h>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/adj/adj.h>
33 #include <vnet/adj/adj_nbr.h>
34 #include <vnet/dpo/receive_dpo.h>
35 #include <vnet/fib/fib_entry.h>
36 #include <vnet/fib/fib_table.h>
37 #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
42 typedef 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;
48   /* 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;
54 } bfd_udp_main_t;
55
56 static vlib_node_registration_t bfd_udp4_input_node;
57 static vlib_node_registration_t bfd_udp6_input_node;
58 static vlib_node_registration_t bfd_udp_echo4_input_node;
59 static vlib_node_registration_t bfd_udp_echo6_input_node;
60
61 bfd_udp_main_t bfd_udp_main;
62
63 vnet_api_error_t
64 bfd_udp_set_echo_source (u32 sw_if_index)
65 {
66   vnet_sw_interface_t *sw_if =
67     vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
68   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
77 vnet_api_error_t
78 bfd_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
85 int
86 bfd_udp_is_echo_available (bfd_transport_e transport)
87 {
88   if (!bfd_udp_main.echo_source_is_set)
89     {
90       BFD_DBG ("UDP echo source not set - echo not available");
91       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     }
134   BFD_DBG ("No usable IP address for UDP echo - echo not available");
135   return 0;
136 }
137
138 static u16
139 bfd_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
152 int
153 bfd_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
186 int
187 bfd_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 */
207             return 1;
208           }
209       }));
210   /* *INDENT-ON* */
211   BFD_ERR ("cannot find ip6 address, no usable address found");
212   return 0;
213 }
214
215 void
216 bfd_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
233 int
234 bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b,
235                         const bfd_session_t * bs, int is_echo)
236 {
237   const bfd_udp_session_t *bus = &bs->udp;
238   const bfd_udp_key_t *key = &bus->key;
239
240   b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
241   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
242   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
243   typedef struct
244   {
245     ip4_header_t ip4;
246     udp_header_t udp;
247   } ip4_udp_headers;
248   ip4_udp_headers *headers = NULL;
249   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;
255   headers->udp.src_port =
256     clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
257   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     }
273
274   /* fix ip length, checksum and udp length */
275   const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
276
277   headers->ip4.length = clib_host_to_net_u16 (ip_length);
278   headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
279
280   const u16 udp_length = ip_length - (sizeof (headers->ip4));
281   headers->udp.length = clib_host_to_net_u16 (udp_length);
282   return 1;
283 }
284
285 int
286 bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b,
287                         const bfd_session_t * bs, int is_echo)
288 {
289   const bfd_udp_session_t *bus = &bs->udp;
290   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;
295   typedef struct
296   {
297     ip6_header_t ip6;
298     udp_header_t udp;
299   } ip6_udp_headers;
300   ip6_udp_headers *headers = NULL;
301   vlib_buffer_advance (b, -sizeof (*headers));
302   headers = vlib_buffer_get_current (b);
303   memset (headers, 0, sizeof (*headers));
304   headers->ip6.ip_version_traffic_class_and_flow_label =
305     clib_host_to_net_u32 (0x6 << 28);
306   headers->ip6.hop_limit = 255;
307   headers->ip6.protocol = IP_PROTOCOL_UDP;
308   headers->udp.src_port =
309     clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
310   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     }
330
331   /* fix ip payload length and udp length */
332   const u16 udp_length =
333     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;
336
337   /* IPv6 UDP checksum is mandatory */
338   int bogus = 0;
339   headers->udp.checksum =
340     ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
341   ASSERT (bogus == 0);
342   if (headers->udp.checksum == 0)
343     {
344       headers->udp.checksum = 0xffff;
345     }
346   return 1;
347 }
348
349 static bfd_session_t *
350 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
351 {
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
360 static void
361 bfd_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
373 static vnet_api_error_t
374 bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
375                               u32 desired_min_tx_usec,
376                               u32 required_min_rx_usec, u8 detect_mult,
377                               const ip46_address_t * local_addr,
378                               const ip46_address_t * peer_addr,
379                               bfd_session_t ** bs_out)
380 {
381   /* get a pool entry and if we end up not needing it, give it back */
382   bfd_transport_e t = BFD_TRANSPORT_UDP4;
383   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);
388   if (!bs)
389     {
390       bfd_put_session (bum->bfd_main, bs);
391       return VNET_API_ERROR_BFD_EAGAIN;
392     }
393   bfd_udp_session_t *bus = &bs->udp;
394   memset (bus, 0, sizeof (*bus));
395   bfd_udp_key_t *key = &bus->key;
396   bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
397   const bfd_session_t *tmp = bfd_lookup_session (bum, key);
398   if (tmp)
399     {
400       clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
401                     tmp->bs_idx);
402       bfd_put_session (bum->bfd_main, bs);
403       return VNET_API_ERROR_BFD_EEXIST;
404     }
405   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",
407            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);
410   if (BFD_TRANSPORT_UDP4 == t)
411     {
412       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
413                                             &key->peer_addr,
414                                             key->sw_if_index);
415       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
416                "returns %d", format_ip46_address, &key->peer_addr,
417                IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
418     }
419   else
420     {
421       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
422                                             &key->peer_addr,
423                                             key->sw_if_index);
424       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
425                "returns %d", format_ip46_address, &key->peer_addr,
426                IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
427     }
428   *bs_out = bs;
429   return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
430                                  required_min_rx_usec, detect_mult);
431 }
432
433 static vnet_api_error_t
434 bfd_udp_validate_api_input (u32 sw_if_index,
435                             const ip46_address_t * local_addr,
436                             const ip46_address_t * peer_addr)
437 {
438   vnet_sw_interface_t *sw_if =
439     vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
440   u8 local_ip_valid = 0;
441   ip_interface_address_t *ia = NULL;
442   if (!sw_if)
443     {
444       clib_warning ("got NULL sw_if");
445       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))
450         {
451           clib_warning ("IP family mismatch");
452           return VNET_API_ERROR_INVALID_ARGUMENT;
453         }
454       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))
473         {
474           clib_warning ("IP family mismatch");
475           return VNET_API_ERROR_INVALID_ARGUMENT;
476         }
477       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] &&
484                 local_addr->ip6.as_u64[1] == x->as_u64[1])
485               {
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     {
496       clib_warning ("address not found on interface");
497       return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
498     }
499
500   return 0;
501 }
502
503 static vnet_api_error_t
504 bfd_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
533 static vnet_api_error_t
534 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
535                        u32 required_min_rx_usec, u8 detect_mult,
536                        const ip46_address_t * local_addr,
537                        const ip46_address_t * peer_addr)
538 {
539   vnet_api_error_t rv =
540     bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
541   if (rv)
542     {
543       return rv;
544     }
545   if (detect_mult < 1)
546     {
547       clib_warning ("detect_mult < 1");
548       return VNET_API_ERROR_INVALID_ARGUMENT;
549     }
550   if (desired_min_tx_usec < 1)
551     {
552       clib_warning ("desired_min_tx_usec < 1");
553       return VNET_API_ERROR_INVALID_ARGUMENT;
554     }
555   return 0;
556 }
557
558 static void
559 bfd_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
568 vnet_api_error_t
569 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
570                      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)
574 {
575   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);
579   bfd_session_t *bs = NULL;
580   if (!rv)
581     {
582       rv =
583         bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
584                                       desired_min_tx_usec,
585                                       required_min_rx_usec, detect_mult,
586                                       local_addr, peer_addr, &bs);
587     }
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;
608 }
609
610 vnet_api_error_t
611 bfd_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
631 vnet_api_error_t
632 bfd_udp_del_session (u32 sw_if_index,
633                      const ip46_address_t * local_addr,
634                      const ip46_address_t * peer_addr)
635 {
636   bfd_session_t *bs = NULL;
637   vnet_api_error_t rv =
638     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
639                                        &bs);
640   if (rv)
641     {
642       return rv;
643     }
644   bfd_udp_del_session_internal (bs);
645   return 0;
646 }
647
648 vnet_api_error_t
649 bfd_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)
658     {
659       return rv;
660     }
661   bfd_session_set_flags (bs, admin_up_down);
662   return 0;
663 }
664
665 vnet_api_error_t
666 bfd_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
687 vnet_api_error_t
688 bfd_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);
701 }
702
703 typedef enum
704 {
705   BFD_UDP_INPUT_NEXT_NORMAL,
706   BFD_UDP_INPUT_NEXT_REPLY,
707   BFD_UDP_INPUT_N_NEXT,
708 } bfd_udp_input_next_t;
709
710 /* Packet counters - BFD control frames */
711 #define foreach_bfd_udp_error(F)           \
712   F (NONE, "good bfd packets (processed)") \
713   F (BAD, "invalid bfd packets")
714
715 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
716 foreach_bfd_udp_error (F);
717 #undef F
718
719 static 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
725 typedef enum
726 {
727 #define F(sym, str) BFD_UDP_ERROR_##sym,
728   foreach_bfd_udp_error (F)
729 #undef F
730     BFD_UDP_N_ERROR,
731 } bfd_udp_error_t;
732
733 /* 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;
739 foreach_bfd_udp_echo_error (F);
740 #undef F
741
742 static 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
748 typedef 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
756 static void
757 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
758                        udp_header_t ** udp)
759 {
760   /* 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     }
769   *ip4 = (ip4_header_t *) (b->data + start);
770   if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
771     {
772       BFD_ERR ("Start of ip header is beyond current data, ignoring");
773       *ip4 = NULL;
774       *udp = NULL;
775       return;
776     }
777   *udp = (udp_header_t *) ((*ip4) + 1);
778 }
779
780 static bfd_udp_error_t
781 bfd_udp4_verify_transport (const ip4_header_t * ip4,
782                            const udp_header_t * udp, const bfd_session_t * bs)
783 {
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     {
788       BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
789                format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
790                key->peer_addr.ip4.as_u8);
791       return BFD_UDP_ERROR_BAD;
792     }
793   if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
794     {
795       BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
796                format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
797                key->local_addr.ip4.as_u8);
798       return BFD_UDP_ERROR_BAD;
799     }
800   const u8 expected_ttl = 255;
801   if (ip4->ttl != expected_ttl)
802     {
803       BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
804                expected_ttl);
805       return BFD_UDP_ERROR_BAD;
806     }
807   if (clib_net_to_host_u16 (udp->src_port) < 49152)
808     {
809       BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
810                udp->src_port);
811     }
812   return BFD_UDP_ERROR_NONE;
813 }
814
815 typedef struct
816 {
817   u32 bs_idx;
818   bfd_pkt_t pkt;
819 } bfd_rpc_update_t;
820
821 static void
822 bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
823 {
824   bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
825 }
826
827 static void
828 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
829 {
830   /* packet length was already verified to be correct by the caller */
831   const u32 data_size = sizeof (bfd_rpc_update_t) -
832     STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
833   u8 data[data_size];
834   bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
835   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
840 static bfd_udp_error_t
841 bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
842                vlib_buffer_t * b, bfd_session_t ** bs_out)
843 {
844   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
845   if (sizeof (*pkt) > b->current_length)
846     {
847       BFD_ERR
848         ("Payload size %d too small to hold bfd packet of minimum size %d",
849          b->current_length, sizeof (*pkt));
850       return BFD_UDP_ERROR_BAD;
851     }
852   ip4_header_t *ip4;
853   udp_header_t *udp;
854   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     }
860   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     }
868   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",
876                pkt->your_disc);
877       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, "
887                "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);
890       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     }
897   BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
898   if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
899     {
900       BFD_ERR ("Packet verification failed, dropping packet");
901       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
913 static void
914 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
915                        udp_header_t ** udp)
916 {
917   /* 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     }
926   *ip6 = (ip6_header_t *) (b->data + start);
927   if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
928     {
929       BFD_ERR ("Start of ip header is beyond current data, ignoring");
930       *ip6 = NULL;
931       *udp = NULL;
932       return;
933     }
934   if ((*ip6)->protocol != IP_PROTOCOL_UDP)
935     {
936       BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
937                "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
938       *ip6 = NULL;
939       *udp = NULL;
940       return;
941     }
942   *udp = (udp_header_t *) ((*ip6) + 1);
943 }
944
945 static bfd_udp_error_t
946 bfd_udp6_verify_transport (const ip6_header_t * ip6,
947                            const udp_header_t * udp, const bfd_session_t * bs)
948 {
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     {
954       BFD_ERR ("IP src addr mismatch, got %U, expected %U",
955                format_ip6_address, ip6, format_ip6_address,
956                &key->peer_addr.ip6);
957       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     {
962       BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
963                format_ip6_address, ip6, format_ip6_address,
964                &key->local_addr.ip6);
965       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",
971                ip6->hop_limit, expected_hop_limit);
972       return BFD_UDP_ERROR_BAD;
973     }
974   if (clib_net_to_host_u16 (udp->src_port) < 49152)
975     {
976       BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
977                udp->src_port);
978     }
979   return BFD_UDP_ERROR_NONE;
980 }
981
982 static bfd_udp_error_t
983 bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
984                vlib_buffer_t * b, bfd_session_t ** bs_out)
985 {
986   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
987   if (sizeof (*pkt) > b->current_length)
988     {
989       BFD_ERR
990         ("Payload size %d too small to hold bfd packet of minimum size %d",
991          b->current_length, sizeof (*pkt));
992       return BFD_UDP_ERROR_BAD;
993     }
994   ip6_header_t *ip6;
995   udp_header_t *udp;
996   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     }
1002   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     }
1010   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",
1018                pkt->your_disc);
1019       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, "
1031                "peer=%U)",
1032                key.sw_if_index, format_ip6_address, &key.local_addr,
1033                format_ip6_address, &key.peer_addr);
1034       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);
1042   if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
1043     {
1044       BFD_ERR ("Packet verification failed, dropping packet");
1045       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;
1055 }
1056
1057 /*
1058  * Process a frame of bfd packets
1059  * Expect 1 packet / frame
1060  */
1061 static uword
1062 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1063                vlib_frame_t * f, int is_ipv6)
1064 {
1065   u32 n_left_from, *from;
1066   bfd_input_trace_t *t0;
1067
1068   from = vlib_frame_vector_args (f);    /* array of buffer indices */
1069   n_left_from = f->n_vectors;   /* number of buffer indices */
1070
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)
1084         {
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         }
1092
1093       /* scan this bfd pkt. error0 is the counter index to bmp */
1094       if (is_ipv6)
1095         {
1096           error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1097         }
1098       else
1099         {
1100           error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1101         }
1102       b0->error = rt->errors[error0];
1103
1104       next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1105       if (BFD_UDP_ERROR_NONE == error0)
1106         {
1107           /*
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            */
1112           const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1113           if (bfd_pkt_get_poll (pkt))
1114             {
1115               bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main,
1116                                             bs);
1117               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         }
1130       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
1139 static uword
1140 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1141 {
1142   return bfd_udp_input (vm, rt, f, 0);
1143 }
1144
1145 /*
1146  * bfd input graph node declaration
1147  */
1148 /* *INDENT-OFF* */
1149 VLIB_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
1169 static uword
1170 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1171 {
1172   return bfd_udp_input (vm, rt, f, 1);
1173 }
1174
1175 /* *INDENT-OFF* */
1176 VLIB_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
1196 /*
1197  * Process a frame of bfd echo packets
1198  * Expect 1 packet / frame
1199  */
1200 static uword
1201 bfd_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
1230       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
1236         {
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         }
1251
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
1261 static uword
1262 bfd_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
1268 u8 *
1269 bfd_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* */
1288 VLIB_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
1308 static uword
1309 bfd_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* */
1316 VLIB_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
1337 static clib_error_t *
1338 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1339 {
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
1348 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1349
1350 static clib_error_t *
1351 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1352 {
1353   if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1354     {
1355       /* TODO */
1356     }
1357   return 0;
1358 }
1359
1360 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1361
1362 /*
1363  * setup function
1364  */
1365 static clib_error_t *
1366 bfd_udp_init (vlib_main_t * vm)
1367 {
1368   mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1369               sizeof (bfd_udp_key_t));
1370   bfd_udp_main.bfd_main = &bfd_main;
1371   bfd_udp_main.vnet_main = vnet_get_main ();
1372   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);
1374   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);
1378   return 0;
1379 }
1380
1381 VLIB_INIT_FUNCTION (bfd_udp_init);
1382
1383 /*
1384  * fd.io coding-style-patch-verification: ON
1385  *
1386  * Local Variables:
1387  * eval: (c-set-style "gnu")
1388  * End:
1389  */