44a4a18dbeb56ad165ebb9920340163d75e472f4
[vpp.git] / vnet / vnet / bfd / bfd_udp.c
1 #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
21 typedef 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
29 bfd_udp_main_t bfd_udp_main;
30
31 void bfd_udp_transport_to_buffer (vlib_main_t *vm, vlib_buffer_t *b,
32                                   bfd_udp_session_t *bus)
33 {
34   udp_header_t *udp;
35   u16 udp_length, ip_length;
36   bfd_udp_key_t *key = &bus->key;
37
38   b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
39   if (ip46_address_is_ip4 (&key->local_addr))
40     {
41       ip4_header_t *ip4;
42       const size_t data_size = sizeof (*ip4) + sizeof (*udp);
43       vlib_buffer_advance (b, -data_size);
44       ip4 = vlib_buffer_get_current (b);
45       udp = (udp_header_t *)(ip4 + 1);
46       memset (ip4, 0, data_size);
47       ip4->ip_version_and_header_length = 0x45;
48       ip4->ttl = 255;
49       ip4->protocol = IP_PROTOCOL_UDP;
50       ip4->src_address.as_u32 = key->local_addr.ip4.as_u32;
51       ip4->dst_address.as_u32 = key->peer_addr.ip4.as_u32;
52
53       udp->src_port = clib_host_to_net_u16 (50000); /* FIXME */
54       udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
55
56       /* fix ip length, checksum and udp length */
57       ip_length = vlib_buffer_length_in_chain (vm, b);
58
59       ip4->length = clib_host_to_net_u16 (ip_length);
60       ip4->checksum = ip4_header_checksum (ip4);
61
62       udp_length = ip_length - (sizeof (*ip4));
63       udp->length = clib_host_to_net_u16 (udp_length);
64     }
65   else
66     {
67       BFD_ERR ("not implemented");
68       abort ();
69     }
70 }
71
72 void bfd_add_udp_transport (vlib_main_t *vm, vlib_buffer_t *b,
73                             bfd_udp_session_t *bus)
74 {
75   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
76   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
77   bfd_udp_transport_to_buffer (vm, b, bus);
78 }
79
80 static bfd_session_t *bfd_lookup_session (bfd_udp_main_t *bum,
81                                           const bfd_udp_key_t *key)
82 {
83   uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
84   if (p)
85     {
86       return bfd_find_session_by_idx (bum->bfd_main, *p);
87     }
88   return 0;
89 }
90
91 static vnet_api_error_t
92 bfd_udp_add_session_internal (bfd_udp_main_t *bum, u32 sw_if_index,
93                               u32 desired_min_tx_us, u32 required_min_rx_us,
94                               u8 detect_mult, const ip46_address_t *local_addr,
95                               const ip46_address_t *peer_addr)
96 {
97   vnet_sw_interface_t *sw_if =
98       vnet_get_sw_interface (vnet_get_main (), sw_if_index);
99   /* get a pool entry and if we end up not needing it, give it back */
100   bfd_transport_t t = BFD_TRANSPORT_UDP4;
101   if (!ip46_address_is_ip4 (local_addr))
102     {
103       t = BFD_TRANSPORT_UDP6;
104     }
105   bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
106   bfd_udp_session_t *bus = &bs->udp;
107   memset (bus, 0, sizeof (*bus));
108   bfd_udp_key_t *key = &bus->key;
109   key->sw_if_index = sw_if->sw_if_index;
110   key->local_addr.as_u64[0] = local_addr->as_u64[0];
111   key->local_addr.as_u64[1] = local_addr->as_u64[1];
112   key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
113   key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
114   const bfd_session_t *tmp = bfd_lookup_session (bum, key);
115   if (tmp)
116     {
117       BFD_ERR ("duplicate bfd-udp session, existing bs_idx=%d", tmp->bs_idx);
118       bfd_put_session (bum->bfd_main, bs);
119       return VNET_API_ERROR_BFD_EEXIST;
120     }
121   key->sw_if_index = sw_if->sw_if_index;
122   mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
123   BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
124            bs->bs_idx, key->sw_if_index, format_ip46_address, &key->local_addr,
125            IP46_TYPE_ANY, format_ip46_address, &key->peer_addr, IP46_TYPE_ANY);
126   if (BFD_TRANSPORT_UDP4 == t)
127     {
128       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
129                                             &key->peer_addr, key->sw_if_index);
130       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
131                "returns %d",
132                format_ip46_address, &key->peer_addr, IP46_TYPE_ANY,
133                key->sw_if_index, bus->adj_index);
134     }
135   else
136     {
137       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
138                                             &key->peer_addr, key->sw_if_index);
139       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
140                "returns %d",
141                format_ip46_address, &key->peer_addr, IP46_TYPE_ANY,
142                key->sw_if_index, bus->adj_index);
143     }
144   bs->config_desired_min_tx_us = desired_min_tx_us;
145   bs->required_min_rx_us = required_min_rx_us;
146   bs->local_detect_mult = detect_mult;
147   bfd_session_start (bum->bfd_main, bs);
148   return 0;
149 }
150
151 static vnet_api_error_t
152 bfd_udp_validate_api_input (u32 sw_if_index, const ip46_address_t *local_addr,
153                             const ip46_address_t *peer_addr)
154 {
155   vnet_sw_interface_t *sw_if =
156       vnet_get_sw_interface (vnet_get_main (), sw_if_index);
157   u8 local_ip_valid = 0;
158   ip_interface_address_t *ia = NULL;
159   if (!sw_if)
160     {
161       BFD_ERR ("got NULL sw_if");
162       return VNET_API_ERROR_INVALID_SW_IF_INDEX;
163     }
164   if (ip46_address_is_ip4 (local_addr))
165     {
166       if (!ip46_address_is_ip4 (peer_addr))
167         {
168           BFD_ERR ("IP family mismatch");
169           return VNET_API_ERROR_INVALID_ARGUMENT;
170         }
171       ip4_main_t *im = &ip4_main;
172
173       /* *INDENT-OFF* */
174       foreach_ip_interface_address (
175           &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
176             ip4_address_t *x =
177                 ip_interface_address_get_address (&im->lookup_main, ia);
178             if (x->as_u32 == local_addr->ip4.as_u32)
179               {
180                 /* valid address for this interface */
181                 local_ip_valid = 1;
182                 break;
183               }
184           }));
185       /* *INDENT-ON* */
186     }
187   else
188     {
189       if (ip46_address_is_ip4 (peer_addr))
190         {
191           BFD_ERR ("IP family mismatch");
192           return VNET_API_ERROR_INVALID_ARGUMENT;
193         }
194       ip6_main_t *im = &ip6_main;
195       /* *INDENT-OFF* */
196       foreach_ip_interface_address (
197           &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
198             ip6_address_t *x =
199                 ip_interface_address_get_address (&im->lookup_main, ia);
200             if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
201                 local_addr->ip6.as_u64[1] == x->as_u64[1])
202               {
203                 /* valid address for this interface */
204                 local_ip_valid = 1;
205                 break;
206               }
207           }));
208       /* *INDENT-ON* */
209     }
210
211   if (!local_ip_valid)
212     {
213       BFD_ERR ("address not found on interface");
214       return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
215     }
216
217   return 0;
218 }
219
220 vnet_api_error_t bfd_udp_add_session (u32 sw_if_index, u32 desired_min_tx_us,
221                                       u32 required_min_rx_us, u8 detect_mult,
222                                       const ip46_address_t *local_addr,
223                                       const ip46_address_t *peer_addr)
224 {
225   vnet_api_error_t rv =
226       bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
227   if (rv)
228     {
229       return rv;
230     }
231   if (detect_mult < 1)
232     {
233       BFD_ERR ("detect_mult < 1");
234       return VNET_API_ERROR_INVALID_ARGUMENT;
235     }
236   if (desired_min_tx_us < 1)
237     {
238       BFD_ERR ("desired_min_tx_us < 1");
239       return VNET_API_ERROR_INVALID_ARGUMENT;
240     }
241   return bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
242                                        desired_min_tx_us, required_min_rx_us,
243                                        detect_mult, local_addr, peer_addr);
244 }
245
246 vnet_api_error_t bfd_udp_del_session (u32 sw_if_index,
247                                       const ip46_address_t *local_addr,
248                                       const ip46_address_t *peer_addr)
249 {
250   vnet_api_error_t rv =
251       bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
252   if (rv)
253     {
254       return rv;
255     }
256   bfd_udp_main_t *bum = &bfd_udp_main;
257   vnet_sw_interface_t *sw_if =
258       vnet_get_sw_interface (vnet_get_main (), sw_if_index);
259   bfd_udp_key_t key;
260   memset (&key, 0, sizeof (key));
261   key.sw_if_index = sw_if->sw_if_index;
262   key.local_addr.as_u64[0] = local_addr->as_u64[0];
263   key.local_addr.as_u64[1] = local_addr->as_u64[1];
264   key.peer_addr.as_u64[0] = peer_addr->as_u64[0];
265   key.peer_addr.as_u64[1] = peer_addr->as_u64[1];
266   bfd_session_t *tmp = bfd_lookup_session (bum, &key);
267   if (tmp)
268     {
269       BFD_DBG ("free bfd-udp session, bs_idx=%d", tmp->bs_idx);
270       mhash_unset (&bum->bfd_session_idx_by_bfd_key, &key, NULL);
271       adj_unlock (tmp->udp.adj_index);
272       bfd_put_session (bum->bfd_main, tmp);
273     }
274   else
275     {
276       BFD_ERR ("no such session");
277       return VNET_API_ERROR_BFD_NOENT;
278     }
279   return 0;
280 }
281
282 typedef enum {
283   BFD_UDP_INPUT_NEXT_NORMAL,
284   BFD_UDP_INPUT_NEXT_REPLY,
285   BFD_UDP_INPUT_N_NEXT,
286 } bfd_udp_input_next_t;
287
288 /* Packet counters */
289 #define foreach_bfd_udp_error(F)           \
290   F (NONE, "good bfd packets (processed)") \
291   F (BAD, "invalid bfd packets")           \
292   F (DISABLED, "bfd packets received on disabled interfaces")
293
294 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
295 foreach_bfd_udp_error (F);
296 #undef F
297
298 static char *bfd_udp_error_strings[] = {
299 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
300   foreach_bfd_udp_error (F)
301 #undef F
302 };
303
304 typedef enum {
305 #define F(sym, str) BFD_UDP_ERROR_##sym,
306   foreach_bfd_udp_error (F)
307 #undef F
308       BFD_UDP_N_ERROR,
309 } bfd_udp_error_t;
310
311 static void bfd_udp4_find_headers (vlib_buffer_t *b, const ip4_header_t **ip4,
312                                    const udp_header_t **udp)
313 {
314   /* sanity check first */
315   const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
316   if (start < 0 && start < sizeof (b->pre_data))
317     {
318       BFD_ERR ("Start of ip header is before pre_data, ignoring");
319       *ip4 = NULL;
320       *udp = NULL;
321       return;
322     }
323   *ip4 = (ip4_header_t *)(b->data + start);
324   if ((u8 *)*ip4 > (u8 *)vlib_buffer_get_current (b))
325     {
326       BFD_ERR ("Start of ip header is beyond current data, ignoring");
327       *ip4 = NULL;
328       *udp = NULL;
329       return;
330     }
331   *udp = (udp_header_t *)((*ip4) + 1);
332 }
333
334 static bfd_udp_error_t bfd_udp4_verify_transport (const ip4_header_t *ip4,
335                                                   const udp_header_t *udp,
336                                                   const bfd_session_t *bs)
337 {
338   const bfd_udp_session_t *bus = &bs->udp;
339   const bfd_udp_key_t *key = &bus->key;
340   if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
341     {
342       BFD_ERR ("IP src addr mismatch, got %U, expected %U", format_ip4_address,
343                ip4->src_address.as_u32, format_ip4_address,
344                key->peer_addr.ip4.as_u32);
345       return BFD_UDP_ERROR_BAD;
346     }
347   if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
348     {
349       BFD_ERR ("IP dst addr mismatch, got %U, expected %U", format_ip4_address,
350                ip4->dst_address.as_u32, format_ip4_address,
351                key->local_addr.ip4.as_u32);
352       return BFD_UDP_ERROR_BAD;
353     }
354   const u8 expected_ttl = 255;
355   if (ip4->ttl != expected_ttl)
356     {
357       BFD_ERR ("IP unexpected TTL value %d, expected %d", ip4->ttl,
358                expected_ttl);
359       return BFD_UDP_ERROR_BAD;
360     }
361   if (clib_net_to_host_u16 (udp->src_port) < 49152 ||
362       clib_net_to_host_u16 (udp->src_port) > 65535)
363     {
364       BFD_ERR ("Invalid UDP src port %d, out of range <49152,65535>",
365                udp->src_port);
366     }
367   return BFD_UDP_ERROR_NONE;
368 }
369
370 typedef struct
371 {
372   u32 bs_idx;
373   bfd_pkt_t pkt;
374 } bfd_rpc_update_t;
375
376 static void bfd_rpc_update_session_cb (const bfd_rpc_update_t *a)
377 {
378   bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
379 }
380
381 static void bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t *pkt)
382 {
383   /* packet length was already verified to be correct by the caller */
384   const u32 data_size = sizeof (bfd_rpc_update_t) -
385                         STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) +
386                         pkt->head.length;
387   u8 data[data_size];
388   bfd_rpc_update_t *update = (bfd_rpc_update_t *)data;
389   update->bs_idx = bs_idx;
390   clib_memcpy (&update->pkt, pkt, pkt->head.length);
391   vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
392 }
393
394 static bfd_udp_error_t bfd_udp4_scan (vlib_main_t *vm, vlib_node_runtime_t *rt,
395                                       vlib_buffer_t *b, bfd_session_t **bs_out)
396 {
397   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
398   if (sizeof (*pkt) > b->current_length)
399     {
400       BFD_ERR (
401           "Payload size %d too small to hold bfd packet of minimum size %d",
402           b->current_length, sizeof (*pkt));
403       return BFD_UDP_ERROR_BAD;
404     }
405   const ip4_header_t *ip4;
406   const udp_header_t *udp;
407   bfd_udp4_find_headers (b, &ip4, &udp);
408   if (!ip4 || !udp)
409     {
410       BFD_ERR ("Couldn't find ip4 or udp header");
411       return BFD_UDP_ERROR_BAD;
412     }
413   if (!bfd_verify_pkt_common (pkt))
414     {
415       return BFD_UDP_ERROR_BAD;
416     }
417   bfd_session_t *bs = NULL;
418   if (pkt->your_disc)
419     {
420       BFD_DBG ("Looking up BFD session using discriminator %u",
421                pkt->your_disc);
422       bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
423     }
424   else
425     {
426       bfd_udp_key_t key;
427       memset (&key, 0, sizeof (key));
428       key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
429       key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
430       key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
431       BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
432                "peer=%U)",
433                key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
434                format_ip4_address, key.peer_addr.ip4.as_u8);
435       bs = bfd_lookup_session (&bfd_udp_main, &key);
436     }
437   if (!bs)
438     {
439       BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
440       return BFD_UDP_ERROR_BAD;
441     }
442   BFD_DBG ("BFD session found, bs_idx=%d", bs->bs_idx);
443   if (!bfd_verify_pkt_session (pkt, b->current_length, bs))
444     {
445       return BFD_UDP_ERROR_BAD;
446     }
447   bfd_udp_error_t err;
448   if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
449     {
450       return err;
451     }
452   bfd_rpc_update_session (bs->bs_idx, pkt);
453   *bs_out = bs;
454   return BFD_UDP_ERROR_NONE;
455 }
456
457 static bfd_udp_error_t bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b)
458 {
459   /* TODO */
460   return BFD_UDP_ERROR_BAD;
461 }
462
463 /*
464  * Process a frame of bfd packets
465  * Expect 1 packet / frame
466  */
467 static uword bfd_udp_input (vlib_main_t *vm, vlib_node_runtime_t *rt,
468                             vlib_frame_t *f, int is_ipv6)
469 {
470   u32 n_left_from, *from;
471   bfd_input_trace_t *t0;
472
473   from = vlib_frame_vector_args (f); /* array of buffer indices */
474   n_left_from = f->n_vectors;        /* number of buffer indices */
475
476   while (n_left_from > 0)
477     {
478       u32 bi0;
479       vlib_buffer_t *b0;
480       u32 next0, error0;
481
482       bi0 = from[0];
483       b0 = vlib_get_buffer (vm, bi0);
484
485       bfd_session_t *bs = NULL;
486
487       /* If this pkt is traced, snapshot the data */
488       if (b0->flags & VLIB_BUFFER_IS_TRACED)
489         {
490           int len;
491           t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
492           len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
493                                                          : sizeof (t0->data);
494           t0->len = len;
495           clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
496         }
497
498       /* scan this bfd pkt. error0 is the counter index to bmp */
499       if (is_ipv6)
500         {
501           error0 = bfd_udp6_scan (vm, b0);
502         }
503       else
504         {
505           error0 = bfd_udp4_scan (vm, rt, b0, &bs);
506         }
507       b0->error = rt->errors[error0];
508
509       next0 = BFD_UDP_INPUT_NEXT_NORMAL;
510       if (BFD_UDP_ERROR_NONE == error0)
511         {
512           /* if everything went fine, check for poll bit, if present, re-use
513              the buffer and based on (now update) session parameters, send the
514              final packet back */
515           const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
516           if (bfd_pkt_get_poll (pkt))
517             {
518               bfd_send_final (vm, b0, bs);
519               next0 = BFD_UDP_INPUT_NEXT_REPLY;
520             }
521         }
522       vlib_set_next_frame_buffer (vm, rt, next0, bi0);
523
524       from += 1;
525       n_left_from -= 1;
526     }
527
528   return f->n_vectors;
529 }
530
531 static uword bfd_udp4_input (vlib_main_t *vm, vlib_node_runtime_t *rt,
532                              vlib_frame_t *f)
533 {
534   return bfd_udp_input (vm, rt, f, 0);
535 }
536
537 /*
538  * bfd input graph node declaration
539  */
540 /* *INDENT-OFF* */
541 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
542   .function = bfd_udp4_input,
543   .name = "bfd-udp4-input",
544   .vector_size = sizeof (u32),
545   .type = VLIB_NODE_TYPE_INTERNAL,
546
547   .n_errors = BFD_UDP_N_ERROR,
548   .error_strings = bfd_udp_error_strings,
549
550   .format_trace = bfd_input_format_trace,
551
552   .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
553   .next_nodes =
554       {
555               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
556               [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
557       },
558 };
559 /* *INDENT-ON* */
560
561 static uword bfd_udp6_input (vlib_main_t *vm, vlib_node_runtime_t *rt,
562                              vlib_frame_t *f)
563 {
564   return bfd_udp_input (vm, rt, f, 1);
565 }
566
567 /* *INDENT-OFF* */
568 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
569   .function = bfd_udp6_input,
570   .name = "bfd-udp6-input",
571   .vector_size = sizeof (u32),
572   .type = VLIB_NODE_TYPE_INTERNAL,
573
574   .n_errors = BFD_UDP_N_ERROR,
575   .error_strings = bfd_udp_error_strings,
576
577   .format_trace = bfd_input_format_trace,
578
579   .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
580   .next_nodes =
581       {
582               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
583               [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
584       },
585 };
586 /* *INDENT-ON* */
587
588 static clib_error_t *bfd_sw_interface_up_down (vnet_main_t *vnm,
589                                                u32 sw_if_index, u32 flags)
590 {
591   // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
592   if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
593     {
594       /* TODO */
595     }
596   return 0;
597 }
598
599 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
600
601 static clib_error_t *bfd_hw_interface_up_down (vnet_main_t *vnm,
602                                                u32 hw_if_index, u32 flags)
603 {
604   if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
605     {
606       /* TODO */
607     }
608   return 0;
609 }
610
611 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
612
613 /*
614  * setup function
615  */
616 static clib_error_t *bfd_udp_init (vlib_main_t *vm)
617 {
618   mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
619               sizeof (bfd_udp_key_t));
620   bfd_udp_main.bfd_main = &bfd_main;
621   udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
622   udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
623   return 0;
624 }
625
626 VLIB_INIT_FUNCTION (bfd_udp_init);