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