bfd: refactor code to fix misc warnings
[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_local.h>
26 #include <vnet/udp/udp_packet.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/ip/ip6_link.h>
33 #include <vnet/adj/adj.h>
34 #include <vnet/adj/adj_nbr.h>
35 #include <vnet/dpo/receive_dpo.h>
36 #include <vnet/fib/fib_entry.h>
37 #include <vnet/fib/fib_table.h>
38 #include <vnet/bfd/bfd_debug.h>
39 #include <vnet/bfd/bfd_udp.h>
40 #include <vnet/bfd/bfd_main.h>
41 #include <vnet/bfd/bfd_api.h>
42
43 typedef struct
44 {
45   bfd_main_t *bfd_main;
46   /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
47    * discriminator is unknown */
48   mhash_t bfd_session_idx_by_bfd_key;
49   /* convenience variable */
50   vnet_main_t *vnet_main;
51   /* flag indicating whether echo_source_sw_if_index holds a valid value */
52   int echo_source_is_set;
53   /* loopback interface used to get echo source ip */
54   u32 echo_source_sw_if_index;
55   /* node index of "ip4-arp" node */
56   u32 ip4_arp_idx;
57   /* node index of "ip6-discover-neighbor" node */
58   u32 ip6_ndp_idx;
59   /* node index of "ip4-rewrite" node */
60   u32 ip4_rewrite_idx;
61   /* node index of "ip6-rewrite" node */
62   u32 ip6_rewrite_idx;
63   /* node index of "ip4-midchain" node */
64   u32 ip4_midchain_idx;
65   /* node index of "ip6-midchain" node */
66   u32 ip6_midchain_idx;
67   /* log class */
68   vlib_log_class_t log_class;
69   /* number of active udp4 sessions */
70   u32 udp4_sessions_count;
71   /* number of active udp6 sessions */
72   u32 udp6_sessions_count;
73 } bfd_udp_main_t;
74
75 static vlib_node_registration_t bfd_udp4_input_node;
76 static vlib_node_registration_t bfd_udp6_input_node;
77 static vlib_node_registration_t bfd_udp_echo4_input_node;
78 static vlib_node_registration_t bfd_udp_echo6_input_node;
79
80 bfd_udp_main_t bfd_udp_main;
81
82 vnet_api_error_t
83 bfd_udp_set_echo_source (u32 sw_if_index)
84 {
85   vnet_sw_interface_t *sw_if =
86     vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
87   if (sw_if)
88     {
89       bfd_udp_main.echo_source_sw_if_index = sw_if_index;
90       bfd_udp_main.echo_source_is_set = 1;
91       return 0;
92     }
93   return VNET_API_ERROR_BFD_ENOENT;
94 }
95
96 vnet_api_error_t
97 bfd_udp_del_echo_source (u32 sw_if_index)
98 {
99   bfd_udp_main.echo_source_sw_if_index = ~0;
100   bfd_udp_main.echo_source_is_set = 0;
101   return 0;
102 }
103
104 int
105 bfd_udp_is_echo_available (bfd_transport_e transport)
106 {
107   if (!bfd_udp_main.echo_source_is_set)
108     {
109       BFD_DBG ("UDP echo source not set - echo not available");
110       return 0;
111     }
112   /*
113    * for the echo to work, we need a loopback interface with at least one
114    * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
115    * pick an unused address from that subnet
116    */
117   vnet_sw_interface_t *sw_if =
118     vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
119                                    bfd_udp_main.echo_source_sw_if_index);
120   if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
121     {
122       if (BFD_TRANSPORT_UDP4 == transport)
123         {
124           ip4_main_t *im = &ip4_main;
125           ip_interface_address_t *ia = NULL;
126           /* *INDENT-OFF* */
127           foreach_ip_interface_address (&im->lookup_main, ia,
128                                         bfd_udp_main.echo_source_sw_if_index,
129                                         0 /* honor unnumbered */, ({
130                                           if (ia->address_length <= 31)
131                                             {
132                                               return 1;
133                                             }
134                                         }));
135           /* *INDENT-ON* */
136         }
137       else if (BFD_TRANSPORT_UDP6 == transport)
138         {
139           ip6_main_t *im = &ip6_main;
140           ip_interface_address_t *ia = NULL;
141           /* *INDENT-OFF* */
142           foreach_ip_interface_address (&im->lookup_main, ia,
143                                         bfd_udp_main.echo_source_sw_if_index,
144                                         0 /* honor unnumbered */, ({
145                                           if (ia->address_length <= 127)
146                                             {
147                                               return 1;
148                                             }
149                                         }));
150           /* *INDENT-ON* */
151         }
152     }
153   BFD_DBG ("No usable IP address for UDP echo - echo not available");
154   return 0;
155 }
156
157 static u16
158 bfd_udp_bs_idx_to_sport (u32 bs_idx)
159 {
160   /* The source port MUST be in the range 49152 through 65535. The same UDP
161    * source port number MUST be used for all BFD Control packets associated
162    * with a particular session.  The source port number SHOULD be unique among
163    * all BFD sessions on the system. If more than 16384 BFD sessions are
164    * simultaneously active, UDP source port numbers MAY be reused on
165    * multiple sessions, but the number of distinct uses of the same UDP
166    * source port number SHOULD be minimized.
167    */
168   return 49152 + bs_idx % (65535 - 49152 + 1);
169 }
170
171 int
172 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
173 {
174   if (!bfd_udp_main.echo_source_is_set)
175     {
176       BFD_ERR ("cannot find ip4 address, echo source not set");
177       return 0;
178     }
179   ip_interface_address_t *ia = NULL;
180   ip4_main_t *im = &ip4_main;
181
182   /* *INDENT-OFF* */
183   foreach_ip_interface_address (
184       &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
185       0 /* honor unnumbered */, ({
186         ip4_address_t *x =
187             ip_interface_address_get_address (&im->lookup_main, ia);
188         if (ia->address_length <= 31)
189           {
190             addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
191             /*
192              * flip the last bit to get a different address, might be network,
193              * we don't care ...
194              */
195             addr->as_u32 ^= 1;
196             addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
197             return 1;
198           }
199       }));
200   /* *INDENT-ON* */
201   BFD_ERR ("cannot find ip4 address, no usable address found");
202   return 0;
203 }
204
205 int
206 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
207 {
208   if (!bfd_udp_main.echo_source_is_set)
209     {
210       BFD_ERR ("cannot find ip6 address, echo source not set");
211       return 0;
212     }
213   ip_interface_address_t *ia = NULL;
214   ip6_main_t *im = &ip6_main;
215
216   /* *INDENT-OFF* */
217   foreach_ip_interface_address (
218       &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
219       0 /* honor unnumbered */, ({
220         ip6_address_t *x =
221             ip_interface_address_get_address (&im->lookup_main, ia);
222         if (ia->address_length <= 127)
223           {
224             *addr = *x;
225             addr->as_u8[15] ^= 1; /* flip the last bit of the address */
226             return 1;
227           }
228       }));
229   /* *INDENT-ON* */
230   BFD_ERR ("cannot find ip6 address, no usable address found");
231   return 0;
232 }
233
234 void
235 bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
236                          int *have_usable_ip4, ip4_address_t * ip4,
237                          int *have_usable_ip6, ip6_address_t * ip6)
238 {
239   if (bfd_udp_main.echo_source_is_set)
240     {
241       *is_set = 1;
242       *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
243       *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
244       *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
245     }
246   else
247     {
248       *is_set = 0;
249     }
250 }
251
252 int
253 bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
254                         int is_echo)
255 {
256   const bfd_udp_session_t *bus = &bs->udp;
257   const bfd_udp_key_t *key = &bus->key;
258   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
259
260   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
261   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
262   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
263   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
264   vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
265   typedef struct
266   {
267     ip4_header_t ip4;
268     udp_header_t udp;
269   } ip4_udp_headers;
270   ip4_udp_headers *headers = NULL;
271   vlib_buffer_advance (b, -sizeof (*headers));
272   headers = vlib_buffer_get_current (b);
273   clib_memset (headers, 0, sizeof (*headers));
274   headers->ip4.ip_version_and_header_length = 0x45;
275   headers->ip4.ttl = 255;
276   headers->ip4.protocol = IP_PROTOCOL_UDP;
277   headers->udp.src_port =
278     clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
279   if (is_echo)
280     {
281       int rv;
282       if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
283         {
284           return rv;
285         }
286       headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
287       headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
288     }
289   else
290     {
291       headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
292       headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
293       headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
294     }
295
296   /* fix ip length, checksum and udp length */
297   const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
298
299   headers->ip4.length = clib_host_to_net_u16 (ip_length);
300   headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
301
302   const u16 udp_length = ip_length - (sizeof (headers->ip4));
303   headers->udp.length = clib_host_to_net_u16 (udp_length);
304   return 1;
305 }
306
307 int
308 bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
309                         int is_echo)
310 {
311   const bfd_udp_session_t *bus = &bs->udp;
312   const bfd_udp_key_t *key = &bus->key;
313   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
314
315   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
316   vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
317   vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
318   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
319   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
320   typedef struct
321   {
322     ip6_header_t ip6;
323     udp_header_t udp;
324   } ip6_udp_headers;
325   ip6_udp_headers *headers = NULL;
326   vlib_buffer_advance (b, -sizeof (*headers));
327   headers = vlib_buffer_get_current (b);
328   clib_memset (headers, 0, sizeof (*headers));
329   headers->ip6.ip_version_traffic_class_and_flow_label =
330     clib_host_to_net_u32 (0x6 << 28);
331   headers->ip6.hop_limit = 255;
332   headers->ip6.protocol = IP_PROTOCOL_UDP;
333   headers->udp.src_port =
334     clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
335   if (is_echo)
336     {
337       int rv;
338       if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
339         {
340           return rv;
341         }
342       clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
343                         sizeof (headers->ip6.dst_address));
344
345       headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
346     }
347   else
348     {
349       clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
350                         sizeof (headers->ip6.src_address));
351       clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
352                         sizeof (headers->ip6.dst_address));
353       headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
354     }
355
356   /* fix ip payload length and udp length */
357   const u16 udp_length =
358     vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
359   headers->udp.length = clib_host_to_net_u16 (udp_length);
360   headers->ip6.payload_length = headers->udp.length;
361
362   /* IPv6 UDP checksum is mandatory */
363   int bogus = 0;
364   headers->udp.checksum =
365     ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
366   ASSERT (bogus == 0);
367   if (headers->udp.checksum == 0)
368     {
369       headers->udp.checksum = 0xffff;
370     }
371   return 1;
372 }
373
374 static void
375 bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
376 {
377   vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
378   u32 *to_next = vlib_frame_vector_args (f);
379   to_next[0] = bi;
380   f->n_vectors = 1;
381   vlib_put_frame_to_node (vm, next_node, f);
382 }
383
384 int
385 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
386 {
387   vnet_main_t *vnm = vnet_get_main ();
388   const bfd_udp_session_t *bus = &bs->udp;
389   ip_adjacency_t *adj = adj_get (bus->adj_index);
390
391   /* don't try to send the buffer if the interface is not up */
392   if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
393     return 0;
394
395   switch (adj->lookup_next_index)
396     {
397     case IP_LOOKUP_NEXT_ARP:
398       switch (bs->transport)
399         {
400         case BFD_TRANSPORT_UDP4:
401           *next_node = bfd_udp_main.ip4_arp_idx;
402           return 1;
403         case BFD_TRANSPORT_UDP6:
404           *next_node = bfd_udp_main.ip6_ndp_idx;
405           return 1;
406         }
407       break;
408     case IP_LOOKUP_NEXT_REWRITE:
409       switch (bs->transport)
410         {
411         case BFD_TRANSPORT_UDP4:
412           *next_node = bfd_udp_main.ip4_rewrite_idx;
413           return 1;
414         case BFD_TRANSPORT_UDP6:
415           *next_node = bfd_udp_main.ip6_rewrite_idx;
416           return 1;
417         }
418       break;
419     case IP_LOOKUP_NEXT_MIDCHAIN:
420       switch (bs->transport)
421         {
422         case BFD_TRANSPORT_UDP4:
423           *next_node = bfd_udp_main.ip4_midchain_idx;
424           return 1;
425         case BFD_TRANSPORT_UDP6:
426           *next_node = bfd_udp_main.ip6_midchain_idx;
427           return 1;
428         }
429       break;
430     default:
431       /* drop */
432       break;
433     }
434   return 0;
435 }
436
437 int
438 bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
439 {
440   u32 next_node;
441   int rv = bfd_udp_calc_next_node (bs, &next_node);
442   if (rv)
443     {
444       bfd_create_frame_to_next_node (vm, bi, next_node);
445     }
446   return rv;
447 }
448
449 int
450 bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
451 {
452   u32 next_node;
453   int rv = bfd_udp_calc_next_node (bs, &next_node);
454   if (rv)
455     {
456       bfd_create_frame_to_next_node (vm, bi, next_node);
457     }
458   return 1;
459 }
460
461 static bfd_session_t *
462 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
463 {
464   uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
465   if (p)
466     {
467       return bfd_find_session_by_idx (bum->bfd_main, *p);
468     }
469   return 0;
470 }
471
472 static void
473 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
474                   const ip46_address_t * local_addr,
475                   const ip46_address_t * peer_addr)
476 {
477   clib_memset (key, 0, sizeof (*key));
478   key->sw_if_index = sw_if_index;
479   key->local_addr.as_u64[0] = local_addr->as_u64[0];
480   key->local_addr.as_u64[1] = local_addr->as_u64[1];
481   key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
482   key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
483 }
484
485 static vnet_api_error_t
486 bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
487                               u32 sw_if_index, u32 desired_min_tx_usec,
488                               u32 required_min_rx_usec, u8 detect_mult,
489                               const ip46_address_t * local_addr,
490                               const ip46_address_t * peer_addr,
491                               bfd_session_t ** bs_out)
492 {
493   /* get a pool entry and if we end up not needing it, give it back */
494   bfd_transport_e t = BFD_TRANSPORT_UDP4;
495   if (!ip46_address_is_ip4 (local_addr))
496     {
497       t = BFD_TRANSPORT_UDP6;
498     }
499   bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
500   if (!bs)
501     {
502       return VNET_API_ERROR_BFD_EAGAIN;
503     }
504   bfd_udp_session_t *bus = &bs->udp;
505   clib_memset (bus, 0, sizeof (*bus));
506   bfd_udp_key_t *key = &bus->key;
507   bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
508   const bfd_session_t *tmp = bfd_lookup_session (bum, key);
509   if (tmp)
510     {
511       vlib_log_err (bum->log_class,
512                     "duplicate bfd-udp session, existing bs_idx=%d",
513                     tmp->bs_idx);
514       bfd_put_session (bum->bfd_main, bs);
515       return VNET_API_ERROR_BFD_EEXIST;
516     }
517   mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
518   BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
519            bs->bs_idx, key->sw_if_index, format_ip46_address,
520            &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
521            &key->peer_addr, IP46_TYPE_ANY);
522   vlib_log_info (bum->log_class, "create BFD session: %U",
523                  format_bfd_session, bs);
524   if (BFD_TRANSPORT_UDP4 == t)
525     {
526       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
527                                             &key->peer_addr,
528                                             key->sw_if_index);
529       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
530                "returns %d", format_ip46_address, &key->peer_addr,
531                IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
532       ++bum->udp4_sessions_count;
533       if (1 == bum->udp4_sessions_count)
534         {
535           udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
536                                  bfd_udp4_input_node.index, 1);
537           udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
538                                  bfd_udp_echo4_input_node.index, 1);
539         }
540     }
541   else
542     {
543       bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
544                                             &key->peer_addr,
545                                             key->sw_if_index);
546       BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
547                "returns %d", format_ip46_address, &key->peer_addr,
548                IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
549       ++bum->udp6_sessions_count;
550       if (1 == bum->udp6_sessions_count)
551         {
552           udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
553                                  bfd_udp6_input_node.index, 0);
554           udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
555                                  bfd_udp_echo6_input_node.index, 0);
556         }
557     }
558   *bs_out = bs;
559   return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
560                                  required_min_rx_usec, detect_mult);
561 }
562
563 static vnet_api_error_t
564 bfd_udp_validate_api_input (u32 sw_if_index,
565                             const ip46_address_t * local_addr,
566                             const ip46_address_t * peer_addr)
567 {
568   bfd_udp_main_t *bum = &bfd_udp_main;
569   vnet_sw_interface_t *sw_if =
570     vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
571   u8 local_ip_valid = 0;
572   ip_interface_address_t *ia = NULL;
573   if (!sw_if)
574     {
575       vlib_log_err (bum->log_class,
576                     "got NULL sw_if when getting interface by index %u",
577                     sw_if_index);
578       return VNET_API_ERROR_INVALID_SW_IF_INDEX;
579     }
580   if (ip46_address_is_ip4 (local_addr))
581     {
582       if (!ip46_address_is_ip4 (peer_addr))
583         {
584           vlib_log_err (bum->log_class,
585                         "IP family mismatch (local is ipv4, peer is ipv6)");
586           return VNET_API_ERROR_INVALID_ARGUMENT;
587         }
588       ip4_main_t *im = &ip4_main;
589
590       /* *INDENT-OFF* */
591       foreach_ip_interface_address (
592           &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
593             ip4_address_t *x =
594                 ip_interface_address_get_address (&im->lookup_main, ia);
595             if (x->as_u32 == local_addr->ip4.as_u32)
596               {
597                 /* valid address for this interface */
598                 local_ip_valid = 1;
599                 break;
600               }
601           }));
602       /* *INDENT-ON* */
603     }
604   else
605     {
606       if (ip46_address_is_ip4 (peer_addr))
607         {
608           vlib_log_err (bum->log_class,
609                         "IP family mismatch (local is ipv6, peer is ipv4)");
610           return VNET_API_ERROR_INVALID_ARGUMENT;
611         }
612
613       if (ip6_address_is_link_local_unicast (&local_addr->ip6))
614         {
615           const ip6_address_t *ll_addr;
616           ll_addr = ip6_get_link_local_address (sw_if_index);
617           if (ll_addr && ip6_address_is_equal (ll_addr, &local_addr->ip6))
618             {
619               /* valid address for this interface */
620               local_ip_valid = 1;
621             }
622         }
623       else
624         {
625           ip6_main_t *im = &ip6_main;
626           /* *INDENT-OFF* */
627           foreach_ip_interface_address (
628               &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
629                 ip6_address_t *x =
630                     ip_interface_address_get_address (&im->lookup_main, ia);
631                 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
632                     local_addr->ip6.as_u64[1] == x->as_u64[1])
633                   {
634                     /* valid address for this interface */
635                     local_ip_valid = 1;
636                     break;
637                   }
638               }));
639           /* *INDENT-ON* */
640         }
641     }
642
643   if (!local_ip_valid)
644     {
645       vlib_log_err (bum->log_class,
646                     "local address %U not found on interface with index %u",
647                     format_ip46_address, local_addr, IP46_TYPE_ANY,
648                     sw_if_index);
649       return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
650     }
651
652   return 0;
653 }
654
655 static vnet_api_error_t
656 bfd_udp_find_session_by_api_input (u32 sw_if_index,
657                                    const ip46_address_t * local_addr,
658                                    const ip46_address_t * peer_addr,
659                                    bfd_session_t ** bs_out)
660 {
661   vnet_api_error_t rv =
662     bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
663   if (!rv)
664     {
665       bfd_udp_main_t *bum = &bfd_udp_main;
666       bfd_udp_key_t key;
667       bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
668       bfd_session_t *bs = bfd_lookup_session (bum, &key);
669       if (bs)
670         {
671           *bs_out = bs;
672         }
673       else
674         {
675           vlib_log_err (bum->log_class,
676                         "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
677                         sw_if_index, format_ip46_address, local_addr,
678                         IP46_TYPE_ANY, format_ip46_address, peer_addr,
679                         IP46_TYPE_ANY);
680           return VNET_API_ERROR_BFD_ENOENT;
681         }
682     }
683   return rv;
684 }
685
686 static vnet_api_error_t
687 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
688                        u8 detect_mult, const ip46_address_t *local_addr,
689                        const ip46_address_t *peer_addr)
690 {
691   bfd_udp_main_t *bum = &bfd_udp_main;
692   vnet_api_error_t rv =
693     bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
694   if (rv)
695     {
696       return rv;
697     }
698   if (detect_mult < 1)
699     {
700       vlib_log_err (bum->log_class, "detect_mult < 1");
701       return VNET_API_ERROR_INVALID_ARGUMENT;
702     }
703   if (desired_min_tx_usec < 1)
704     {
705       vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
706       return VNET_API_ERROR_INVALID_ARGUMENT;
707     }
708   return 0;
709 }
710
711 static void
712 bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
713 {
714   bfd_udp_main_t *bum = &bfd_udp_main;
715   BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
716   mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
717   adj_unlock (bs->udp.adj_index);
718   switch (bs->transport)
719     {
720     case BFD_TRANSPORT_UDP4:
721       --bum->udp4_sessions_count;
722       if (!bum->udp4_sessions_count)
723         {
724           udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
725           udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
726         }
727       break;
728     case BFD_TRANSPORT_UDP6:
729       --bum->udp6_sessions_count;
730       if (!bum->udp6_sessions_count)
731         {
732           udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
733           udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
734         }
735       break;
736     }
737   bfd_put_session (bum->bfd_main, bs);
738 }
739
740 static vnet_api_error_t
741 bfd_udp_add_and_start_session (u32 sw_if_index,
742                                const ip46_address_t *local_addr,
743                                const ip46_address_t *peer_addr,
744                                u32 desired_min_tx_usec,
745                                u32 required_min_rx_usec, u8 detect_mult,
746                                u8 is_authenticated, u32 conf_key_id,
747                                u8 bfd_key_id)
748 {
749   bfd_session_t *bs = NULL;
750   vnet_api_error_t rv;
751
752   rv = bfd_udp_add_session_internal (
753     vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec,
754     required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs);
755
756   if (!rv && is_authenticated)
757     {
758       rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
759                               0 /* is not delayed */);
760       if (rv)
761         {
762           bfd_udp_del_session_internal (vlib_get_main (), bs);
763         }
764     }
765   if (!rv)
766     {
767       bfd_session_start (bfd_udp_main.bfd_main, bs);
768     }
769
770   return rv;
771 }
772
773 vnet_api_error_t
774 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
775                      const ip46_address_t * peer_addr,
776                      u32 desired_min_tx_usec, u32 required_min_rx_usec,
777                      u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
778                      u8 bfd_key_id)
779 {
780   bfd_main_t *bm = &bfd_main;
781   bfd_lock (bm);
782
783   vnet_api_error_t rv = bfd_api_verify_common (
784     sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
785
786   if (!rv)
787     rv = bfd_udp_add_and_start_session (
788       sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
789       required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
790       bfd_key_id);
791
792   bfd_unlock (bm);
793   return rv;
794 }
795
796 vnet_api_error_t
797 bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr,
798                      const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
799                      u32 required_min_rx_usec, u8 detect_mult,
800                      u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
801 {
802   bfd_main_t *bm = &bfd_main;
803   bfd_lock (bm);
804
805   vnet_api_error_t rv = bfd_api_verify_common (
806     sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
807   if (!rv)
808     {
809       bfd_session_t *bs = NULL;
810
811       rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr,
812                                               peer_addr, &bs);
813       if (VNET_API_ERROR_BFD_ENOENT == rv)
814         rv = bfd_udp_add_and_start_session (
815           sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
816           required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
817           bfd_key_id);
818       else
819         rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
820                                      desired_min_tx_usec, required_min_rx_usec,
821                                      detect_mult);
822     }
823
824   bfd_unlock (bm);
825   return rv;
826 }
827
828 vnet_api_error_t
829 bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr,
830                      const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
831                      u32 required_min_rx_usec, u8 detect_mult)
832 {
833   bfd_session_t *bs = NULL;
834   bfd_main_t *bm = &bfd_main;
835   vnet_api_error_t error;
836   bfd_lock (bm);
837   vnet_api_error_t rv =
838     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
839                                        &bs);
840   if (rv)
841     {
842       bfd_unlock (bm);
843       return rv;
844     }
845
846   error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
847                                   desired_min_tx_usec, required_min_rx_usec,
848                                   detect_mult);
849   bfd_unlock (bm);
850   return error;
851 }
852
853 vnet_api_error_t
854 bfd_udp_del_session (u32 sw_if_index,
855                      const ip46_address_t * local_addr,
856                      const ip46_address_t * peer_addr)
857 {
858   bfd_session_t *bs = NULL;
859   bfd_main_t *bm = &bfd_main;
860   bfd_lock (bm);
861   vnet_api_error_t rv =
862     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
863                                        &bs);
864   if (rv)
865     {
866       bfd_unlock (bm);
867       return rv;
868     }
869   bfd_udp_del_session_internal (vlib_get_main (), bs);
870   bfd_unlock (bm);
871   return 0;
872 }
873
874 vnet_api_error_t
875 bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
876                            const ip46_address_t * local_addr,
877                            const ip46_address_t * peer_addr, u8 admin_up_down)
878 {
879   bfd_session_t *bs = NULL;
880   bfd_main_t *bm = &bfd_main;
881   bfd_lock (bm);
882   vnet_api_error_t rv =
883     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
884                                        &bs);
885   if (rv)
886     {
887       bfd_unlock (bm);
888       return rv;
889     }
890   bfd_session_set_flags (vm, bs, admin_up_down);
891   bfd_unlock (bm);
892   return 0;
893 }
894
895 vnet_api_error_t
896 bfd_udp_auth_activate (u32 sw_if_index,
897                        const ip46_address_t * local_addr,
898                        const ip46_address_t * peer_addr,
899                        u32 conf_key_id, u8 key_id, u8 is_delayed)
900 {
901   bfd_main_t *bm = &bfd_main;
902   bfd_lock (bm);
903   vnet_api_error_t error;
904
905   bfd_session_t *bs = NULL;
906   vnet_api_error_t rv =
907     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
908                                        &bs);
909   if (rv)
910     {
911       bfd_unlock (bm);
912       return rv;
913     }
914   error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
915   bfd_unlock (bm);
916   return error;
917 }
918
919 vnet_api_error_t
920 bfd_udp_auth_deactivate (u32 sw_if_index,
921                          const ip46_address_t * local_addr,
922                          const ip46_address_t * peer_addr, u8 is_delayed)
923 {
924   bfd_main_t *bm = &bfd_main;
925   vnet_api_error_t error;
926   bfd_lock (bm);
927   bfd_session_t *bs = NULL;
928   vnet_api_error_t rv =
929     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
930                                        &bs);
931   if (rv)
932     {
933       bfd_unlock (bm);
934       return rv;
935     }
936   error = bfd_auth_deactivate (bs, is_delayed);
937   bfd_unlock (bm);
938   return error;
939 }
940
941 typedef enum
942 {
943   BFD_UDP_INPUT_NEXT_NORMAL,
944   BFD_UDP_INPUT_NEXT_REPLY_ARP,
945   BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
946   BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
947   BFD_UDP_INPUT_N_NEXT,
948 } bfd_udp_input_next_t;
949
950 /* Packet counters - BFD control frames */
951 #define foreach_bfd_udp_error(F)           \
952   F (NONE, "good bfd packets (processed)") \
953   F (BAD, "invalid bfd packets")
954
955 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
956 foreach_bfd_udp_error (F);
957 #undef F
958
959 static char *bfd_udp_error_strings[] = {
960 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
961   foreach_bfd_udp_error (F)
962 #undef F
963 };
964
965 typedef enum
966 {
967 #define F(sym, str) BFD_UDP_ERROR_##sym,
968   foreach_bfd_udp_error (F)
969 #undef F
970     BFD_UDP_N_ERROR,
971 } bfd_udp_error_t;
972
973 typedef enum
974 {
975   BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
976   BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
977   BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
978   BFD_UDP_ECHO_INPUT_N_NEXT,
979 } bfd_udp_echo_input_next_t;
980
981 /* Packet counters - BFD ECHO packets */
982 #define foreach_bfd_udp_echo_error(F)           \
983   F (NONE, "good bfd echo packets (processed)") \
984   F (BAD, "invalid bfd echo packets")
985
986 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
987 foreach_bfd_udp_echo_error (F);
988 #undef F
989
990 static char *bfd_udp_echo_error_strings[] = {
991 #define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
992   foreach_bfd_udp_echo_error (F)
993 #undef F
994 };
995
996 typedef enum
997 {
998 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
999   foreach_bfd_udp_echo_error (F)
1000 #undef F
1001     BFD_UDP_ECHO_N_ERROR,
1002 } bfd_udp_echo_error_t;
1003
1004 static void
1005 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
1006                        udp_header_t ** udp)
1007 {
1008   /* sanity check first */
1009   const i32 start = vnet_buffer (b)->l3_hdr_offset;
1010   if (start < -(signed) sizeof (b->pre_data))
1011     {
1012       BFD_ERR ("Start of ip header is before pre_data, ignoring");
1013       *ip4 = NULL;
1014       *udp = NULL;
1015       return;
1016     }
1017   *ip4 = (ip4_header_t *) (b->data + start);
1018   if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
1019     {
1020       BFD_ERR ("Start of ip header is beyond current data, ignoring");
1021       *ip4 = NULL;
1022       *udp = NULL;
1023       return;
1024     }
1025   *udp = (udp_header_t *) ((*ip4) + 1);
1026 }
1027
1028 static bfd_udp_error_t
1029 bfd_udp4_verify_transport (const ip4_header_t * ip4,
1030                            const udp_header_t * udp, const bfd_session_t * bs)
1031 {
1032   const bfd_udp_session_t *bus = &bs->udp;
1033   const bfd_udp_key_t *key = &bus->key;
1034   if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
1035     {
1036       BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
1037                format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
1038                key->peer_addr.ip4.as_u8);
1039       return BFD_UDP_ERROR_BAD;
1040     }
1041   if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
1042     {
1043       BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
1044                format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
1045                key->local_addr.ip4.as_u8);
1046       return BFD_UDP_ERROR_BAD;
1047     }
1048   const u8 expected_ttl = 255;
1049   if (ip4->ttl != expected_ttl)
1050     {
1051       BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
1052                expected_ttl);
1053       return BFD_UDP_ERROR_BAD;
1054     }
1055   if (clib_net_to_host_u16 (udp->src_port) < 49152)
1056     {
1057       BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1058                udp->src_port);
1059     }
1060   return BFD_UDP_ERROR_NONE;
1061 }
1062
1063 typedef struct
1064 {
1065   u32 bs_idx;
1066   bfd_pkt_t pkt;
1067 } bfd_rpc_update_t;
1068
1069 static void
1070 bfd_rpc_update_session (vlib_main_t * vm, u32 bs_idx, const bfd_pkt_t * pkt)
1071 {
1072   bfd_main_t *bm = &bfd_main;
1073   bfd_lock (bm);
1074   bfd_consume_pkt (vm, bm, pkt, bs_idx);
1075   bfd_unlock (bm);
1076 }
1077
1078 static bfd_udp_error_t
1079 bfd_udp4_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1080 {
1081   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1082   if (sizeof (*pkt) > b->current_length)
1083     {
1084       BFD_ERR
1085         ("Payload size %d too small to hold bfd packet of minimum size %d",
1086          b->current_length, sizeof (*pkt));
1087       return BFD_UDP_ERROR_BAD;
1088     }
1089   ip4_header_t *ip4;
1090   udp_header_t *udp;
1091   bfd_udp4_find_headers (b, &ip4, &udp);
1092   if (!ip4 || !udp)
1093     {
1094       BFD_ERR ("Couldn't find ip4 or udp header");
1095       return BFD_UDP_ERROR_BAD;
1096     }
1097   const u32 udp_payload_length = udp->length - sizeof (*udp);
1098   if (pkt->head.length > udp_payload_length)
1099     {
1100       BFD_ERR
1101         ("BFD packet length is larger than udp payload length (%u > %u)",
1102          pkt->head.length, udp_payload_length);
1103       return BFD_UDP_ERROR_BAD;
1104     }
1105   if (!bfd_verify_pkt_common (pkt))
1106     {
1107       return BFD_UDP_ERROR_BAD;
1108     }
1109   bfd_session_t *bs = NULL;
1110   if (pkt->your_disc)
1111     {
1112       BFD_DBG ("Looking up BFD session using discriminator %u",
1113                pkt->your_disc);
1114       bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1115     }
1116   else
1117     {
1118       bfd_udp_key_t key;
1119       clib_memset (&key, 0, sizeof (key));
1120       key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1121       key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1122       key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1123       BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1124                "peer=%U)",
1125                key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1126                format_ip4_address, key.peer_addr.ip4.as_u8);
1127       bs = bfd_lookup_session (&bfd_udp_main, &key);
1128     }
1129   if (!bs)
1130     {
1131       BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1132       return BFD_UDP_ERROR_BAD;
1133     }
1134   BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1135   if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1136     {
1137       BFD_ERR ("Packet verification failed, dropping packet");
1138       return BFD_UDP_ERROR_BAD;
1139     }
1140   bfd_udp_error_t err;
1141   if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1142     {
1143       return err;
1144     }
1145   bfd_rpc_update_session (vm, bs->bs_idx, pkt);
1146   *bs_out = bs;
1147   return BFD_UDP_ERROR_NONE;
1148 }
1149
1150 static void
1151 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1152                        udp_header_t ** udp)
1153 {
1154   /* sanity check first */
1155   const i32 start = vnet_buffer (b)->l3_hdr_offset;
1156   if (start < -(signed) sizeof (b->pre_data))
1157     {
1158       BFD_ERR ("Start of ip header is before pre_data, ignoring");
1159       *ip6 = NULL;
1160       *udp = NULL;
1161       return;
1162     }
1163   *ip6 = (ip6_header_t *) (b->data + start);
1164   if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1165     {
1166       BFD_ERR ("Start of ip header is beyond current data, ignoring");
1167       *ip6 = NULL;
1168       *udp = NULL;
1169       return;
1170     }
1171   if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1172     {
1173       BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1174                "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1175       *ip6 = NULL;
1176       *udp = NULL;
1177       return;
1178     }
1179   *udp = (udp_header_t *) ((*ip6) + 1);
1180 }
1181
1182 static bfd_udp_error_t
1183 bfd_udp6_verify_transport (const ip6_header_t * ip6,
1184                            const udp_header_t * udp, const bfd_session_t * bs)
1185 {
1186   const bfd_udp_session_t *bus = &bs->udp;
1187   const bfd_udp_key_t *key = &bus->key;
1188   if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1189       ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1190     {
1191       BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1192                format_ip6_address, ip6, format_ip6_address,
1193                &key->peer_addr.ip6);
1194       return BFD_UDP_ERROR_BAD;
1195     }
1196   if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1197       ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1198     {
1199       BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1200                format_ip6_address, ip6, format_ip6_address,
1201                &key->local_addr.ip6);
1202       return BFD_UDP_ERROR_BAD;
1203     }
1204   const u8 expected_hop_limit = 255;
1205   if (ip6->hop_limit != expected_hop_limit)
1206     {
1207       BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1208                ip6->hop_limit, expected_hop_limit);
1209       return BFD_UDP_ERROR_BAD;
1210     }
1211   if (clib_net_to_host_u16 (udp->src_port) < 49152)
1212     {
1213       BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1214                udp->src_port);
1215     }
1216   return BFD_UDP_ERROR_NONE;
1217 }
1218
1219 static bfd_udp_error_t
1220 bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
1221 {
1222   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1223   if (sizeof (*pkt) > b->current_length)
1224     {
1225       BFD_ERR
1226         ("Payload size %d too small to hold bfd packet of minimum size %d",
1227          b->current_length, sizeof (*pkt));
1228       return BFD_UDP_ERROR_BAD;
1229     }
1230   ip6_header_t *ip6;
1231   udp_header_t *udp;
1232   bfd_udp6_find_headers (b, &ip6, &udp);
1233   if (!ip6 || !udp)
1234     {
1235       BFD_ERR ("Couldn't find ip6 or udp header");
1236       return BFD_UDP_ERROR_BAD;
1237     }
1238   const u32 udp_payload_length = udp->length - sizeof (*udp);
1239   if (pkt->head.length > udp_payload_length)
1240     {
1241       BFD_ERR
1242         ("BFD packet length is larger than udp payload length (%u > %u)",
1243          pkt->head.length, udp_payload_length);
1244       return BFD_UDP_ERROR_BAD;
1245     }
1246   if (!bfd_verify_pkt_common (pkt))
1247     {
1248       return BFD_UDP_ERROR_BAD;
1249     }
1250   bfd_session_t *bs = NULL;
1251   if (pkt->your_disc)
1252     {
1253       BFD_DBG ("Looking up BFD session using discriminator %u",
1254                pkt->your_disc);
1255       bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1256     }
1257   else
1258     {
1259       bfd_udp_key_t key;
1260       clib_memset (&key, 0, sizeof (key));
1261       key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1262       key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1263       key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1264       key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1265       key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1266       BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1267                "peer=%U)",
1268                key.sw_if_index, format_ip6_address, &key.local_addr,
1269                format_ip6_address, &key.peer_addr);
1270       bs = bfd_lookup_session (&bfd_udp_main, &key);
1271     }
1272   if (!bs)
1273     {
1274       BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1275       return BFD_UDP_ERROR_BAD;
1276     }
1277   BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1278   if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
1279     {
1280       BFD_ERR ("Packet verification failed, dropping packet");
1281       return BFD_UDP_ERROR_BAD;
1282     }
1283   bfd_udp_error_t err;
1284   if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1285     {
1286       return err;
1287     }
1288   bfd_rpc_update_session (vm, bs->bs_idx, pkt);
1289   *bs_out = bs;
1290   return BFD_UDP_ERROR_NONE;
1291 }
1292
1293 /*
1294  * Process a frame of bfd packets
1295  * Expect 1 packet / frame
1296  */
1297 static uword
1298 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1299                vlib_frame_t * f, int is_ipv6)
1300 {
1301   u32 n_left_from, *from;
1302   bfd_input_trace_t *t0;
1303   bfd_main_t *bm = &bfd_main;
1304
1305   from = vlib_frame_vector_args (f);    /* array of buffer indices */
1306   n_left_from = f->n_vectors;   /* number of buffer indices */
1307
1308   while (n_left_from > 0)
1309     {
1310       u32 bi0;
1311       vlib_buffer_t *b0;
1312       u32 next0, error0;
1313
1314       bi0 = from[0];
1315       b0 = vlib_get_buffer (vm, bi0);
1316
1317       bfd_session_t *bs = NULL;
1318
1319       /* If this pkt is traced, snapshot the data */
1320       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1321         {
1322           u64 len;
1323           t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1324           len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1325             : sizeof (t0->data);
1326           t0->len = len;
1327           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1328         }
1329
1330       /* scan this bfd pkt. error0 is the counter index to bmp */
1331       bfd_lock (bm);
1332       if (is_ipv6)
1333         {
1334           error0 = bfd_udp6_scan (vm, b0, &bs);
1335         }
1336       else
1337         {
1338           error0 = bfd_udp4_scan (vm, b0, &bs);
1339         }
1340       b0->error = rt->errors[error0];
1341
1342       next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1343       if (BFD_UDP_ERROR_NONE == error0)
1344         {
1345           /*
1346            *  if everything went fine, check for poll bit, if present, re-use
1347            *  the buffer and based on (now updated) session parameters, send
1348            *  the final packet back
1349            */
1350           const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1351           if (bfd_pkt_get_poll (pkt))
1352             {
1353               b0->current_data = 0;
1354               b0->current_length = 0;
1355               bfd_init_final_control_frame (vm, b0, bs);
1356               if (is_ipv6)
1357                 {
1358                   vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1359                                                b0->error, 1);
1360                 }
1361               else
1362                 {
1363                   vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1364                                                b0->error, 1);
1365                 }
1366               const bfd_udp_session_t *bus = &bs->udp;
1367               ip_adjacency_t *adj = adj_get (bus->adj_index);
1368               switch (adj->lookup_next_index)
1369                 {
1370                 case IP_LOOKUP_NEXT_ARP:
1371                   next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1372                   break;
1373                 case IP_LOOKUP_NEXT_REWRITE:
1374                   next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1375                   break;
1376                 case IP_LOOKUP_NEXT_MIDCHAIN:
1377                   next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1378                   break;
1379                 default:
1380                   /* drop */
1381                   break;
1382                 }
1383             }
1384         }
1385       bfd_unlock (bm);
1386       vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1387
1388       from += 1;
1389       n_left_from -= 1;
1390     }
1391
1392   return f->n_vectors;
1393 }
1394
1395 static uword
1396 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1397 {
1398   return bfd_udp_input (vm, rt, f, 0);
1399 }
1400
1401 /*
1402  * bfd input graph node declaration
1403  */
1404 /* *INDENT-OFF* */
1405 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1406   .function = bfd_udp4_input,
1407   .name = "bfd-udp4-input",
1408   .vector_size = sizeof (u32),
1409   .type = VLIB_NODE_TYPE_INTERNAL,
1410
1411   .n_errors = BFD_UDP_N_ERROR,
1412   .error_strings = bfd_udp_error_strings,
1413
1414   .format_trace = bfd_input_format_trace,
1415
1416   .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1417   .next_nodes =
1418       {
1419               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1420               [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1421               [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1422               [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1423       },
1424 };
1425 /* *INDENT-ON* */
1426
1427 static uword
1428 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1429 {
1430   return bfd_udp_input (vm, rt, f, 1);
1431 }
1432
1433 /* *INDENT-OFF* */
1434 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1435   .function = bfd_udp6_input,
1436   .name = "bfd-udp6-input",
1437   .vector_size = sizeof (u32),
1438   .type = VLIB_NODE_TYPE_INTERNAL,
1439
1440   .n_errors = BFD_UDP_N_ERROR,
1441   .error_strings = bfd_udp_error_strings,
1442
1443   .format_trace = bfd_input_format_trace,
1444
1445   .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1446   .next_nodes =
1447       {
1448               [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1449               [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1450               [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1451               [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1452       },
1453 };
1454 /* *INDENT-ON* */
1455
1456 /*
1457  * Process a frame of bfd echo packets
1458  * Expect 1 packet / frame
1459  */
1460 static uword
1461 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1462                     vlib_frame_t * f, int is_ipv6)
1463 {
1464   u32 n_left_from, *from;
1465   bfd_input_trace_t *t0;
1466   bfd_main_t *bm = &bfd_main;
1467
1468   from = vlib_frame_vector_args (f);    /* array of buffer indices */
1469   n_left_from = f->n_vectors;   /* number of buffer indices */
1470
1471   while (n_left_from > 0)
1472     {
1473       u32 bi0;
1474       vlib_buffer_t *b0;
1475       u32 next0;
1476
1477       bi0 = from[0];
1478       b0 = vlib_get_buffer (vm, bi0);
1479
1480       /* If this pkt is traced, snapshot the data */
1481       if (b0->flags & VLIB_BUFFER_IS_TRACED)
1482         {
1483           u64 len;
1484           t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1485           len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1486             : sizeof (t0->data);
1487           t0->len = len;
1488           clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1489         }
1490
1491       bfd_lock (bm);
1492       if (bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0))
1493         {
1494           b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1495           next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1496         }
1497       else
1498         {
1499           /* loop back the packet */
1500           b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1501           if (is_ipv6)
1502             {
1503               vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1504                                            b0->error, 1);
1505             }
1506           else
1507             {
1508               vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1509                                            b0->error, 1);
1510             }
1511           next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1512         }
1513
1514       bfd_unlock (bm);
1515       vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1516
1517       from += 1;
1518       n_left_from -= 1;
1519     }
1520
1521   return f->n_vectors;
1522 }
1523
1524 static uword
1525 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1526                      vlib_frame_t * f)
1527 {
1528   return bfd_udp_echo_input (vm, rt, f, 0);
1529 }
1530
1531 u8 *
1532 bfd_echo_input_format_trace (u8 * s, va_list * args)
1533 {
1534   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1535   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1536   const bfd_udp_echo_input_trace_t *t =
1537     va_arg (*args, bfd_udp_echo_input_trace_t *);
1538   if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1539     {
1540       s = format (s, "BFD ECHO:\n");
1541       s = format (s, "    data: %U", format_hexdump, t->data, t->len);
1542     }
1543
1544   return s;
1545 }
1546
1547 /*
1548  * bfd input graph node declaration
1549  */
1550 /* *INDENT-OFF* */
1551 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1552   .function = bfd_udp_echo4_input,
1553   .name = "bfd-udp-echo4-input",
1554   .vector_size = sizeof (u32),
1555   .type = VLIB_NODE_TYPE_INTERNAL,
1556
1557   .n_errors = BFD_UDP_ECHO_N_ERROR,
1558   .error_strings = bfd_udp_error_strings,
1559
1560   .format_trace = bfd_echo_input_format_trace,
1561
1562   .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1563   .next_nodes =
1564       {
1565               [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1566               [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1567               [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1568       },
1569 };
1570 /* *INDENT-ON* */
1571
1572 static uword
1573 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1574                      vlib_frame_t * f)
1575 {
1576   return bfd_udp_echo_input (vm, rt, f, 1);
1577 }
1578
1579 /* *INDENT-OFF* */
1580 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1581   .function = bfd_udp_echo6_input,
1582   .name = "bfd-udp-echo6-input",
1583   .vector_size = sizeof (u32),
1584   .type = VLIB_NODE_TYPE_INTERNAL,
1585
1586   .n_errors = BFD_UDP_ECHO_N_ERROR,
1587   .error_strings = bfd_udp_echo_error_strings,
1588
1589   .format_trace = bfd_echo_input_format_trace,
1590
1591   .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1592   .next_nodes =
1593       {
1594               [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1595               [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1596               [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1597       },
1598 };
1599
1600 /* *INDENT-ON* */
1601
1602 static clib_error_t *
1603 bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index,
1604                        u32 is_create)
1605 {
1606   u32 *to_be_freed = NULL;
1607   bfd_udp_main_t *bum = &bfd_udp_main;
1608   BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1609            is_create);
1610   if (!is_create)
1611     {
1612       bfd_session_t *bs;
1613       pool_foreach (bs, bum->bfd_main->sessions)
1614         {
1615           if (bs->transport != BFD_TRANSPORT_UDP4 &&
1616               bs->transport != BFD_TRANSPORT_UDP6)
1617             {
1618               continue;
1619             }
1620           if (bs->udp.key.sw_if_index != sw_if_index)
1621             {
1622               continue;
1623             }
1624           vec_add1 (to_be_freed, bs->bs_idx);
1625         }
1626     }
1627   u32 *bs_idx;
1628   vec_foreach (bs_idx, to_be_freed)
1629     {
1630       bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx);
1631       vlib_log_notice (bum->log_class,
1632                        "removal of sw_if_index=%u forces removal of bfd "
1633                        "session with bs_idx=%u",
1634                        sw_if_index, bs->bs_idx);
1635       bfd_session_set_flags (vlib_get_main (), bs, 0);
1636       bfd_udp_del_session_internal (vlib_get_main (), bs);
1637     }
1638   return 0;
1639 }
1640
1641 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1642
1643 /*
1644  * setup function
1645  */
1646 static clib_error_t *
1647 bfd_udp_init (vlib_main_t * vm)
1648 {
1649   bfd_udp_main.udp4_sessions_count = 0;
1650   bfd_udp_main.udp6_sessions_count = 0;
1651   mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1652               sizeof (bfd_udp_key_t));
1653   bfd_udp_main.bfd_main = &bfd_main;
1654   bfd_udp_main.vnet_main = vnet_get_main ();
1655   vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1656   ASSERT (node);
1657   bfd_udp_main.ip4_arp_idx = node->index;
1658   node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1659   ASSERT (node);
1660   bfd_udp_main.ip6_ndp_idx = node->index;
1661   node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1662   ASSERT (node);
1663   bfd_udp_main.ip4_rewrite_idx = node->index;
1664   node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1665   ASSERT (node);
1666   bfd_udp_main.ip6_rewrite_idx = node->index;
1667   node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
1668   ASSERT (node);
1669   bfd_udp_main.ip4_midchain_idx = node->index;
1670   node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
1671   ASSERT (node);
1672   bfd_udp_main.ip6_midchain_idx = node->index;
1673
1674   bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1675   vlib_log_debug (bfd_udp_main.log_class, "initialized");
1676   return 0;
1677 }
1678
1679 VLIB_INIT_FUNCTION (bfd_udp_init);
1680
1681 /*
1682  * fd.io coding-style-patch-verification: ON
1683  *
1684  * Local Variables:
1685  * eval: (c-set-style "gnu")
1686  * End:
1687  */