ip: use IPv6 flowlabel in flow hash computation
[vpp.git] / src / vnet / ip / ip_api.c
1 /*
2  *------------------------------------------------------------------
3  * ip_api.c - vnet ip api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/ethernet/ethernet_types_api.h>
27 #include <vnet/ip/ip.h>
28 #include <vnet/ip/ip_types_api.h>
29 #include <vnet/ip/ip_punt_drop.h>
30 #include <vnet/ip/ip_types_api.h>
31 #include <vnet/fib/fib_table.h>
32 #include <vnet/fib/fib_api.h>
33 #include <vnet/ethernet/arp_packet.h>
34 #include <vnet/mfib/ip6_mfib.h>
35 #include <vnet/mfib/ip4_mfib.h>
36 #include <vnet/mfib/mfib_signal.h>
37 #include <vnet/mfib/mfib_entry.h>
38 #include <vnet/mfib/mfib_api.h>
39 #include <vnet/ip/ip_source_and_port_range_check.h>
40 #include <vnet/fib/fib_path_list.h>
41 #include <vnet/ip/ip6_hop_by_hop.h>
42 #include <vnet/ip/ip6_link.h>
43 #include <vnet/ip/reass/ip4_sv_reass.h>
44 #include <vnet/ip/reass/ip4_full_reass.h>
45 #include <vnet/ip/reass/ip6_sv_reass.h>
46 #include <vnet/ip/reass/ip6_full_reass.h>
47 #include <vnet/ip/ip_table.h>
48 #include <vnet/ip/ip_container_proxy.h>
49
50 #include <vnet/vnet_msg_enum.h>
51
52 #define vl_typedefs             /* define message structures */
53 #include <vnet/vnet_all_api_h.h>
54 #undef vl_typedefs
55
56 #define vl_endianfun            /* define message structures */
57 #include <vnet/vnet_all_api_h.h>
58 #undef vl_endianfun
59
60 /* instantiate all the print functions we know about */
61 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
62 #define vl_printfun
63 #include <vnet/vnet_all_api_h.h>
64 #undef vl_printfun
65
66 #include <vlibapi/api_helper_macros.h>
67
68 #include <vnet/format_fns.h>
69
70 #define foreach_ip_api_msg                                                    \
71   _ (SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable)        \
72   _ (IP_TABLE_DUMP, ip_table_dump)                                            \
73   _ (IP_ROUTE_DUMP, ip_route_dump)                                            \
74   _ (IP_MTABLE_DUMP, ip_mtable_dump)                                          \
75   _ (IP_MROUTE_DUMP, ip_mroute_dump)                                          \
76   _ (IP_MROUTE_ADD_DEL, ip_mroute_add_del)                                    \
77   _ (MFIB_SIGNAL_DUMP, mfib_signal_dump)                                      \
78   _ (IP_ADDRESS_DUMP, ip_address_dump)                                        \
79   _ (IP_UNNUMBERED_DUMP, ip_unnumbered_dump)                                  \
80   _ (IP_DUMP, ip_dump)                                                        \
81   _ (IP_TABLE_REPLACE_BEGIN, ip_table_replace_begin)                          \
82   _ (IP_TABLE_REPLACE_END, ip_table_replace_end)                              \
83   _ (IP_TABLE_FLUSH, ip_table_flush)                                          \
84   _ (IP_ROUTE_ADD_DEL, ip_route_add_del)                                      \
85   _ (IP_ROUTE_LOOKUP, ip_route_lookup)                                        \
86   _ (IP_TABLE_ADD_DEL, ip_table_add_del)                                      \
87   _ (IP_PUNT_POLICE, ip_punt_police)                                          \
88   _ (IP_PUNT_REDIRECT, ip_punt_redirect)                                      \
89   _ (SET_IP_FLOW_HASH, set_ip_flow_hash)                                      \
90   _ (SET_IP_FLOW_HASH_V2, set_ip_flow_hash_v2)                                \
91   _ (IP_CONTAINER_PROXY_ADD_DEL, ip_container_proxy_add_del)                  \
92   _ (IP_CONTAINER_PROXY_DUMP, ip_container_proxy_dump)                        \
93   _ (IOAM_ENABLE, ioam_enable)                                                \
94   _ (IOAM_DISABLE, ioam_disable)                                              \
95   _ (IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL,                                  \
96      ip_source_and_port_range_check_add_del)                                  \
97   _ (IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL,                        \
98      ip_source_and_port_range_check_interface_add_del)                        \
99   _ (SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS,                                 \
100      sw_interface_ip6_set_link_local_address)                                 \
101   _ (SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS,                                 \
102      sw_interface_ip6_get_link_local_address)                                 \
103   _ (IP_REASSEMBLY_SET, ip_reassembly_set)                                    \
104   _ (IP_REASSEMBLY_GET, ip_reassembly_get)                                    \
105   _ (IP_REASSEMBLY_ENABLE_DISABLE, ip_reassembly_enable_disable)              \
106   _ (IP_PUNT_REDIRECT_DUMP, ip_punt_redirect_dump)
107
108 static void
109   vl_api_sw_interface_ip6_enable_disable_t_handler
110   (vl_api_sw_interface_ip6_enable_disable_t * mp)
111 {
112   vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
113   int rv = 0;
114
115   VALIDATE_SW_IF_INDEX (mp);
116
117   rv = ((mp->enable == 1) ?
118         ip6_link_enable (ntohl (mp->sw_if_index), NULL) :
119         ip6_link_disable (ntohl (mp->sw_if_index)));
120
121   BAD_SW_IF_INDEX_LABEL;
122
123   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
124 }
125
126 static void
127 send_ip_table_details (vpe_api_main_t * am,
128                        vl_api_registration_t * reg,
129                        u32 context, const fib_table_t * table)
130 {
131   vl_api_ip_table_details_t *mp;
132
133   mp = vl_msg_api_alloc (sizeof (*mp));
134   if (!mp)
135     return;
136   clib_memset (mp, 0, sizeof (*mp));
137   mp->_vl_msg_id = ntohs (VL_API_IP_TABLE_DETAILS);
138   mp->context = context;
139
140   mp->table.is_ip6 = (table->ft_proto == FIB_PROTOCOL_IP6);
141   mp->table.table_id = htonl (table->ft_table_id);
142   memcpy (mp->table.name, table->ft_desc,
143           clib_min (vec_len (table->ft_desc), sizeof (mp->table.name)));
144
145   vl_api_send_msg (reg, (u8 *) mp);
146 }
147
148 static void
149 vl_api_ip_table_dump_t_handler (vl_api_ip_table_dump_t * mp)
150 {
151   vpe_api_main_t *am = &vpe_api_main;
152   vl_api_registration_t *reg;
153   fib_table_t *fib_table;
154
155   reg = vl_api_client_index_to_registration (mp->client_index);
156   if (!reg)
157     return;
158
159   /* *INDENT-OFF* */
160   pool_foreach (fib_table, ip4_main.fibs)
161    {
162     send_ip_table_details(am, reg, mp->context, fib_table);
163   }
164   pool_foreach (fib_table, ip6_main.fibs)
165    {
166     /* don't send link locals */
167     if (fib_table->ft_flags & FIB_TABLE_FLAG_IP6_LL)
168       continue;
169     send_ip_table_details(am, reg, mp->context, fib_table);
170   }
171   /* *INDENT-ON* */
172 }
173
174 typedef struct vl_api_ip_fib_dump_walk_ctx_t_
175 {
176   fib_node_index_t *feis;
177 } vl_api_ip_fib_dump_walk_ctx_t;
178
179 static fib_table_walk_rc_t
180 vl_api_ip_fib_dump_walk (fib_node_index_t fei, void *arg)
181 {
182   vl_api_ip_fib_dump_walk_ctx_t *ctx = arg;
183
184   vec_add1 (ctx->feis, fei);
185
186   return (FIB_TABLE_WALK_CONTINUE);
187 }
188
189 static void
190 send_ip_route_details (vpe_api_main_t * am,
191                        vl_api_registration_t * reg,
192                        u32 context, fib_node_index_t fib_entry_index)
193 {
194   fib_route_path_t *rpaths, *rpath;
195   vl_api_ip_route_details_t *mp;
196   const fib_prefix_t *pfx;
197   vl_api_fib_path_t *fp;
198   int path_count;
199
200   rpaths = NULL;
201   pfx = fib_entry_get_prefix (fib_entry_index);
202   rpaths = fib_entry_encode (fib_entry_index);
203
204   path_count = vec_len (rpaths);
205   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
206   if (!mp)
207     return;
208   clib_memset (mp, 0, sizeof (*mp));
209   mp->_vl_msg_id = ntohs (VL_API_IP_ROUTE_DETAILS);
210   mp->context = context;
211
212   ip_prefix_encode (pfx, &mp->route.prefix);
213   mp->route.table_id =
214     htonl (fib_table_get_table_id
215            (fib_entry_get_fib_index (fib_entry_index), pfx->fp_proto));
216   mp->route.n_paths = path_count;
217   mp->route.stats_index =
218     htonl (fib_table_entry_get_stats_index
219            (fib_entry_get_fib_index (fib_entry_index), pfx));
220
221   fp = mp->route.paths;
222   vec_foreach (rpath, rpaths)
223   {
224     fib_api_path_encode (rpath, fp);
225     fp++;
226   }
227
228   vl_api_send_msg (reg, (u8 *) mp);
229   vec_free (rpaths);
230 }
231
232 typedef struct apt_ip6_fib_show_ctx_t_
233 {
234   fib_node_index_t *entries;
235 } api_ip6_fib_show_ctx_t;
236
237 static void
238 vl_api_ip_route_dump_t_handler (vl_api_ip_route_dump_t * mp)
239 {
240   vpe_api_main_t *am = &vpe_api_main;
241   fib_node_index_t *fib_entry_index;
242   vl_api_registration_t *reg;
243   fib_protocol_t fproto;
244   u32 fib_index;
245
246   reg = vl_api_client_index_to_registration (mp->client_index);
247   if (!reg)
248     return;
249
250   vl_api_ip_fib_dump_walk_ctx_t ctx = {
251     .feis = NULL,
252   };
253
254   fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
255   fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
256
257   if (INDEX_INVALID == fib_index)
258     return;
259
260   fib_table_walk (fib_index, fproto, vl_api_ip_fib_dump_walk, &ctx);
261
262   vec_foreach (fib_entry_index, ctx.feis)
263   {
264     send_ip_route_details (am, reg, mp->context, *fib_entry_index);
265   }
266
267   vec_free (ctx.feis);
268 }
269
270 static void
271 send_ip_mtable_details (vl_api_registration_t * reg,
272                         u32 context, const mfib_table_t * mfib_table)
273 {
274   vl_api_ip_mtable_details_t *mp;
275
276   mp = vl_msg_api_alloc (sizeof (*mp));
277   if (!mp)
278     return;
279   memset (mp, 0, sizeof (*mp));
280   mp->_vl_msg_id = ntohs (VL_API_IP_MTABLE_DETAILS);
281   mp->context = context;
282
283   mp->table.table_id = htonl (mfib_table->mft_table_id);
284   mp->table.is_ip6 = (FIB_PROTOCOL_IP6 == mfib_table->mft_proto);
285
286   vl_api_send_msg (reg, (u8 *) mp);
287 }
288
289 static void
290 vl_api_ip_mtable_dump_t_handler (vl_api_ip_mtable_dump_t * mp)
291 {
292   vl_api_registration_t *reg;
293   mfib_table_t *mfib_table;
294
295   reg = vl_api_client_index_to_registration (mp->client_index);
296   if (!reg)
297     return;
298
299   /* *INDENT-OFF* */
300   pool_foreach (mfib_table, ip4_main.mfibs)
301    {
302       send_ip_mtable_details (reg, mp->context, mfib_table);
303   }
304   pool_foreach (mfib_table, ip6_main.mfibs)
305    {
306       send_ip_mtable_details (reg, mp->context, mfib_table);
307   }
308   /* *INDENT-ON* */
309 }
310
311 typedef struct vl_api_ip_mfib_dump_ctx_t_
312 {
313   fib_node_index_t *entries;
314 } vl_api_ip_mfib_dump_ctx_t;
315
316 static walk_rc_t
317 mfib_route_dump_walk (fib_node_index_t fei, void *arg)
318 {
319   vl_api_ip_mfib_dump_ctx_t *ctx = arg;
320
321   vec_add1 (ctx->entries, fei);
322
323   return (WALK_CONTINUE);
324 }
325
326 static void
327 send_ip_mroute_details (vpe_api_main_t * am,
328                         vl_api_registration_t * reg,
329                         u32 context, fib_node_index_t mfib_entry_index)
330 {
331   fib_route_path_t *rpaths, *rpath;
332   vl_api_ip_mroute_details_t *mp;
333   const mfib_prefix_t *pfx;
334   vl_api_mfib_path_t *fp;
335   u8 path_count;
336
337   rpaths = NULL;
338   pfx = mfib_entry_get_prefix (mfib_entry_index);
339   rpaths = mfib_entry_encode (mfib_entry_index);
340
341   path_count = vec_len (rpaths);
342   mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
343   if (!mp)
344     return;
345   clib_memset (mp, 0, sizeof (*mp));
346   mp->_vl_msg_id = ntohs (VL_API_IP_MROUTE_DETAILS);
347   mp->context = context;
348
349   ip_mprefix_encode (pfx, &mp->route.prefix);
350   mp->route.table_id =
351     htonl (mfib_table_get_table_id
352            (mfib_entry_get_fib_index (mfib_entry_index), pfx->fp_proto));
353   mp->route.n_paths = path_count;
354   fp = mp->route.paths;
355   vec_foreach (rpath, rpaths)
356   {
357     mfib_api_path_encode (rpath, fp);
358     fp++;
359   }
360
361   vl_api_send_msg (reg, (u8 *) mp);
362   vec_free (rpaths);
363 }
364
365 static void
366 vl_api_ip_mroute_dump_t_handler (vl_api_ip_mroute_dump_t * mp)
367 {
368   vpe_api_main_t *am = &vpe_api_main;
369   vl_api_registration_t *reg;
370   fib_node_index_t *mfeip;
371   fib_protocol_t fproto;
372   u32 fib_index;
373
374   vl_api_ip_mfib_dump_ctx_t ctx = {
375     .entries = NULL,
376   };
377
378   reg = vl_api_client_index_to_registration (mp->client_index);
379   if (!reg)
380     return;
381
382   fproto = fib_ip_proto (mp->table.is_ip6);
383   fib_index = mfib_table_find (fproto, ntohl (mp->table.table_id));
384
385   if (INDEX_INVALID == fib_index)
386     return;
387
388   mfib_table_walk (fib_index, fproto, mfib_route_dump_walk, &ctx);
389
390   vec_sort_with_function (ctx.entries, mfib_entry_cmp_for_sort);
391
392   vec_foreach (mfeip, ctx.entries)
393   {
394     send_ip_mroute_details (am, reg, mp->context, *mfeip);
395   }
396
397   vec_free (ctx.entries);
398 }
399
400 static void
401 vl_api_ip_punt_police_t_handler (vl_api_ip_punt_police_t * mp,
402                                  vlib_main_t * vm)
403 {
404   vl_api_ip_punt_police_reply_t *rmp;
405   int rv = 0;
406
407   if (mp->is_ip6)
408     ip6_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
409   else
410     ip4_punt_policer_add_del (mp->is_add, ntohl (mp->policer_index));
411
412   REPLY_MACRO (VL_API_IP_PUNT_POLICE_REPLY);
413 }
414
415 static void
416 vl_api_ip_punt_redirect_t_handler (vl_api_ip_punt_redirect_t * mp,
417                                    vlib_main_t * vm)
418 {
419   vl_api_ip_punt_redirect_reply_t *rmp;
420   int rv = 0;
421   ip46_type_t ipv;
422   ip46_address_t nh;
423
424   if (!vnet_sw_if_index_is_api_valid (ntohl (mp->punt.tx_sw_if_index)))
425     goto bad_sw_if_index;
426
427   ipv = ip_address_decode (&mp->punt.nh, &nh);
428   if (mp->is_add)
429     {
430       if (ipv == IP46_TYPE_IP6)
431         {
432           ip6_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
433                                  ntohl (mp->punt.tx_sw_if_index), &nh);
434         }
435       else if (ipv == IP46_TYPE_IP4)
436         {
437           ip4_punt_redirect_add (ntohl (mp->punt.rx_sw_if_index),
438                                  ntohl (mp->punt.tx_sw_if_index), &nh);
439         }
440     }
441   else
442     {
443       if (ipv == IP46_TYPE_IP6)
444         {
445           ip6_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
446         }
447       else if (ipv == IP46_TYPE_IP4)
448         {
449           ip4_punt_redirect_del (ntohl (mp->punt.rx_sw_if_index));
450         }
451     }
452
453   BAD_SW_IF_INDEX_LABEL;
454
455   REPLY_MACRO (VL_API_IP_PUNT_REDIRECT_REPLY);
456 }
457
458 static clib_error_t *
459 call_elf_section_ip_table_callbacks (vnet_main_t * vnm, u32 table_id,
460                                      u32 flags,
461                                      _vnet_ip_table_function_list_elt_t **
462                                      elts)
463 {
464   _vnet_ip_table_function_list_elt_t *elt;
465   vnet_ip_table_function_priority_t prio;
466   clib_error_t *error = 0;
467
468   for (prio = VNET_IP_TABLE_FUNC_PRIORITY_LOW;
469        prio <= VNET_IP_TABLE_FUNC_PRIORITY_HIGH; prio++)
470     {
471       elt = elts[prio];
472
473       while (elt)
474         {
475           error = elt->fp (vnm, table_id, flags);
476           if (error)
477             return error;
478           elt = elt->next_ip_table_function;
479         }
480     }
481   return error;
482 }
483
484 void
485 ip_table_delete (fib_protocol_t fproto, u32 table_id, u8 is_api)
486 {
487   u32 fib_index, mfib_index;
488   vnet_main_t *vnm = vnet_get_main ();
489
490   /*
491    * ignore action on the default table - this is always present
492    * and cannot be added nor deleted from the API
493    */
494   if (0 != table_id)
495     {
496       /*
497        * The API holds only one lock on the table.
498        * i.e. it can be added many times via the API but needs to be
499        * deleted only once.
500        * The FIB index for unicast and multicast is not necessarily the
501        * same, since internal VPP systesm (like LISP and SR) create
502        * their own unicast tables.
503        */
504       fib_index = fib_table_find (fproto, table_id);
505       mfib_index = mfib_table_find (fproto, table_id);
506
507       if ((~0 != fib_index) || (~0 != mfib_index))
508         call_elf_section_ip_table_callbacks (vnm, table_id, 0 /* is_add */ ,
509                                              vnm->ip_table_add_del_functions);
510
511       if (~0 != fib_index)
512         {
513           fib_table_unlock (fib_index, fproto,
514                             (is_api ? FIB_SOURCE_API : FIB_SOURCE_CLI));
515         }
516       if (~0 != mfib_index)
517         {
518           mfib_table_unlock (mfib_index, fproto,
519                              (is_api ? MFIB_SOURCE_API : MFIB_SOURCE_CLI));
520         }
521     }
522 }
523
524 void
525 vl_api_ip_table_add_del_t_handler (vl_api_ip_table_add_del_t * mp)
526 {
527   vl_api_ip_table_add_del_reply_t *rmp;
528   fib_protocol_t fproto = (mp->table.is_ip6 ?
529                            FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
530   u32 table_id = ntohl (mp->table.table_id);
531   int rv = 0;
532
533   if (mp->is_add)
534     {
535       ip_table_create (fproto, table_id, 1, mp->table.name);
536     }
537   else
538     {
539       ip_table_delete (fproto, table_id, 1);
540     }
541
542   REPLY_MACRO (VL_API_IP_TABLE_ADD_DEL_REPLY);
543 }
544
545 static int
546 ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp, u32 * stats_index)
547 {
548   fib_route_path_t *rpaths = NULL, *rpath;
549   fib_entry_flag_t entry_flags;
550   vl_api_fib_path_t *apath;
551   fib_prefix_t pfx;
552   u32 fib_index;
553   int rv, ii;
554
555   entry_flags = FIB_ENTRY_FLAG_NONE;
556   ip_prefix_decode (&mp->route.prefix, &pfx);
557
558   rv = fib_api_table_id_decode (pfx.fp_proto,
559                                 ntohl (mp->route.table_id), &fib_index);
560   if (0 != rv)
561     goto out;
562
563   if (0 != mp->route.n_paths)
564     vec_validate (rpaths, mp->route.n_paths - 1);
565
566   for (ii = 0; ii < mp->route.n_paths; ii++)
567     {
568       apath = &mp->route.paths[ii];
569       rpath = &rpaths[ii];
570
571       rv = fib_api_path_decode (apath, rpath);
572
573       if ((rpath->frp_flags & FIB_ROUTE_PATH_LOCAL) &&
574           (~0 == rpath->frp_sw_if_index))
575         entry_flags |= (FIB_ENTRY_FLAG_CONNECTED | FIB_ENTRY_FLAG_LOCAL);
576
577       if (0 != rv)
578         goto out;
579     }
580
581   rv = fib_api_route_add_del (mp->is_add,
582                               mp->is_multipath,
583                               fib_index, &pfx, entry_flags, rpaths);
584
585   if (mp->is_add && 0 == rv)
586     *stats_index = fib_table_entry_get_stats_index (fib_index, &pfx);
587
588 out:
589   vec_free (rpaths);
590
591   return (rv);
592 }
593
594 void
595 vl_api_ip_route_add_del_t_handler (vl_api_ip_route_add_del_t * mp)
596 {
597   vl_api_ip_route_add_del_reply_t *rmp;
598   u32 stats_index = ~0;
599   int rv;
600
601   rv = ip_route_add_del_t_handler (mp, &stats_index);
602
603   /* *INDENT-OFF* */
604   REPLY_MACRO2 (VL_API_IP_ROUTE_ADD_DEL_REPLY,
605   ({
606     rmp->stats_index = htonl (stats_index);
607   }))
608   /* *INDENT-ON* */
609 }
610
611 void
612 vl_api_ip_route_lookup_t_handler (vl_api_ip_route_lookup_t * mp)
613 {
614   vl_api_ip_route_lookup_reply_t *rmp = NULL;
615   fib_route_path_t *rpaths = NULL, *rpath;
616   const fib_prefix_t *pfx = NULL;
617   fib_prefix_t lookup;
618   vl_api_fib_path_t *fp;
619   fib_node_index_t fib_entry_index;
620   u32 fib_index;
621   int npaths = 0;
622   int rv;
623
624   ip_prefix_decode (&mp->prefix, &lookup);
625   rv = fib_api_table_id_decode (lookup.fp_proto, ntohl (mp->table_id),
626                                 &fib_index);
627   if (PREDICT_TRUE (!rv))
628     {
629       if (mp->exact)
630         fib_entry_index = fib_table_lookup_exact_match (fib_index, &lookup);
631       else
632         fib_entry_index = fib_table_lookup (fib_index, &lookup);
633       if (fib_entry_index == FIB_NODE_INDEX_INVALID)
634         rv = VNET_API_ERROR_NO_SUCH_ENTRY;
635       else
636         {
637           pfx = fib_entry_get_prefix (fib_entry_index);
638           rpaths = fib_entry_encode (fib_entry_index);
639           npaths = vec_len (rpaths);
640         }
641     }
642
643   /* *INDENT-OFF* */
644   REPLY_MACRO3_ZERO(VL_API_IP_ROUTE_LOOKUP_REPLY,
645                     npaths * sizeof (*fp),
646   ({
647     if (!rv)
648       {
649         ip_prefix_encode (pfx, &rmp->route.prefix);
650         rmp->route.table_id = mp->table_id;
651         rmp->route.n_paths = npaths;
652         rmp->route.stats_index = fib_table_entry_get_stats_index (fib_index, pfx);
653         rmp->route.stats_index = htonl (rmp->route.stats_index);
654
655         fp = rmp->route.paths;
656         vec_foreach (rpath, rpaths)
657           {
658             fib_api_path_encode (rpath, fp);
659             fp++;
660           }
661       }
662   }));
663   /* *INDENT-ON* */
664   vec_free (rpaths);
665 }
666
667 void
668 ip_table_create (fib_protocol_t fproto,
669                  u32 table_id, u8 is_api, const u8 * name)
670 {
671   u32 fib_index, mfib_index;
672   vnet_main_t *vnm = vnet_get_main ();
673
674   /*
675    * ignore action on the default table - this is always present
676    * and cannot be added nor deleted from the API
677    */
678   if (0 != table_id)
679     {
680       /*
681        * The API holds only one lock on the table.
682        * i.e. it can be added many times via the API but needs to be
683        * deleted only once.
684        * The FIB index for unicast and multicast is not necessarily the
685        * same, since internal VPP systesm (like LISP and SR) create
686        * their own unicast tables.
687        */
688       fib_index = fib_table_find (fproto, table_id);
689       mfib_index = mfib_table_find (fproto, table_id);
690
691       if (~0 == fib_index)
692         {
693           fib_table_find_or_create_and_lock_w_name (fproto, table_id,
694                                                     (is_api ?
695                                                      FIB_SOURCE_API :
696                                                      FIB_SOURCE_CLI), name);
697         }
698       if (~0 == mfib_index)
699         {
700           mfib_table_find_or_create_and_lock_w_name (fproto, table_id,
701                                                      (is_api ?
702                                                       MFIB_SOURCE_API :
703                                                       MFIB_SOURCE_CLI), name);
704         }
705
706       if ((~0 == fib_index) || (~0 == mfib_index))
707         call_elf_section_ip_table_callbacks (vnm, table_id, 1 /* is_add */ ,
708                                              vnm->ip_table_add_del_functions);
709     }
710 }
711
712 static u32
713 mroute_add_del_handler (u8 is_add,
714                         u8 is_multipath,
715                         u32 fib_index,
716                         const mfib_prefix_t * prefix,
717                         u32 entry_flags,
718                         u32 rpf_id, fib_route_path_t * rpaths)
719 {
720   u32 mfib_entry_index = ~0;
721
722   if (0 == vec_len (rpaths))
723     {
724       mfib_entry_index = mfib_table_entry_update (fib_index, prefix,
725                                                   MFIB_SOURCE_API,
726                                                   rpf_id, entry_flags);
727     }
728   else
729     {
730       if (is_add)
731         {
732           mfib_entry_index =
733             mfib_table_entry_paths_update (fib_index, prefix,
734                                            MFIB_SOURCE_API, rpaths);
735         }
736       else
737         {
738           mfib_table_entry_paths_remove (fib_index, prefix,
739                                          MFIB_SOURCE_API, rpaths);
740         }
741     }
742
743   return (mfib_entry_index);
744 }
745
746 static int
747 api_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp,
748                               u32 * stats_index)
749 {
750   fib_route_path_t *rpath, *rpaths = NULL;
751   fib_node_index_t mfib_entry_index;
752   mfib_entry_flags_t eflags;
753   mfib_prefix_t pfx;
754   u32 fib_index;
755   int rv;
756   u16 ii;
757
758   ip_mprefix_decode (&mp->route.prefix, &pfx);
759
760   rv = mfib_api_table_id_decode (pfx.fp_proto,
761                                  ntohl (mp->route.table_id), &fib_index);
762   if (0 != rv)
763     goto out;
764
765   vec_validate (rpaths, mp->route.n_paths - 1);
766
767   for (ii = 0; ii < mp->route.n_paths; ii++)
768     {
769       rpath = &rpaths[ii];
770
771       rv = mfib_api_path_decode (&mp->route.paths[ii], rpath);
772
773       if (0 != rv)
774         goto out;
775     }
776
777   eflags = mfib_api_path_entry_flags_decode (mp->route.entry_flags);
778   mfib_entry_index = mroute_add_del_handler (mp->is_add,
779                                              mp->is_add,
780                                              fib_index, &pfx,
781                                              eflags,
782                                              ntohl (mp->route.rpf_id),
783                                              rpaths);
784
785   if (~0 != mfib_entry_index)
786     *stats_index = mfib_entry_get_stats_index (mfib_entry_index);
787
788 out:
789   return (rv);
790 }
791
792 void
793 vl_api_ip_mroute_add_del_t_handler (vl_api_ip_mroute_add_del_t * mp)
794 {
795   vl_api_ip_mroute_add_del_reply_t *rmp;
796   u32 stats_index = ~0;
797   int rv;
798
799   rv = api_mroute_add_del_t_handler (mp, &stats_index);
800
801   /* *INDENT-OFF* */
802   REPLY_MACRO2 (VL_API_IP_MROUTE_ADD_DEL_REPLY,
803   ({
804     rmp->stats_index = htonl (stats_index);
805   }));
806   /* *INDENT-ON* */
807 }
808
809 static void
810 send_ip_details (vpe_api_main_t * am,
811                  vl_api_registration_t * reg, u32 sw_if_index, u8 is_ipv6,
812                  u32 context)
813 {
814   vl_api_ip_details_t *mp;
815
816   mp = vl_msg_api_alloc (sizeof (*mp));
817   clib_memset (mp, 0, sizeof (*mp));
818   mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
819
820   mp->sw_if_index = ntohl (sw_if_index);
821   mp->is_ipv6 = is_ipv6;
822   mp->context = context;
823
824   vl_api_send_msg (reg, (u8 *) mp);
825 }
826
827 static void
828 send_ip_address_details (vpe_api_main_t * am,
829                          vl_api_registration_t * reg,
830                          const fib_prefix_t * pfx,
831                          u32 sw_if_index, u32 context)
832 {
833   vl_api_ip_address_details_t *mp;
834
835   mp = vl_msg_api_alloc (sizeof (*mp));
836   clib_memset (mp, 0, sizeof (*mp));
837   mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
838
839   ip_prefix_encode (pfx, &mp->prefix);
840   mp->context = context;
841   mp->sw_if_index = htonl (sw_if_index);
842
843   vl_api_send_msg (reg, (u8 *) mp);
844 }
845
846 static void
847 vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
848 {
849   vpe_api_main_t *am = &vpe_api_main;
850   vl_api_registration_t *reg;
851   ip6_main_t *im6 = &ip6_main;
852   ip4_main_t *im4 = &ip4_main;
853   ip_lookup_main_t *lm6 = &im6->lookup_main;
854   ip_lookup_main_t *lm4 = &im4->lookup_main;
855   ip_interface_address_t *ia = 0;
856   u32 sw_if_index = ~0;
857   int rv __attribute__ ((unused)) = 0;
858
859   VALIDATE_SW_IF_INDEX (mp);
860
861   sw_if_index = ntohl (mp->sw_if_index);
862
863   reg = vl_api_client_index_to_registration (mp->client_index);
864   if (!reg)
865     return;
866
867   if (mp->is_ipv6)
868     {
869       /* *INDENT-OFF* */
870       /* Do not send subnet details of the IP-interface for
871        * unnumbered interfaces. otherwise listening clients
872        * will be confused that the subnet is applied on more
873        * than one interface */
874       foreach_ip_interface_address (lm6, ia, sw_if_index, 0,
875       ({
876         fib_prefix_t pfx = {
877           .fp_addr.ip6 = *(ip6_address_t *)ip_interface_address_get_address (lm6, ia),
878           .fp_len = ia->address_length,
879           .fp_proto = FIB_PROTOCOL_IP6,
880         };
881         send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
882       }));
883       /* *INDENT-ON* */
884     }
885   else
886     {
887       /* *INDENT-OFF* */
888       foreach_ip_interface_address (lm4, ia, sw_if_index, 0,
889       ({
890         fib_prefix_t pfx = {
891           .fp_addr.ip4 = *(ip4_address_t *)ip_interface_address_get_address (lm4, ia),
892           .fp_len = ia->address_length,
893           .fp_proto = FIB_PROTOCOL_IP4,
894         };
895
896         send_ip_address_details(am, reg, &pfx, sw_if_index, mp->context);
897       }));
898       /* *INDENT-ON* */
899     }
900
901   BAD_SW_IF_INDEX_LABEL;
902 }
903
904 static void
905 send_ip_unnumbered_details (vpe_api_main_t * am,
906                             vl_api_registration_t * reg,
907                             u32 sw_if_index, u32 ip_sw_if_index, u32 context)
908 {
909   vl_api_ip_unnumbered_details_t *mp;
910
911   mp = vl_msg_api_alloc (sizeof (*mp));
912   clib_memset (mp, 0, sizeof (*mp));
913   mp->_vl_msg_id = ntohs (VL_API_IP_UNNUMBERED_DETAILS);
914
915   mp->context = context;
916   mp->sw_if_index = htonl (sw_if_index);
917   mp->ip_sw_if_index = htonl (ip_sw_if_index);
918
919   vl_api_send_msg (reg, (u8 *) mp);
920 }
921
922 static void
923 vl_api_ip_unnumbered_dump_t_handler (vl_api_ip_unnumbered_dump_t * mp)
924 {
925   vnet_main_t *vnm = vnet_get_main ();
926   vnet_interface_main_t *im = &vnm->interface_main;
927   int rv __attribute__ ((unused)) = 0;
928   vpe_api_main_t *am = &vpe_api_main;
929   vl_api_registration_t *reg;
930   vnet_sw_interface_t *si;
931   u32 sw_if_index;
932
933   sw_if_index = ntohl (mp->sw_if_index);
934
935   reg = vl_api_client_index_to_registration (mp->client_index);
936   if (!reg)
937     return;
938
939   if (~0 != sw_if_index)
940     {
941       VALIDATE_SW_IF_INDEX (mp);
942
943       si = vnet_get_sw_interface (vnm, ntohl (mp->sw_if_index));
944
945       if (si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
946         {
947           send_ip_unnumbered_details (am, reg,
948                                       sw_if_index,
949                                       si->unnumbered_sw_if_index,
950                                       mp->context);
951         }
952     }
953   else
954     {
955       /* *INDENT-OFF* */
956       pool_foreach (si, im->sw_interfaces)
957        {
958         if ((si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
959           {
960             send_ip_unnumbered_details(am, reg,
961                                        si->sw_if_index,
962                                        si->unnumbered_sw_if_index,
963                                        mp->context);
964           }
965       }
966       /* *INDENT-ON* */
967     }
968
969   BAD_SW_IF_INDEX_LABEL;
970 }
971
972 static void
973 vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
974 {
975   vpe_api_main_t *am = &vpe_api_main;
976   vnet_main_t *vnm = vnet_get_main ();
977   //vlib_main_t *vm = vlib_get_main ();
978   vnet_interface_main_t *im = &vnm->interface_main;
979   vl_api_registration_t *reg;
980   vnet_sw_interface_t *si, *sorted_sis;
981   u32 sw_if_index = ~0;
982
983   reg = vl_api_client_index_to_registration (mp->client_index);
984   if (!reg)
985     return;
986
987   /* Gather interfaces. */
988   sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
989   _vec_len (sorted_sis) = 0;
990   /* *INDENT-OFF* */
991   pool_foreach (si, im->sw_interfaces)
992    {
993     vec_add1 (sorted_sis, si[0]);
994   }
995   /* *INDENT-ON* */
996
997   vec_foreach (si, sorted_sis)
998   {
999     if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
1000       {
1001         /* if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index)) */
1002         /*   { */
1003         /*     continue; */
1004         /*   } */
1005         sw_if_index = si->sw_if_index;
1006         send_ip_details (am, reg, sw_if_index, mp->is_ipv6, mp->context);
1007       }
1008   }
1009
1010   vec_free (sorted_sis);
1011 }
1012
1013 static void
1014 vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t *mp)
1015 {
1016   vl_api_set_ip_flow_hash_reply_t *rmp;
1017   int rv;
1018   u32 table_id;
1019   flow_hash_config_t flow_hash_config = 0;
1020
1021   table_id = ntohl (mp->vrf_id);
1022
1023 #define _(a,b) if (mp->a) flow_hash_config |= b;
1024   foreach_flow_hash_bit_v1;
1025 #undef _
1026
1027   rv = ip_flow_hash_set ((mp->is_ipv6 ? AF_IP6 : AF_IP4), table_id,
1028                          flow_hash_config);
1029
1030   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1031 }
1032
1033 static void
1034 vl_api_set_ip_flow_hash_v2_t_handler (vl_api_set_ip_flow_hash_v2_t *mp)
1035 {
1036   vl_api_set_ip_flow_hash_v2_reply_t *rmp;
1037   ip_address_family_t af;
1038   int rv;
1039
1040   rv = ip_address_family_decode (mp->af, &af);
1041
1042   if (!rv)
1043     rv = ip_flow_hash_set (af, htonl (mp->table_id),
1044                            htonl (mp->flow_hash_config));
1045
1046   REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_V2_REPLY);
1047 }
1048
1049 void
1050 vl_mfib_signal_send_one (vl_api_registration_t * reg,
1051                          u32 context, const mfib_signal_t * mfs)
1052 {
1053   vl_api_mfib_signal_details_t *mp;
1054   const mfib_prefix_t *prefix;
1055   mfib_table_t *mfib;
1056   mfib_itf_t *mfi;
1057
1058   mp = vl_msg_api_alloc (sizeof (*mp));
1059
1060   clib_memset (mp, 0, sizeof (*mp));
1061   mp->_vl_msg_id = ntohs (VL_API_MFIB_SIGNAL_DETAILS);
1062   mp->context = context;
1063
1064   mfi = mfib_itf_get (mfs->mfs_itf);
1065   prefix = mfib_entry_get_prefix (mfs->mfs_entry);
1066   mfib = mfib_table_get (mfib_entry_get_fib_index (mfs->mfs_entry),
1067                          prefix->fp_proto);
1068   mp->table_id = ntohl (mfib->mft_table_id);
1069   mp->sw_if_index = ntohl (mfi->mfi_sw_if_index);
1070
1071   ip_mprefix_encode (prefix, &mp->prefix);
1072
1073   if (0 != mfs->mfs_buffer_len)
1074     {
1075       mp->ip_packet_len = ntohs (mfs->mfs_buffer_len);
1076
1077       memcpy (mp->ip_packet_data, mfs->mfs_buffer, mfs->mfs_buffer_len);
1078     }
1079   else
1080     {
1081       mp->ip_packet_len = 0;
1082     }
1083
1084   vl_api_send_msg (reg, (u8 *) mp);
1085 }
1086
1087 static void
1088 vl_api_mfib_signal_dump_t_handler (vl_api_mfib_signal_dump_t * mp)
1089 {
1090   vl_api_registration_t *reg;
1091
1092   reg = vl_api_client_index_to_registration (mp->client_index);
1093   if (!reg)
1094     return;
1095
1096   while (vl_api_can_send_msg (reg) && mfib_signal_send_one (reg, mp->context))
1097     ;
1098 }
1099
1100 static void
1101   vl_api_ip_container_proxy_add_del_t_handler
1102   (vl_api_ip_container_proxy_add_del_t * mp)
1103 {
1104   vl_api_ip_container_proxy_add_del_reply_t *rmp;
1105   vnet_ip_container_proxy_args_t args;
1106   int rv = 0;
1107   clib_error_t *error;
1108
1109   clib_memset (&args, 0, sizeof (args));
1110
1111   ip_prefix_decode (&mp->pfx, &args.prefix);
1112
1113   args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
1114   args.is_add = mp->is_add;
1115   if ((error = vnet_ip_container_proxy_add_del (&args)))
1116     {
1117       rv = clib_error_get_code (error);
1118       clib_error_report (error);
1119     }
1120
1121   REPLY_MACRO (VL_API_IP_CONTAINER_PROXY_ADD_DEL_REPLY);
1122 }
1123
1124 typedef struct ip_container_proxy_walk_ctx_t_
1125 {
1126   vl_api_registration_t *reg;
1127   u32 context;
1128 } ip_container_proxy_walk_ctx_t;
1129
1130 static int
1131 ip_container_proxy_send_details (const fib_prefix_t * pfx, u32 sw_if_index,
1132                                  void *args)
1133 {
1134   vl_api_ip_container_proxy_details_t *mp;
1135   ip_container_proxy_walk_ctx_t *ctx = args;
1136
1137   mp = vl_msg_api_alloc (sizeof (*mp));
1138   if (!mp)
1139     return 1;
1140
1141   clib_memset (mp, 0, sizeof (*mp));
1142   mp->_vl_msg_id = ntohs (VL_API_IP_CONTAINER_PROXY_DETAILS);
1143   mp->context = ctx->context;
1144
1145   mp->sw_if_index = ntohl (sw_if_index);
1146   ip_prefix_encode (pfx, &mp->prefix);
1147
1148   vl_api_send_msg (ctx->reg, (u8 *) mp);
1149
1150   return 1;
1151 }
1152
1153 static void
1154 vl_api_ip_container_proxy_dump_t_handler (vl_api_ip_container_proxy_dump_t *
1155                                           mp)
1156 {
1157   vl_api_registration_t *reg;
1158
1159   reg = vl_api_client_index_to_registration (mp->client_index);
1160   if (!reg)
1161     return;
1162
1163   ip_container_proxy_walk_ctx_t ctx = {
1164     .context = mp->context,
1165     .reg = reg,
1166   };
1167
1168   ip_container_proxy_walk (ip_container_proxy_send_details, &ctx);
1169 }
1170
1171 static void
1172 vl_api_ioam_enable_t_handler (vl_api_ioam_enable_t * mp)
1173 {
1174   int rv = 0;
1175   vl_api_ioam_enable_reply_t *rmp;
1176   clib_error_t *error;
1177
1178   /* Ignoring the profile id as currently a single profile
1179    * is supported */
1180   error = ip6_ioam_enable (mp->trace_enable, mp->pot_enable,
1181                            mp->seqno, mp->analyse);
1182   if (error)
1183     {
1184       clib_error_report (error);
1185       rv = clib_error_get_code (error);
1186     }
1187
1188   REPLY_MACRO (VL_API_IOAM_ENABLE_REPLY);
1189 }
1190
1191 static void
1192 vl_api_ioam_disable_t_handler (vl_api_ioam_disable_t * mp)
1193 {
1194   int rv = 0;
1195   vl_api_ioam_disable_reply_t *rmp;
1196   clib_error_t *error;
1197
1198   error = clear_ioam_rewrite_fn ();
1199   if (error)
1200     {
1201       clib_error_report (error);
1202       rv = clib_error_get_code (error);
1203     }
1204
1205   REPLY_MACRO (VL_API_IOAM_DISABLE_REPLY);
1206 }
1207
1208 static void
1209   vl_api_ip_source_and_port_range_check_add_del_t_handler
1210   (vl_api_ip_source_and_port_range_check_add_del_t * mp)
1211 {
1212   vl_api_ip_source_and_port_range_check_add_del_reply_t *rmp;
1213   int rv = 0;
1214
1215   u8 is_add = mp->is_add;
1216   fib_prefix_t pfx;
1217   u16 *low_ports = 0;
1218   u16 *high_ports = 0;
1219   u32 vrf_id;
1220   u16 tmp_low, tmp_high;
1221   u8 num_ranges;
1222   int i;
1223
1224   ip_prefix_decode (&mp->prefix, &pfx);
1225
1226   // Validate port range
1227   num_ranges = mp->number_of_ranges;
1228   if (num_ranges > 32)
1229     {                           // This is size of array in VPE.API
1230       rv = VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1231       goto reply;
1232     }
1233
1234   vec_reset_length (low_ports);
1235   vec_reset_length (high_ports);
1236
1237   for (i = 0; i < num_ranges; i++)
1238     {
1239       tmp_low = mp->low_ports[i];
1240       tmp_high = mp->high_ports[i];
1241       // If tmp_low <= tmp_high then only need to check tmp_low = 0
1242       // If tmp_low <= tmp_high then only need to check tmp_high > 65535
1243       if (tmp_low > tmp_high || tmp_low == 0 || tmp_high > 65535)
1244         {
1245           rv = VNET_API_ERROR_INVALID_VALUE;
1246           goto reply;
1247         }
1248       vec_add1 (low_ports, tmp_low);
1249       vec_add1 (high_ports, tmp_high + 1);
1250     }
1251
1252   vrf_id = ntohl (mp->vrf_id);
1253
1254   if (vrf_id < 1)
1255     {
1256       rv = VNET_API_ERROR_INVALID_VALUE;
1257       goto reply;
1258     }
1259
1260
1261   if (FIB_PROTOCOL_IP6 == pfx.fp_proto)
1262     {
1263       rv = ip6_source_and_port_range_check_add_del (&pfx.fp_addr.ip6,
1264                                                     pfx.fp_len,
1265                                                     vrf_id,
1266                                                     low_ports,
1267                                                     high_ports, is_add);
1268     }
1269   else
1270     {
1271       rv = ip4_source_and_port_range_check_add_del (&pfx.fp_addr.ip4,
1272                                                     pfx.fp_len,
1273                                                     vrf_id,
1274                                                     low_ports,
1275                                                     high_ports, is_add);
1276     }
1277
1278 reply:
1279   vec_free (low_ports);
1280   vec_free (high_ports);
1281   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_ADD_DEL_REPLY);
1282 }
1283
1284 static void
1285   vl_api_ip_source_and_port_range_check_interface_add_del_t_handler
1286   (vl_api_ip_source_and_port_range_check_interface_add_del_t * mp)
1287 {
1288   vlib_main_t *vm = vlib_get_main ();
1289   vl_api_ip_source_and_port_range_check_interface_add_del_reply_t *rmp;
1290   ip4_main_t *im = &ip4_main;
1291   int rv;
1292   u32 sw_if_index;
1293   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1294   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
1295   uword *p = 0;
1296   int i;
1297
1298   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] =
1299     ntohl (mp->tcp_out_vrf_id);
1300   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] =
1301     ntohl (mp->udp_out_vrf_id);
1302   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] =
1303     ntohl (mp->tcp_in_vrf_id);
1304   vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] =
1305     ntohl (mp->udp_in_vrf_id);
1306
1307
1308   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
1309     {
1310       if (vrf_id[i] != 0 && vrf_id[i] != ~0)
1311         {
1312           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
1313
1314           if (p == 0)
1315             {
1316               rv = VNET_API_ERROR_INVALID_VALUE;
1317               goto reply;
1318             }
1319
1320           fib_index[i] = p[0];
1321         }
1322       else
1323         fib_index[i] = ~0;
1324     }
1325   sw_if_index = ntohl (mp->sw_if_index);
1326
1327   VALIDATE_SW_IF_INDEX (mp);
1328
1329   rv =
1330     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index,
1331                                         mp->is_add);
1332
1333   BAD_SW_IF_INDEX_LABEL;
1334 reply:
1335
1336   REPLY_MACRO (VL_API_IP_SOURCE_AND_PORT_RANGE_CHECK_INTERFACE_ADD_DEL_REPLY);
1337 }
1338
1339 static void
1340   vl_api_sw_interface_ip6_set_link_local_address_t_handler
1341   (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1342 {
1343   vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1344   ip6_address_t ip;
1345   int rv;
1346
1347   VALIDATE_SW_IF_INDEX (mp);
1348
1349   ip6_address_decode (mp->ip, &ip);
1350
1351   rv = ip6_link_set_local_address (ntohl (mp->sw_if_index), &ip);
1352
1353   BAD_SW_IF_INDEX_LABEL;
1354   REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1355 }
1356
1357 static void
1358 vl_api_sw_interface_ip6_get_link_local_address_t_handler (
1359   vl_api_sw_interface_ip6_get_link_local_address_t *mp)
1360 {
1361   vl_api_sw_interface_ip6_get_link_local_address_reply_t *rmp;
1362   const ip6_address_t *ip = NULL;
1363   int rv = 0;
1364
1365   VALIDATE_SW_IF_INDEX (mp);
1366
1367   ip = ip6_get_link_local_address (ntohl (mp->sw_if_index));
1368   if (NULL == ip)
1369     rv = VNET_API_ERROR_IP6_NOT_ENABLED;
1370
1371   BAD_SW_IF_INDEX_LABEL;
1372   /* clang-format off */
1373   REPLY_MACRO2 (VL_API_SW_INTERFACE_IP6_GET_LINK_LOCAL_ADDRESS_REPLY,
1374   ({
1375     if (!rv)
1376       ip6_address_encode (ip, rmp->ip);
1377   }))
1378   /* clang-format on */
1379 }
1380
1381 static void
1382 vl_api_ip_table_replace_begin_t_handler (vl_api_ip_table_replace_begin_t * mp)
1383 {
1384   vl_api_ip_table_replace_begin_reply_t *rmp;
1385   fib_protocol_t fproto;
1386   u32 fib_index;
1387   int rv = 0;
1388
1389   fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1390   fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1391
1392   if (INDEX_INVALID == fib_index)
1393     rv = VNET_API_ERROR_NO_SUCH_FIB;
1394   else
1395     {
1396       fib_table_mark (fib_index, fproto, FIB_SOURCE_API);
1397       mfib_table_mark (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1398                        fproto, MFIB_SOURCE_API);
1399     }
1400   REPLY_MACRO (VL_API_IP_TABLE_REPLACE_BEGIN_REPLY);
1401 }
1402
1403 static void
1404 vl_api_ip_table_replace_end_t_handler (vl_api_ip_table_replace_end_t * mp)
1405 {
1406   vl_api_ip_table_replace_end_reply_t *rmp;
1407   fib_protocol_t fproto;
1408   u32 fib_index;
1409   int rv = 0;
1410
1411   fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1412   fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1413
1414   if (INDEX_INVALID == fib_index)
1415     rv = VNET_API_ERROR_NO_SUCH_FIB;
1416   else
1417     {
1418       fib_table_sweep (fib_index, fproto, FIB_SOURCE_API);
1419       mfib_table_sweep (mfib_table_find
1420                         (fproto, ntohl (mp->table.table_id)), fproto,
1421                         MFIB_SOURCE_API);
1422     }
1423   REPLY_MACRO (VL_API_IP_TABLE_REPLACE_END_REPLY);
1424 }
1425
1426 static void
1427 vl_api_ip_table_flush_t_handler (vl_api_ip_table_flush_t * mp)
1428 {
1429   vl_api_ip_table_flush_reply_t *rmp;
1430   fib_protocol_t fproto;
1431   u32 fib_index;
1432   int rv = 0;
1433
1434   fproto = (mp->table.is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
1435   fib_index = fib_table_find (fproto, ntohl (mp->table.table_id));
1436
1437   if (INDEX_INVALID == fib_index)
1438     rv = VNET_API_ERROR_NO_SUCH_FIB;
1439   else
1440     {
1441       vnet_main_t *vnm = vnet_get_main ();
1442       vnet_interface_main_t *im = &vnm->interface_main;
1443       vnet_sw_interface_t *si;
1444
1445       /* Shut down interfaces in this FIB / clean out intfc routes */
1446       /* *INDENT-OFF* */
1447       pool_foreach (si, im->sw_interfaces)
1448        {
1449         if (fib_index == fib_table_get_index_for_sw_if_index (fproto,
1450                                                               si->sw_if_index))
1451           {
1452             u32 flags = si->flags;
1453             flags &= ~VNET_SW_INTERFACE_FLAG_ADMIN_UP;
1454             vnet_sw_interface_set_flags (vnm, si->sw_if_index, flags);
1455           }
1456       }
1457       /* *INDENT-ON* */
1458
1459       fib_table_flush (fib_index, fproto, FIB_SOURCE_API);
1460       mfib_table_flush (mfib_table_find (fproto, ntohl (mp->table.table_id)),
1461                         fproto, MFIB_SOURCE_API);
1462     }
1463
1464   REPLY_MACRO (VL_API_IP_TABLE_FLUSH_REPLY);
1465 }
1466
1467 void
1468 vl_api_ip_reassembly_set_t_handler (vl_api_ip_reassembly_set_t * mp)
1469 {
1470   vl_api_ip_reassembly_set_reply_t *rmp;
1471   int rv = 0;
1472   switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
1473     {
1474     case IP_REASS_TYPE_FULL:
1475       if (mp->is_ip6)
1476         {
1477           rv = ip6_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1478                                    clib_net_to_host_u32
1479                                    (mp->max_reassemblies),
1480                                    clib_net_to_host_u32
1481                                    (mp->max_reassembly_length),
1482                                    clib_net_to_host_u32
1483                                    (mp->expire_walk_interval_ms));
1484         }
1485       else
1486         {
1487           rv = ip4_full_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1488                                    clib_net_to_host_u32
1489                                    (mp->max_reassemblies),
1490                                    clib_net_to_host_u32
1491                                    (mp->max_reassembly_length),
1492                                    clib_net_to_host_u32
1493                                    (mp->expire_walk_interval_ms));
1494         }
1495       break;
1496     case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1497       if (mp->is_ip6)
1498         {
1499           rv =
1500             ip6_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1501                               clib_net_to_host_u32 (mp->max_reassemblies),
1502                               clib_net_to_host_u32
1503                               (mp->max_reassembly_length),
1504                               clib_net_to_host_u32
1505                               (mp->expire_walk_interval_ms));
1506         }
1507       else
1508         {
1509           rv = ip4_sv_reass_set (clib_net_to_host_u32 (mp->timeout_ms),
1510                                  clib_net_to_host_u32 (mp->max_reassemblies),
1511                                  clib_net_to_host_u32
1512                                  (mp->max_reassembly_length),
1513                                  clib_net_to_host_u32
1514                                  (mp->expire_walk_interval_ms));
1515         }
1516       break;
1517     }
1518
1519   REPLY_MACRO (VL_API_IP_REASSEMBLY_SET_REPLY);
1520 }
1521
1522 void
1523 vl_api_ip_reassembly_get_t_handler (vl_api_ip_reassembly_get_t * mp)
1524 {
1525   vl_api_registration_t *rp;
1526
1527   rp = vl_api_client_index_to_registration (mp->client_index);
1528   if (rp == 0)
1529     return;
1530
1531   vl_api_ip_reassembly_get_reply_t *rmp = vl_msg_api_alloc (sizeof (*rmp));
1532   clib_memset (rmp, 0, sizeof (*rmp));
1533   rmp->_vl_msg_id = ntohs (VL_API_IP_REASSEMBLY_GET_REPLY);
1534   rmp->context = mp->context;
1535   rmp->retval = 0;
1536   u32 timeout_ms;
1537   u32 max_reassemblies;
1538   u32 max_reassembly_length;
1539   u32 expire_walk_interval_ms;
1540   switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
1541     {
1542     case IP_REASS_TYPE_FULL:
1543       if (mp->is_ip6)
1544         {
1545           rmp->is_ip6 = 1;
1546           ip6_full_reass_get (&timeout_ms, &max_reassemblies,
1547                               &max_reassembly_length,
1548                               &expire_walk_interval_ms);
1549         }
1550       else
1551         {
1552           rmp->is_ip6 = 0;
1553           ip4_full_reass_get (&timeout_ms, &max_reassemblies,
1554                               &max_reassembly_length,
1555                               &expire_walk_interval_ms);
1556         }
1557       break;
1558     case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1559       if (mp->is_ip6)
1560         {
1561           rmp->is_ip6 = 1;
1562           ip6_sv_reass_get (&timeout_ms, &max_reassemblies,
1563                             &max_reassembly_length, &expire_walk_interval_ms);
1564         }
1565       else
1566         {
1567           rmp->is_ip6 = 0;
1568           ip4_sv_reass_get (&timeout_ms, &max_reassemblies,
1569                             &max_reassembly_length, &expire_walk_interval_ms);
1570         }
1571       break;
1572     }
1573   rmp->timeout_ms = clib_host_to_net_u32 (timeout_ms);
1574   rmp->max_reassemblies = clib_host_to_net_u32 (max_reassemblies);
1575   rmp->max_reassembly_length = clib_host_to_net_u32 (max_reassembly_length);
1576   rmp->expire_walk_interval_ms =
1577     clib_host_to_net_u32 (expire_walk_interval_ms);
1578   vl_api_send_msg (rp, (u8 *) rmp);
1579 }
1580
1581 void
1582   vl_api_ip_reassembly_enable_disable_t_handler
1583   (vl_api_ip_reassembly_enable_disable_t * mp)
1584 {
1585   vl_api_ip_reassembly_enable_disable_reply_t *rmp;
1586   int rv = 0;
1587   switch ((vl_api_ip_reass_type_t) clib_net_to_host_u32 (mp->type))
1588     {
1589     case IP_REASS_TYPE_FULL:
1590       rv =
1591         ip4_full_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1592                                        mp->enable_ip4);
1593       if (0 == rv)
1594         rv =
1595           ip6_full_reass_enable_disable (clib_net_to_host_u32
1596                                          (mp->sw_if_index), mp->enable_ip6);
1597       break;
1598     case IP_REASS_TYPE_SHALLOW_VIRTUAL:
1599       rv =
1600         ip4_sv_reass_enable_disable (clib_net_to_host_u32 (mp->sw_if_index),
1601                                      mp->enable_ip4);
1602       if (0 == rv)
1603         {
1604           rv =
1605             ip6_sv_reass_enable_disable (clib_net_to_host_u32
1606                                          (mp->sw_if_index), mp->enable_ip6);
1607         }
1608       break;
1609     }
1610
1611   REPLY_MACRO (VL_API_IP_REASSEMBLY_ENABLE_DISABLE_REPLY);
1612 }
1613
1614 typedef struct ip_punt_redirect_walk_ctx_t_
1615 {
1616   vl_api_registration_t *reg;
1617   u32 context;
1618 } ip_punt_redirect_walk_ctx_t;
1619
1620 static walk_rc_t
1621 send_ip_punt_redirect_details (u32 rx_sw_if_index,
1622                                const ip_punt_redirect_rx_t * ipr, void *arg)
1623 {
1624   ip_punt_redirect_walk_ctx_t *ctx = arg;
1625   vl_api_ip_punt_redirect_details_t *mp;
1626   fib_path_encode_ctx_t path_ctx = {
1627     .rpaths = NULL,
1628   };
1629
1630   mp = vl_msg_api_alloc (sizeof (*mp));
1631   if (!mp)
1632     return (WALK_STOP);;
1633
1634   clib_memset (mp, 0, sizeof (*mp));
1635   mp->_vl_msg_id = ntohs (VL_API_IP_PUNT_REDIRECT_DETAILS);
1636   mp->context = ctx->context;
1637
1638   fib_path_list_walk_w_ext (ipr->pl, NULL, fib_path_encode, &path_ctx);
1639
1640   mp->punt.rx_sw_if_index = htonl (rx_sw_if_index);
1641   mp->punt.tx_sw_if_index = htonl (path_ctx.rpaths[0].frp_sw_if_index);
1642
1643   ip_address_encode (&path_ctx.rpaths[0].frp_addr,
1644                      fib_proto_to_ip46 (ipr->fproto), &mp->punt.nh);
1645
1646   vl_api_send_msg (ctx->reg, (u8 *) mp);
1647
1648   vec_free (path_ctx.rpaths);
1649
1650   return (WALK_CONTINUE);
1651 }
1652
1653 static void
1654 vl_api_ip_punt_redirect_dump_t_handler (vl_api_ip_punt_redirect_dump_t * mp)
1655 {
1656   vl_api_registration_t *reg;
1657   fib_protocol_t fproto = FIB_PROTOCOL_IP4;
1658
1659   reg = vl_api_client_index_to_registration (mp->client_index);
1660   if (!reg)
1661     return;
1662
1663   if (mp->is_ipv6 == 1)
1664     fproto = FIB_PROTOCOL_IP6;
1665
1666   ip_punt_redirect_walk_ctx_t ctx = {
1667     .reg = reg,
1668     .context = mp->context,
1669   };
1670
1671   if (~0 != mp->sw_if_index)
1672     {
1673       u32 rx_sw_if_index;
1674       index_t pri;
1675
1676       rx_sw_if_index = ntohl (mp->sw_if_index);
1677       pri = ip_punt_redirect_find (fproto, rx_sw_if_index);
1678
1679       if (INDEX_INVALID == pri)
1680         return;
1681
1682       send_ip_punt_redirect_details (rx_sw_if_index,
1683                                      ip_punt_redirect_get (pri), &ctx);
1684     }
1685   else
1686     ip_punt_redirect_walk (fproto, send_ip_punt_redirect_details, &ctx);
1687 }
1688
1689 #define vl_msg_name_crc_list
1690 #include <vnet/ip/ip.api.h>
1691 #undef vl_msg_name_crc_list
1692
1693 static void
1694 setup_message_id_table (api_main_t * am)
1695 {
1696 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1697   foreach_vl_msg_name_crc_ip;
1698 #undef _
1699 }
1700
1701 static clib_error_t *
1702 ip_api_hookup (vlib_main_t * vm)
1703 {
1704   api_main_t *am = vlibapi_get_main ();
1705
1706 #define _(N,n)                                                  \
1707     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1708                            vl_api_##n##_t_handler,              \
1709                            vl_noop_handler,                     \
1710                            vl_api_##n##_t_endian,               \
1711                            vl_api_##n##_t_print,                \
1712                            sizeof(vl_api_##n##_t), 1);
1713   foreach_ip_api_msg;
1714 #undef _
1715
1716   /*
1717    * Mark the route add/del API as MP safe
1718    */
1719   am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL] = 1;
1720   am->is_mp_safe[VL_API_IP_ROUTE_ADD_DEL_REPLY] = 1;
1721
1722   /*
1723    * Set up the (msg_name, crc, message-id) table
1724    */
1725   setup_message_id_table (am);
1726
1727   return 0;
1728 }
1729
1730 VLIB_API_INIT_FUNCTION (ip_api_hookup);
1731
1732 /*
1733  * fd.io coding-style-patch-verification: ON
1734  *
1735  * Local Variables:
1736  * eval: (c-set-style "gnu")
1737  * End:
1738  */