blob: aafde464445ac34d330dc0c387d8a73f895358fa [file] [log] [blame]
Dave Barachb5e8a772016-12-06 12:04:42 -05001/*
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/ip/ip.h>
27#include <vnet/ip/ip6_neighbor.h>
28#include <vnet/fib/fib_table.h>
29#include <vnet/fib/fib_api.h>
30#include <vnet/dpo/drop_dpo.h>
31#include <vnet/dpo/receive_dpo.h>
32#include <vnet/dpo/lookup_dpo.h>
33#include <vnet/dpo/classify_dpo.h>
34#include <vnet/dpo/ip_null_dpo.h>
35#include <vnet/ethernet/arp_packet.h>
36
37#include <vnet/vnet_msg_enum.h>
38
39#define vl_typedefs /* define message structures */
40#include <vnet/vnet_all_api_h.h>
41#undef vl_typedefs
42
43#define vl_endianfun /* define message structures */
44#include <vnet/vnet_all_api_h.h>
45#undef vl_endianfun
46
47/* instantiate all the print functions we know about */
48#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
49#define vl_printfun
50#include <vnet/vnet_all_api_h.h>
51#undef vl_printfun
52
53#include <vlibapi/api_helper_macros.h>
54
55#define foreach_ip_api_msg \
56_(IP_FIB_DUMP, ip_fib_dump) \
57_(IP_FIB_DETAILS, ip_fib_details) \
58_(IP6_FIB_DUMP, ip6_fib_dump) \
59_(IP6_FIB_DETAILS, ip6_fib_details) \
60_(IP_NEIGHBOR_DUMP, ip_neighbor_dump) \
61_(IP_NEIGHBOR_DETAILS, ip_neighbor_details) \
62_(IP_ADDRESS_DUMP, ip_address_dump) \
63_(IP_DUMP, ip_dump) \
64_(IP_NEIGHBOR_ADD_DEL, ip_neighbor_add_del) \
65_(IP_ADD_DEL_ROUTE, ip_add_del_route) \
66_(SET_IP_FLOW_HASH,set_ip_flow_hash) \
67_(SW_INTERFACE_IP6ND_RA_CONFIG, sw_interface_ip6nd_ra_config) \
68_(SW_INTERFACE_IP6ND_RA_PREFIX, sw_interface_ip6nd_ra_prefix) \
69_(SW_INTERFACE_IP6_ENABLE_DISABLE, sw_interface_ip6_enable_disable ) \
70_(SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS, \
71 sw_interface_ip6_set_link_local_address)
72
73extern void stats_dslock_with_hint (int hint, int tag);
74extern void stats_dsunlock (void);
75
76static void
77send_ip_neighbor_details (u8 is_ipv6,
78 u8 is_static,
79 u8 * mac_address,
80 u8 * ip_address,
81 unix_shared_memory_queue_t * q, u32 context)
82{
83 vl_api_ip_neighbor_details_t *mp;
84
85 mp = vl_msg_api_alloc (sizeof (*mp));
86 memset (mp, 0, sizeof (*mp));
87 mp->_vl_msg_id = ntohs (VL_API_IP_NEIGHBOR_DETAILS);
88 mp->context = context;
89 mp->is_ipv6 = is_ipv6;
90 mp->is_static = is_static;
91 memcpy (mp->mac_address, mac_address, 6);
92 memcpy (mp->ip_address, ip_address, (is_ipv6) ? 16 : 4);
93
94 vl_msg_api_send_shmem (q, (u8 *) & mp);
95}
96
97static void
98vl_api_ip_neighbor_details_t_handler (vl_api_ip_neighbor_details_t * mp)
99{
100 clib_warning ("BUG");
101}
102
103static void
104vl_api_ip_neighbor_dump_t_handler (vl_api_ip_neighbor_dump_t * mp)
105{
106 unix_shared_memory_queue_t *q;
107
108 q = vl_api_client_index_to_input_queue (mp->client_index);
109 if (q == 0)
110 return;
111
112 u32 sw_if_index = ntohl (mp->sw_if_index);
113
114 if (mp->is_ipv6)
115 {
116 ip6_neighbor_t *n, *ns;
117
118 ns = ip6_neighbors_entries (sw_if_index);
119 /* *INDENT-OFF* */
120 vec_foreach (n, ns)
121 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500122 send_ip_neighbor_details
123 (mp->is_ipv6, ((n->flags & IP6_NEIGHBOR_FLAG_STATIC) ? 1 : 0),
124 (u8 *) n->link_layer_address,
125 (u8 *) & (n->key.ip6_address.as_u8),
126 q, mp->context);
Dave Barachb5e8a772016-12-06 12:04:42 -0500127 }
128 /* *INDENT-ON* */
129 vec_free (ns);
130 }
131 else
132 {
133 ethernet_arp_ip4_entry_t *n, *ns;
134
135 ns = ip4_neighbor_entries (sw_if_index);
136 /* *INDENT-OFF* */
137 vec_foreach (n, ns)
138 {
139 send_ip_neighbor_details (mp->is_ipv6,
140 ((n->flags & ETHERNET_ARP_IP4_ENTRY_FLAG_STATIC) ? 1 : 0),
141 (u8*) n->ethernet_address,
142 (u8*) & (n->ip4_address.as_u8),
143 q, mp->context);
144 }
145 /* *INDENT-ON* */
146 vec_free (ns);
147 }
148}
149
150
151void
152copy_fib_next_hop (fib_route_path_encode_t * api_rpath, void *fp_arg)
153{
154 int is_ip4;
155 vl_api_fib_path_t *fp = (vl_api_fib_path_t *) fp_arg;
156
157 if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP4)
158 fp->afi = IP46_TYPE_IP4;
159 else if (api_rpath->rpath.frp_proto == FIB_PROTOCOL_IP6)
160 fp->afi = IP46_TYPE_IP6;
161 else
162 {
163 is_ip4 = ip46_address_is_ip4 (&api_rpath->rpath.frp_addr);
164 if (is_ip4)
165 fp->afi = IP46_TYPE_IP4;
166 else
167 fp->afi = IP46_TYPE_IP6;
168 }
169 if (fp->afi == IP46_TYPE_IP4)
170 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip4,
171 sizeof (api_rpath->rpath.frp_addr.ip4));
172 else
173 memcpy (fp->next_hop, &api_rpath->rpath.frp_addr.ip6,
174 sizeof (api_rpath->rpath.frp_addr.ip6));
175}
176
177static void
178vl_api_ip_fib_details_t_handler (vl_api_ip_fib_details_t * mp)
179{
180 clib_warning ("BUG");
181}
182
183static void
184vl_api_ip_fib_details_t_endian (vl_api_ip_fib_details_t * mp)
185{
186 clib_warning ("BUG");
187}
188
189static void
190vl_api_ip_fib_details_t_print (vl_api_ip_fib_details_t * mp)
191{
192 clib_warning ("BUG");
193}
194
195static void
196send_ip_fib_details (vpe_api_main_t * am,
197 unix_shared_memory_queue_t * q,
198 u32 table_id, fib_prefix_t * pfx,
199 fib_route_path_encode_t * api_rpaths, u32 context)
200{
201 vl_api_ip_fib_details_t *mp;
202 fib_route_path_encode_t *api_rpath;
203 vl_api_fib_path_t *fp;
204 int path_count;
205
206 path_count = vec_len (api_rpaths);
207 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
208 if (!mp)
209 return;
210 memset (mp, 0, sizeof (*mp));
211 mp->_vl_msg_id = ntohs (VL_API_IP_FIB_DETAILS);
212 mp->context = context;
213
214 mp->table_id = htonl (table_id);
215 mp->address_length = pfx->fp_len;
216 memcpy (mp->address, &pfx->fp_addr.ip4, sizeof (pfx->fp_addr.ip4));
217
218 mp->count = htonl (path_count);
219 fp = mp->path;
220 vec_foreach (api_rpath, api_rpaths)
221 {
222 memset (fp, 0, sizeof (*fp));
223 switch (api_rpath->dpo.dpoi_type)
224 {
225 case DPO_RECEIVE:
226 fp->is_local = true;
227 break;
228 case DPO_DROP:
229 fp->is_drop = true;
230 break;
231 case DPO_IP_NULL:
232 switch (api_rpath->dpo.dpoi_index)
233 {
234 case IP_NULL_ACTION_NONE:
235 fp->is_drop = true;
236 break;
237 case IP_NULL_ACTION_SEND_ICMP_UNREACH:
238 fp->is_unreach = true;
239 break;
240 case IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
241 fp->is_prohibit = true;
242 break;
243 default:
244 break;
245 }
246 break;
247 default:
248 break;
249 }
250 fp->weight = htonl (api_rpath->rpath.frp_weight);
251 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
252 copy_fib_next_hop (api_rpath, fp);
253 fp++;
254 }
255
256 vl_msg_api_send_shmem (q, (u8 *) & mp);
257}
258
259static void
260vl_api_ip_fib_dump_t_handler (vl_api_ip_fib_dump_t * mp)
261{
262 vpe_api_main_t *am = &vpe_api_main;
263 unix_shared_memory_queue_t *q;
264 ip4_main_t *im = &ip4_main;
265 fib_table_t *fib_table;
266 fib_node_index_t lfei, *lfeip, *lfeis = NULL;
267 mpls_label_t key;
268 fib_prefix_t pfx;
269 u32 fib_index;
270 fib_route_path_encode_t *api_rpaths;
271 int i;
272
273 q = vl_api_client_index_to_input_queue (mp->client_index);
274 if (q == 0)
275 return;
276
277 /* *INDENT-OFF* */
278 pool_foreach (fib_table, im->fibs,
279 ({
280 for (i = 0; i < ARRAY_LEN (fib_table->v4.fib_entry_by_dst_address); i++)
281 {
282 hash_foreach(key, lfei, fib_table->v4.fib_entry_by_dst_address[i],
283 ({
284 vec_add1(lfeis, lfei);
285 }));
286 }
287 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500288 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500289
Dave Barachd7cb1b52016-12-09 09:52:16 -0500290 vec_sort_with_function (lfeis, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500291
Dave Barachd7cb1b52016-12-09 09:52:16 -0500292 vec_foreach (lfeip, lfeis)
Dave Barachb5e8a772016-12-06 12:04:42 -0500293 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500294 fib_entry_get_prefix (*lfeip, &pfx);
295 fib_index = fib_entry_get_fib_index (*lfeip);
296 fib_table = fib_table_get (fib_index, pfx.fp_proto);
Dave Barachb5e8a772016-12-06 12:04:42 -0500297 api_rpaths = NULL;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500298 fib_entry_encode (*lfeip, &api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500299 send_ip_fib_details (am, q,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500300 fib_table->ft_table_id, &pfx, api_rpaths,
301 mp->context);
302 vec_free (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500303 }
304
305 vec_free (lfeis);
306}
307
308static void
309vl_api_ip6_fib_details_t_handler (vl_api_ip6_fib_details_t * mp)
310{
311 clib_warning ("BUG");
312}
313
314static void
315vl_api_ip6_fib_details_t_endian (vl_api_ip6_fib_details_t * mp)
316{
317 clib_warning ("BUG");
318}
319
320static void
321vl_api_ip6_fib_details_t_print (vl_api_ip6_fib_details_t * mp)
322{
323 clib_warning ("BUG");
324}
325
326static void
327send_ip6_fib_details (vpe_api_main_t * am,
Dave Barachd7cb1b52016-12-09 09:52:16 -0500328 unix_shared_memory_queue_t * q,
329 u32 table_id, fib_prefix_t * pfx,
330 fib_route_path_encode_t * api_rpaths, u32 context)
Dave Barachb5e8a772016-12-06 12:04:42 -0500331{
332 vl_api_ip6_fib_details_t *mp;
333 fib_route_path_encode_t *api_rpath;
334 vl_api_fib_path_t *fp;
335 int path_count;
336
Dave Barachd7cb1b52016-12-09 09:52:16 -0500337 path_count = vec_len (api_rpaths);
Dave Barachb5e8a772016-12-06 12:04:42 -0500338 mp = vl_msg_api_alloc (sizeof (*mp) + path_count * sizeof (*fp));
339 if (!mp)
340 return;
341 memset (mp, 0, sizeof (*mp));
342 mp->_vl_msg_id = ntohs (VL_API_IP6_FIB_DETAILS);
343 mp->context = context;
344
345 mp->table_id = htonl (table_id);
346 mp->address_length = pfx->fp_len;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500347 memcpy (mp->address, &pfx->fp_addr.ip6, sizeof (pfx->fp_addr.ip6));
Dave Barachb5e8a772016-12-06 12:04:42 -0500348
349 mp->count = htonl (path_count);
350 fp = mp->path;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500351 vec_foreach (api_rpath, api_rpaths)
Dave Barachb5e8a772016-12-06 12:04:42 -0500352 {
353 memset (fp, 0, sizeof (*fp));
354 switch (api_rpath->dpo.dpoi_type)
355 {
356 case DPO_RECEIVE:
357 fp->is_local = true;
358 break;
359 case DPO_DROP:
360 fp->is_drop = true;
361 break;
362 case DPO_IP_NULL:
363 switch (api_rpath->dpo.dpoi_index)
364 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500365 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_NONE:
Dave Barachb5e8a772016-12-06 12:04:42 -0500366 fp->is_drop = true;
367 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500368 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_UNREACH:
Dave Barachb5e8a772016-12-06 12:04:42 -0500369 fp->is_unreach = true;
370 break;
Dave Barachd7cb1b52016-12-09 09:52:16 -0500371 case IP_NULL_DPO_ACTION_NUM + IP_NULL_ACTION_SEND_ICMP_PROHIBIT:
Dave Barachb5e8a772016-12-06 12:04:42 -0500372 fp->is_prohibit = true;
373 break;
374 default:
375 break;
376 }
377 break;
378 default:
379 break;
380 }
Dave Barachd7cb1b52016-12-09 09:52:16 -0500381 fp->weight = htonl (api_rpath->rpath.frp_weight);
382 fp->sw_if_index = htonl (api_rpath->rpath.frp_sw_if_index);
Dave Barachb5e8a772016-12-06 12:04:42 -0500383 copy_fib_next_hop (api_rpath, fp);
384 fp++;
385 }
386
387 vl_msg_api_send_shmem (q, (u8 *) & mp);
388}
389
Dave Barachd7cb1b52016-12-09 09:52:16 -0500390typedef struct apt_ip6_fib_show_ctx_t_
391{
392 u32 fib_index;
393 fib_node_index_t *entries;
Dave Barachb5e8a772016-12-06 12:04:42 -0500394} api_ip6_fib_show_ctx_t;
395
396static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500397api_ip6_fib_table_put_entries (clib_bihash_kv_24_8_t * kvp, void *arg)
Dave Barachb5e8a772016-12-06 12:04:42 -0500398{
399 api_ip6_fib_show_ctx_t *ctx = arg;
400
401 if ((kvp->key[2] >> 32) == ctx->fib_index)
402 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500403 vec_add1 (ctx->entries, kvp->value);
Dave Barachb5e8a772016-12-06 12:04:42 -0500404 }
405}
406
407static void
Dave Barachd7cb1b52016-12-09 09:52:16 -0500408api_ip6_fib_table_get_all (unix_shared_memory_queue_t * q,
409 vl_api_ip6_fib_dump_t * mp,
410 fib_table_t * fib_table)
Dave Barachb5e8a772016-12-06 12:04:42 -0500411{
412 vpe_api_main_t *am = &vpe_api_main;
413 ip6_main_t *im6 = &ip6_main;
414 ip6_fib_t *fib = &fib_table->v6;
415 fib_node_index_t *fib_entry_index;
416 api_ip6_fib_show_ctx_t ctx = {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500417 .fib_index = fib->index,.entries = NULL,
Dave Barachb5e8a772016-12-06 12:04:42 -0500418 };
419 fib_route_path_encode_t *api_rpaths;
420 fib_prefix_t pfx;
421
Dave Barachd7cb1b52016-12-09 09:52:16 -0500422 BV (clib_bihash_foreach_key_value_pair)
423 ((BVT (clib_bihash) *) & im6->ip6_table[IP6_FIB_TABLE_NON_FWDING].
424 ip6_hash, api_ip6_fib_table_put_entries, &ctx);
Dave Barachb5e8a772016-12-06 12:04:42 -0500425
Dave Barachd7cb1b52016-12-09 09:52:16 -0500426 vec_sort_with_function (ctx.entries, fib_entry_cmp_for_sort);
Dave Barachb5e8a772016-12-06 12:04:42 -0500427
Dave Barachd7cb1b52016-12-09 09:52:16 -0500428 vec_foreach (fib_entry_index, ctx.entries)
429 {
430 fib_entry_get_prefix (*fib_entry_index, &pfx);
431 api_rpaths = NULL;
432 fib_entry_encode (*fib_entry_index, &api_rpaths);
433 send_ip6_fib_details (am, q,
434 fib_table->ft_table_id,
435 &pfx, api_rpaths, mp->context);
436 vec_free (api_rpaths);
437 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500438
Dave Barachd7cb1b52016-12-09 09:52:16 -0500439 vec_free (ctx.entries);
Dave Barachb5e8a772016-12-06 12:04:42 -0500440}
441
442static void
443vl_api_ip6_fib_dump_t_handler (vl_api_ip6_fib_dump_t * mp)
444{
445 unix_shared_memory_queue_t *q;
446 ip6_main_t *im6 = &ip6_main;
447 fib_table_t *fib_table;
448
449 q = vl_api_client_index_to_input_queue (mp->client_index);
450 if (q == 0)
451 return;
452
453 /* *INDENT-OFF* */
454 pool_foreach (fib_table, im6->fibs,
455 ({
456 api_ip6_fib_table_get_all(q, mp, fib_table);
457 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500458 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500459}
460
461static void
462vl_api_ip_neighbor_add_del_t_handler (vl_api_ip_neighbor_add_del_t * mp,
463 vlib_main_t * vm)
464{
465 vl_api_ip_neighbor_add_del_reply_t *rmp;
466 vnet_main_t *vnm = vnet_get_main ();
467 int rv = 0;
468
469 VALIDATE_SW_IF_INDEX (mp);
470
471 stats_dslock_with_hint (1 /* release hint */ , 7 /* tag */ );
472
473 /*
474 * there's no validation here of the ND/ARP entry being added.
475 * The expectation is that the FIB will ensure that nothing bad
476 * will come of adding bogus entries.
477 */
478 if (mp->is_ipv6)
479 {
480 if (mp->is_add)
481 rv = vnet_set_ip6_ethernet_neighbor
482 (vm, ntohl (mp->sw_if_index),
483 (ip6_address_t *) (mp->dst_address),
484 mp->mac_address, sizeof (mp->mac_address), mp->is_static);
485 else
486 rv = vnet_unset_ip6_ethernet_neighbor
487 (vm, ntohl (mp->sw_if_index),
488 (ip6_address_t *) (mp->dst_address),
489 mp->mac_address, sizeof (mp->mac_address));
490 }
491 else
492 {
493 ethernet_arp_ip4_over_ethernet_address_t a;
494
495 clib_memcpy (&a.ethernet, mp->mac_address, 6);
496 clib_memcpy (&a.ip4, mp->dst_address, 4);
497
498 if (mp->is_add)
499 rv = vnet_arp_set_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index),
500 &a, mp->is_static);
501 else
502 rv =
503 vnet_arp_unset_ip4_over_ethernet (vnm, ntohl (mp->sw_if_index), &a);
504 }
505
506 BAD_SW_IF_INDEX_LABEL;
507
508 stats_dsunlock ();
509 REPLY_MACRO (VL_API_IP_NEIGHBOR_ADD_DEL_REPLY);
510}
511
512int
513add_del_route_t_handler (u8 is_multipath,
514 u8 is_add,
515 u8 is_drop,
516 u8 is_unreach,
517 u8 is_prohibit,
518 u8 is_local,
519 u8 is_classify,
520 u32 classify_table_index,
521 u8 is_resolve_host,
522 u8 is_resolve_attached,
523 u32 fib_index,
524 const fib_prefix_t * prefix,
525 u8 next_hop_proto_is_ip4,
526 const ip46_address_t * next_hop,
527 u32 next_hop_sw_if_index,
528 u8 next_hop_fib_index,
529 u32 next_hop_weight,
530 mpls_label_t next_hop_via_label,
531 mpls_label_t * next_hop_out_label_stack)
532{
533 vnet_classify_main_t *cm = &vnet_classify_main;
534 fib_route_path_flags_t path_flags = FIB_ROUTE_PATH_FLAG_NONE;
535 fib_route_path_t path = {
536 .frp_proto = (next_hop_proto_is_ip4 ?
537 FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6),
538 .frp_addr = (NULL == next_hop ? zero_addr : *next_hop),
539 .frp_sw_if_index = next_hop_sw_if_index,
540 .frp_fib_index = next_hop_fib_index,
541 .frp_weight = next_hop_weight,
542 .frp_label_stack = next_hop_out_label_stack,
543 };
544 fib_route_path_t *paths = NULL;
545
546 if (MPLS_LABEL_INVALID != next_hop_via_label)
547 {
548 path.frp_proto = FIB_PROTOCOL_MPLS;
549 path.frp_local_label = next_hop_via_label;
550 }
551 if (is_resolve_host)
552 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
553 if (is_resolve_attached)
554 path_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
555
556 path.frp_flags = path_flags;
557
558 if (is_multipath)
559 {
560 stats_dslock_with_hint (1 /* release hint */ , 10 /* tag */ );
561
562
563 vec_add1 (paths, path);
564
565 if (is_add)
566 fib_table_entry_path_add2 (fib_index,
567 prefix,
568 FIB_SOURCE_API,
569 FIB_ENTRY_FLAG_NONE, paths);
570 else
571 fib_table_entry_path_remove2 (fib_index,
572 prefix, FIB_SOURCE_API, paths);
573
574 vec_free (paths);
575 stats_dsunlock ();
576 return 0;
577 }
578
579 stats_dslock_with_hint (1 /* release hint */ , 2 /* tag */ );
580
581 if (is_drop || is_local || is_classify || is_unreach || is_prohibit)
582 {
583 /*
584 * special route types that link directly to the adj
585 */
586 if (is_add)
587 {
588 dpo_id_t dpo = DPO_INVALID;
589 dpo_proto_t dproto;
590
591 dproto = fib_proto_to_dpo (prefix->fp_proto);
592
593 if (is_drop)
594 ip_null_dpo_add_and_lock (dproto, IP_NULL_ACTION_NONE, &dpo);
595 else if (is_local)
596 receive_dpo_add_or_lock (dproto, ~0, NULL, &dpo);
597 else if (is_unreach)
598 ip_null_dpo_add_and_lock (dproto,
599 IP_NULL_ACTION_SEND_ICMP_UNREACH, &dpo);
600 else if (is_prohibit)
601 ip_null_dpo_add_and_lock (dproto,
602 IP_NULL_ACTION_SEND_ICMP_PROHIBIT,
603 &dpo);
604 else if (is_classify)
605 {
606 if (pool_is_free_index (cm->tables,
607 ntohl (classify_table_index)))
608 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500609 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500610 return VNET_API_ERROR_NO_SUCH_TABLE;
611 }
612
613 dpo_set (&dpo, DPO_CLASSIFY, dproto,
614 classify_dpo_create (dproto,
615 ntohl (classify_table_index)));
616 }
617 else
618 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500619 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500620 return VNET_API_ERROR_NO_SUCH_TABLE;
621 }
622
623 fib_table_entry_special_dpo_update (fib_index,
624 prefix,
625 FIB_SOURCE_API,
626 FIB_ENTRY_FLAG_EXCLUSIVE, &dpo);
627 dpo_reset (&dpo);
628 }
629 else
630 {
631 fib_table_entry_special_remove (fib_index, prefix, FIB_SOURCE_API);
632 }
633 }
634 else
635 {
636 if (is_add)
637 {
638 vec_add1 (paths, path);
639 fib_table_entry_update (fib_index,
640 prefix,
641 FIB_SOURCE_API, FIB_ENTRY_FLAG_NONE, paths);
642 vec_free (paths);
643 }
644 else
645 {
646 fib_table_entry_delete (fib_index, prefix, FIB_SOURCE_API);
647 }
648 }
649
Dave Barachd7cb1b52016-12-09 09:52:16 -0500650 stats_dsunlock ();
Dave Barachb5e8a772016-12-06 12:04:42 -0500651 return (0);
652}
653
654int
655add_del_route_check (fib_protocol_t table_proto,
656 u32 table_id,
657 u32 next_hop_sw_if_index,
658 fib_protocol_t next_hop_table_proto,
659 u32 next_hop_table_id,
660 u8 create_missing_tables,
661 u32 * fib_index, u32 * next_hop_fib_index)
662{
663 vnet_main_t *vnm = vnet_get_main ();
664
665 *fib_index = fib_table_find (table_proto, ntohl (table_id));
666 if (~0 == *fib_index)
667 {
668 if (create_missing_tables)
669 {
670 *fib_index = fib_table_find_or_create_and_lock (table_proto,
671 ntohl (table_id));
672 }
673 else
674 {
675 /* No such VRF, and we weren't asked to create one */
676 return VNET_API_ERROR_NO_SUCH_FIB;
677 }
678 }
679
680 if (~0 != ntohl (next_hop_sw_if_index))
681 {
682 if (pool_is_free_index (vnm->interface_main.sw_interfaces,
683 ntohl (next_hop_sw_if_index)))
684 {
685 return VNET_API_ERROR_NO_MATCHING_INTERFACE;
686 }
687 }
688 else
689 {
690 *next_hop_fib_index = fib_table_find (next_hop_table_proto,
691 ntohl (next_hop_table_id));
692
693 if (~0 == *next_hop_fib_index)
694 {
695 if (create_missing_tables)
696 {
697 *next_hop_fib_index =
698 fib_table_find_or_create_and_lock (next_hop_table_proto,
699 ntohl (next_hop_table_id));
700 }
701 else
702 {
703 /* No such VRF, and we weren't asked to create one */
704 return VNET_API_ERROR_NO_SUCH_FIB;
705 }
706 }
707 }
708
709 return (0);
710}
711
712static int
713ip4_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
714{
715 u32 fib_index, next_hop_fib_index;
716 mpls_label_t *label_stack = NULL;
717 int rv, ii, n_labels;;
718
719 rv = add_del_route_check (FIB_PROTOCOL_IP4,
720 mp->table_id,
721 mp->next_hop_sw_if_index,
722 FIB_PROTOCOL_IP4,
723 mp->next_hop_table_id,
724 mp->create_vrf_if_needed,
725 &fib_index, &next_hop_fib_index);
726
727 if (0 != rv)
728 return (rv);
729
730 fib_prefix_t pfx = {
731 .fp_len = mp->dst_address_length,
732 .fp_proto = FIB_PROTOCOL_IP4,
733 };
734 clib_memcpy (&pfx.fp_addr.ip4, mp->dst_address, sizeof (pfx.fp_addr.ip4));
735
736 ip46_address_t nh;
737 memset (&nh, 0, sizeof (nh));
738 memcpy (&nh.ip4, mp->next_hop_address, sizeof (nh.ip4));
739
740 n_labels = mp->next_hop_n_out_labels;
741 if (n_labels == 0)
742 ;
743 else if (1 == n_labels)
744 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
745 else
746 {
747 vec_validate (label_stack, n_labels - 1);
748 for (ii = 0; ii < n_labels; ii++)
749 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
750 }
751
752 return (add_del_route_t_handler (mp->is_multipath,
753 mp->is_add,
754 mp->is_drop,
755 mp->is_unreach,
756 mp->is_prohibit,
757 mp->is_local,
758 mp->is_classify,
759 mp->classify_table_index,
760 mp->is_resolve_host,
761 mp->is_resolve_attached,
762 fib_index, &pfx, 1,
763 &nh,
764 ntohl (mp->next_hop_sw_if_index),
765 next_hop_fib_index,
766 mp->next_hop_weight,
767 ntohl (mp->next_hop_via_label),
768 label_stack));
769}
770
771static int
772ip6_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
773{
774 u32 fib_index, next_hop_fib_index;
775 mpls_label_t *label_stack = NULL;
776 int rv, ii, n_labels;;
777
778 rv = add_del_route_check (FIB_PROTOCOL_IP6,
779 mp->table_id,
780 mp->next_hop_sw_if_index,
781 FIB_PROTOCOL_IP6,
782 mp->next_hop_table_id,
783 mp->create_vrf_if_needed,
784 &fib_index, &next_hop_fib_index);
785
786 if (0 != rv)
787 return (rv);
788
789 fib_prefix_t pfx = {
790 .fp_len = mp->dst_address_length,
791 .fp_proto = FIB_PROTOCOL_IP6,
792 };
793 clib_memcpy (&pfx.fp_addr.ip6, mp->dst_address, sizeof (pfx.fp_addr.ip6));
794
795 ip46_address_t nh;
796 memset (&nh, 0, sizeof (nh));
797 memcpy (&nh.ip6, mp->next_hop_address, sizeof (nh.ip6));
798
799 n_labels = mp->next_hop_n_out_labels;
800 if (n_labels == 0)
801 ;
802 else if (1 == n_labels)
803 vec_add1 (label_stack, ntohl (mp->next_hop_out_label_stack[0]));
804 else
805 {
806 vec_validate (label_stack, n_labels - 1);
807 for (ii = 0; ii < n_labels; ii++)
808 label_stack[ii] = ntohl (mp->next_hop_out_label_stack[ii]);
809 }
810
811 return (add_del_route_t_handler (mp->is_multipath,
812 mp->is_add,
813 mp->is_drop,
814 mp->is_unreach,
815 mp->is_prohibit,
816 mp->is_local,
817 mp->is_classify,
818 mp->classify_table_index,
819 mp->is_resolve_host,
820 mp->is_resolve_attached,
821 fib_index, &pfx, 0,
822 &nh, ntohl (mp->next_hop_sw_if_index),
823 next_hop_fib_index,
824 mp->next_hop_weight,
825 ntohl (mp->next_hop_via_label),
826 label_stack));
827}
828
829void
830vl_api_ip_add_del_route_t_handler (vl_api_ip_add_del_route_t * mp)
831{
832 vl_api_ip_add_del_route_reply_t *rmp;
833 int rv;
834 vnet_main_t *vnm = vnet_get_main ();
835
836 vnm->api_errno = 0;
837
838 if (mp->is_ipv6)
839 rv = ip6_add_del_route_t_handler (mp);
840 else
841 rv = ip4_add_del_route_t_handler (mp);
842
843 rv = (rv == 0) ? vnm->api_errno : rv;
844
845 REPLY_MACRO (VL_API_IP_ADD_DEL_ROUTE_REPLY);
846}
847
848static void
849send_ip_details (vpe_api_main_t * am,
850 unix_shared_memory_queue_t * q, u32 sw_if_index, u32 context)
851{
852 vl_api_ip_details_t *mp;
853
854 mp = vl_msg_api_alloc (sizeof (*mp));
855 memset (mp, 0, sizeof (*mp));
856 mp->_vl_msg_id = ntohs (VL_API_IP_DETAILS);
857
858 mp->sw_if_index = ntohl (sw_if_index);
859 mp->context = context;
860
861 vl_msg_api_send_shmem (q, (u8 *) & mp);
862}
863
864static void
865send_ip_address_details (vpe_api_main_t * am,
866 unix_shared_memory_queue_t * q,
867 u8 * ip, u16 prefix_length, u8 is_ipv6, u32 context)
868{
869 vl_api_ip_address_details_t *mp;
870
871 mp = vl_msg_api_alloc (sizeof (*mp));
872 memset (mp, 0, sizeof (*mp));
873 mp->_vl_msg_id = ntohs (VL_API_IP_ADDRESS_DETAILS);
874
875 if (is_ipv6)
876 {
877 clib_memcpy (&mp->ip, ip, sizeof (mp->ip));
878 }
879 else
880 {
881 u32 *tp = (u32 *) mp->ip;
882 *tp = *(u32 *) ip;
883 }
884 mp->prefix_length = prefix_length;
885 mp->context = context;
886
887 vl_msg_api_send_shmem (q, (u8 *) & mp);
888}
889
890static void
891vl_api_ip_address_dump_t_handler (vl_api_ip_address_dump_t * mp)
892{
893 vpe_api_main_t *am = &vpe_api_main;
894 unix_shared_memory_queue_t *q;
895 ip6_address_t *r6;
896 ip4_address_t *r4;
897 ip6_main_t *im6 = &ip6_main;
898 ip4_main_t *im4 = &ip4_main;
899 ip_lookup_main_t *lm6 = &im6->lookup_main;
900 ip_lookup_main_t *lm4 = &im4->lookup_main;
901 ip_interface_address_t *ia = 0;
902 u32 sw_if_index = ~0;
903 int rv __attribute__ ((unused)) = 0;
904
905 VALIDATE_SW_IF_INDEX (mp);
906
907 sw_if_index = ntohl (mp->sw_if_index);
908
909 q = vl_api_client_index_to_input_queue (mp->client_index);
910 if (q == 0)
911 return;
912
Dave Barachb5e8a772016-12-06 12:04:42 -0500913 if (mp->is_ipv6)
914 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500915 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500916 foreach_ip_interface_address (lm6, ia, sw_if_index,
917 1 /* honor unnumbered */,
918 ({
919 r6 = ip_interface_address_get_address (lm6, ia);
920 u16 prefix_length = ia->address_length;
921 send_ip_address_details(am, q, (u8*)r6, prefix_length, 1, mp->context);
922 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500923 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500924 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500925 else
926 {
Dave Barachd7cb1b52016-12-09 09:52:16 -0500927 /* *INDENT-OFF* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500928 foreach_ip_interface_address (lm4, ia, sw_if_index,
929 1 /* honor unnumbered */,
930 ({
931 r4 = ip_interface_address_get_address (lm4, ia);
932 u16 prefix_length = ia->address_length;
933 send_ip_address_details(am, q, (u8*)r4, prefix_length, 0, mp->context);
934 }));
Dave Barachd7cb1b52016-12-09 09:52:16 -0500935 /* *INDENT-ON* */
Dave Barachb5e8a772016-12-06 12:04:42 -0500936 }
Dave Barachb5e8a772016-12-06 12:04:42 -0500937 BAD_SW_IF_INDEX_LABEL;
938}
939
940static void
941vl_api_ip_dump_t_handler (vl_api_ip_dump_t * mp)
942{
943 vpe_api_main_t *am = &vpe_api_main;
944 vnet_main_t *vnm = vnet_get_main ();
945 vlib_main_t *vm = vlib_get_main ();
946 vnet_interface_main_t *im = &vnm->interface_main;
947 unix_shared_memory_queue_t *q;
948 vnet_sw_interface_t *si, *sorted_sis;
949 u32 sw_if_index = ~0;
950
951 q = vl_api_client_index_to_input_queue (mp->client_index);
952 if (q == 0)
953 {
954 return;
955 }
956
957 /* Gather interfaces. */
958 sorted_sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
959 _vec_len (sorted_sis) = 0;
960 /* *INDENT-OFF* */
961 pool_foreach (si, im->sw_interfaces,
962 ({
963 vec_add1 (sorted_sis, si[0]);
964 }));
965 /* *INDENT-ON* */
966
967 vec_foreach (si, sorted_sis)
968 {
969 if (!(si->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED))
970 {
971 if (mp->is_ipv6 && !ip6_interface_enabled (vm, si->sw_if_index))
972 {
973 continue;
974 }
975 sw_if_index = si->sw_if_index;
976 send_ip_details (am, q, sw_if_index, mp->context);
977 }
978 }
979}
980
981static void
982set_ip6_flow_hash (vl_api_set_ip_flow_hash_t * mp)
983{
984 vl_api_set_ip_flow_hash_reply_t *rmp;
985 int rv = VNET_API_ERROR_UNIMPLEMENTED;
986
987 clib_warning ("unimplemented...");
988
989 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
990}
991
992static void
993set_ip4_flow_hash (vl_api_set_ip_flow_hash_t * mp)
994{
995 vl_api_set_ip_flow_hash_reply_t *rmp;
996 int rv;
997 u32 table_id;
998 flow_hash_config_t flow_hash_config = 0;
999
1000 table_id = ntohl (mp->vrf_id);
1001
1002#define _(a,b) if (mp->a) flow_hash_config |= b;
1003 foreach_flow_hash_bit;
1004#undef _
1005
1006 rv = vnet_set_ip4_flow_hash (table_id, flow_hash_config);
1007
1008 REPLY_MACRO (VL_API_SET_IP_FLOW_HASH_REPLY);
1009}
1010
1011
1012static void
1013vl_api_set_ip_flow_hash_t_handler (vl_api_set_ip_flow_hash_t * mp)
1014{
1015 if (mp->is_ipv6 == 0)
1016 set_ip4_flow_hash (mp);
1017 else
1018 set_ip6_flow_hash (mp);
1019}
1020
1021static void
1022 vl_api_sw_interface_ip6nd_ra_config_t_handler
1023 (vl_api_sw_interface_ip6nd_ra_config_t * mp)
1024{
1025 vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
1026 vlib_main_t *vm = vlib_get_main ();
1027 int rv = 0;
1028 u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
1029 default_router;
1030
1031 is_no = mp->is_no == 1;
1032 suppress = mp->suppress == 1;
1033 managed = mp->managed == 1;
1034 other = mp->other == 1;
1035 ll_option = mp->ll_option == 1;
1036 send_unicast = mp->send_unicast == 1;
1037 cease = mp->cease == 1;
1038 default_router = mp->default_router == 1;
1039
1040 VALIDATE_SW_IF_INDEX (mp);
1041
1042 rv = ip6_neighbor_ra_config (vm, ntohl (mp->sw_if_index),
1043 suppress, managed, other,
1044 ll_option, send_unicast, cease,
1045 default_router, ntohl (mp->lifetime),
1046 ntohl (mp->initial_count),
1047 ntohl (mp->initial_interval),
1048 ntohl (mp->max_interval),
1049 ntohl (mp->min_interval), is_no);
1050
1051 BAD_SW_IF_INDEX_LABEL;
1052
1053 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
1054}
1055
1056static void
1057 vl_api_sw_interface_ip6nd_ra_prefix_t_handler
1058 (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
1059{
1060 vlib_main_t *vm = vlib_get_main ();
1061 vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
1062 int rv = 0;
1063 u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
1064
1065 VALIDATE_SW_IF_INDEX (mp);
1066
1067 is_no = mp->is_no == 1;
1068 use_default = mp->use_default == 1;
1069 no_advertise = mp->no_advertise == 1;
1070 off_link = mp->off_link == 1;
1071 no_autoconfig = mp->no_autoconfig == 1;
1072 no_onlink = mp->no_onlink == 1;
1073
1074 rv = ip6_neighbor_ra_prefix (vm, ntohl (mp->sw_if_index),
1075 (ip6_address_t *) mp->address,
1076 mp->address_length, use_default,
1077 ntohl (mp->val_lifetime),
1078 ntohl (mp->pref_lifetime), no_advertise,
1079 off_link, no_autoconfig, no_onlink, is_no);
1080
1081 BAD_SW_IF_INDEX_LABEL;
1082 REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
1083}
1084
1085static void
1086 vl_api_sw_interface_ip6_enable_disable_t_handler
1087 (vl_api_sw_interface_ip6_enable_disable_t * mp)
1088{
1089 vlib_main_t *vm = vlib_get_main ();
1090 vl_api_sw_interface_ip6_enable_disable_reply_t *rmp;
1091 vnet_main_t *vnm = vnet_get_main ();
1092 int rv = 0;
1093 clib_error_t *error;
1094
1095 vnm->api_errno = 0;
1096
1097 VALIDATE_SW_IF_INDEX (mp);
1098
1099 error =
1100 (mp->enable == 1) ? enable_ip6_interface (vm,
1101 ntohl (mp->sw_if_index)) :
1102 disable_ip6_interface (vm, ntohl (mp->sw_if_index));
1103
1104 if (error)
1105 {
1106 clib_error_report (error);
1107 rv = VNET_API_ERROR_UNSPECIFIED;
1108 }
1109 else
1110 {
1111 rv = vnm->api_errno;
1112 }
1113
1114 BAD_SW_IF_INDEX_LABEL;
1115
1116 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_ENABLE_DISABLE_REPLY);
1117}
1118
1119static void
1120 vl_api_sw_interface_ip6_set_link_local_address_t_handler
1121 (vl_api_sw_interface_ip6_set_link_local_address_t * mp)
1122{
1123 vlib_main_t *vm = vlib_get_main ();
1124 vl_api_sw_interface_ip6_set_link_local_address_reply_t *rmp;
1125 int rv = 0;
1126 clib_error_t *error;
1127 vnet_main_t *vnm = vnet_get_main ();
1128
1129 vnm->api_errno = 0;
1130
1131 VALIDATE_SW_IF_INDEX (mp);
1132
1133 error = set_ip6_link_local_address (vm,
1134 ntohl (mp->sw_if_index),
Neale Ranns75152282017-01-09 01:00:45 -08001135 (ip6_address_t *) mp->address);
Dave Barachb5e8a772016-12-06 12:04:42 -05001136 if (error)
1137 {
1138 clib_error_report (error);
1139 rv = VNET_API_ERROR_UNSPECIFIED;
1140 }
1141 else
1142 {
1143 rv = vnm->api_errno;
1144 }
1145
1146 BAD_SW_IF_INDEX_LABEL;
1147
1148 REPLY_MACRO (VL_API_SW_INTERFACE_IP6_SET_LINK_LOCAL_ADDRESS_REPLY);
1149}
1150
1151
1152#define vl_msg_name_crc_list
1153#include <vnet/ip/ip.api.h>
1154#undef vl_msg_name_crc_list
1155
1156static void
1157setup_message_id_table (api_main_t * am)
1158{
1159#define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
1160 foreach_vl_msg_name_crc_ip;
1161#undef _
1162}
1163
1164static clib_error_t *
1165ip_api_hookup (vlib_main_t * vm)
1166{
1167 api_main_t *am = &api_main;
1168
1169#define _(N,n) \
1170 vl_msg_api_set_handlers(VL_API_##N, #n, \
1171 vl_api_##n##_t_handler, \
1172 vl_noop_handler, \
1173 vl_api_##n##_t_endian, \
1174 vl_api_##n##_t_print, \
1175 sizeof(vl_api_##n##_t), 1);
1176 foreach_ip_api_msg;
1177#undef _
1178
1179 /*
1180 * Set up the (msg_name, crc, message-id) table
1181 */
1182 setup_message_id_table (am);
1183
1184 return 0;
1185}
1186
1187VLIB_API_INIT_FUNCTION (ip_api_hookup);
1188
1189/*
1190 * fd.io coding-style-patch-verification: ON
1191 *
1192 * Local Variables:
1193 * eval: (c-set-style "gnu")
1194 * End:
1195 */