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