fib: A 16-8-8 and a 8-8-8-8 versions of an ip4_fib_t
[vpp.git] / src / vnet / ip6-nd / ip6_nd_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 <stddef.h>
21
22 #include <vnet/ip6-nd/ip6_nd.h>
23 #include <vnet/ip6-nd/ip6_ra.h>
24
25 #include <vnet/fib/fib_table.h>
26 #include <vnet/ip/ip_types_api.h>
27
28 #include <vpp/app/version.h>
29
30 #include <vlibapi/api.h>
31 #include <vlibmemory/api.h>
32
33 /* define message IDs */
34 #include <vnet/format_fns.h>
35 #include <vnet/ip6-nd/ip6_nd.api_enum.h>
36 #include <vnet/ip6-nd/ip6_nd.api_types.h>
37
38 /**
39  * Base message ID fot the plugin
40  */
41 static u32 ip6_nd_base_msg_id;
42 #define REPLY_MSG_ID_BASE ip6_nd_base_msg_id
43
44 #include <vlibapi/api_helper_macros.h>
45
46 static void
47 send_ip6nd_proxy_details (vl_api_registration_t * reg,
48                           u32 context,
49                           const ip46_address_t * addr, u32 sw_if_index)
50 {
51   vl_api_ip6nd_proxy_details_t *mp;
52
53   mp = vl_msg_api_alloc (sizeof (*mp));
54   clib_memset (mp, 0, sizeof (*mp));
55   mp->_vl_msg_id = ntohs (VL_API_IP6ND_PROXY_DETAILS);
56   mp->context = context;
57   mp->sw_if_index = htonl (sw_if_index);
58
59   ip6_address_encode (&addr->ip6, mp->ip);
60
61   vl_api_send_msg (reg, (u8 *) mp);
62 }
63
64 typedef struct api_ip6nd_proxy_fib_table_walk_ctx_t_
65 {
66   u32 *indices;
67 } api_ip6nd_proxy_fib_table_walk_ctx_t;
68
69 static fib_table_walk_rc_t
70 api_ip6nd_proxy_fib_table_walk (fib_node_index_t fei, void *arg)
71 {
72   api_ip6nd_proxy_fib_table_walk_ctx_t *ctx = arg;
73
74   if (fib_entry_is_sourced (fei, FIB_SOURCE_IP6_ND_PROXY))
75     {
76       vec_add1 (ctx->indices, fei);
77     }
78
79   return (FIB_TABLE_WALK_CONTINUE);
80 }
81
82 static void
83 vl_api_ip6nd_proxy_dump_t_handler (vl_api_ip6nd_proxy_dump_t * mp)
84 {
85   ip6_main_t *im6 = &ip6_main;
86   u32 fib_index;
87   api_ip6nd_proxy_fib_table_walk_ctx_t ctx = {
88     .indices = NULL,
89   };
90   fib_node_index_t *feip;
91   const fib_prefix_t *pfx;
92   vl_api_registration_t *reg;
93
94   reg = vl_api_client_index_to_registration (mp->client_index);
95   if (!reg)
96     return;
97
98   /* *INDENT-OFF* */
99   pool_foreach_index (fib_index, im6->fibs)
100     {
101       fib_table_walk (fib_index, FIB_PROTOCOL_IP6,
102                       api_ip6nd_proxy_fib_table_walk, &ctx);
103     }
104   /* *INDENT-ON* */
105
106   vec_sort_with_function (ctx.indices, fib_entry_cmp_for_sort);
107
108   vec_foreach (feip, ctx.indices)
109   {
110     pfx = fib_entry_get_prefix (*feip);
111
112     send_ip6nd_proxy_details (reg,
113                               mp->context,
114                               &pfx->fp_addr,
115                               fib_entry_get_resolving_interface (*feip));
116   }
117
118   vec_free (ctx.indices);
119 }
120
121 static void
122 vl_api_ip6nd_proxy_add_del_t_handler (vl_api_ip6nd_proxy_add_del_t * mp)
123 {
124   vl_api_ip6nd_proxy_add_del_reply_t *rmp;
125   ip6_address_t ip6;
126   int rv = 0;
127
128   VALIDATE_SW_IF_INDEX (mp);
129
130   ip6_address_decode (mp->ip, &ip6);
131   if (mp->is_add)
132     rv = ip6_nd_proxy_add (ntohl (mp->sw_if_index), &ip6);
133   else
134     rv = ip6_nd_proxy_del (ntohl (mp->sw_if_index), &ip6);
135
136   BAD_SW_IF_INDEX_LABEL;
137   REPLY_MACRO (VL_API_IP6ND_PROXY_ADD_DEL_REPLY);
138 }
139
140 static void
141   vl_api_sw_interface_ip6nd_ra_config_t_handler
142   (vl_api_sw_interface_ip6nd_ra_config_t * mp)
143 {
144   vl_api_sw_interface_ip6nd_ra_config_reply_t *rmp;
145   vlib_main_t *vm = vlib_get_main ();
146   int rv = 0;
147   u8 is_no, suppress, managed, other, ll_option, send_unicast, cease,
148     default_router;
149
150   is_no = mp->is_no == 1;
151   suppress = mp->suppress == 1;
152   managed = mp->managed == 1;
153   other = mp->other == 1;
154   ll_option = mp->ll_option == 1;
155   send_unicast = mp->send_unicast == 1;
156   cease = mp->cease == 1;
157   default_router = mp->default_router == 1;
158
159   VALIDATE_SW_IF_INDEX (mp);
160
161   rv = ip6_ra_config (vm, ntohl (mp->sw_if_index),
162                       suppress, managed, other,
163                       ll_option, send_unicast, cease,
164                       default_router, ntohl (mp->lifetime),
165                       ntohl (mp->initial_count),
166                       ntohl (mp->initial_interval),
167                       ntohl (mp->max_interval),
168                       ntohl (mp->min_interval), is_no);
169
170   BAD_SW_IF_INDEX_LABEL;
171
172   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_CONFIG_REPLY);
173 }
174
175 static void
176   vl_api_sw_interface_ip6nd_ra_prefix_t_handler
177   (vl_api_sw_interface_ip6nd_ra_prefix_t * mp)
178 {
179   vlib_main_t *vm = vlib_get_main ();
180   vl_api_sw_interface_ip6nd_ra_prefix_reply_t *rmp;
181   fib_prefix_t pfx;
182   int rv = 0;
183   u8 is_no, use_default, no_advertise, off_link, no_autoconfig, no_onlink;
184
185   VALIDATE_SW_IF_INDEX (mp);
186
187   ip_prefix_decode (&mp->prefix, &pfx);
188   is_no = mp->is_no == 1;
189   use_default = mp->use_default == 1;
190   no_advertise = mp->no_advertise == 1;
191   off_link = mp->off_link == 1;
192   no_autoconfig = mp->no_autoconfig == 1;
193   no_onlink = mp->no_onlink == 1;
194
195   rv = ip6_ra_prefix (vm, ntohl (mp->sw_if_index),
196                       &pfx.fp_addr.ip6,
197                       pfx.fp_len, use_default,
198                       ntohl (mp->val_lifetime),
199                       ntohl (mp->pref_lifetime), no_advertise,
200                       off_link, no_autoconfig, no_onlink, is_no);
201
202   BAD_SW_IF_INDEX_LABEL;
203   REPLY_MACRO (VL_API_SW_INTERFACE_IP6ND_RA_PREFIX_REPLY);
204 }
205
206 static void
207   vl_api_ip6nd_send_router_solicitation_t_handler
208   (vl_api_ip6nd_send_router_solicitation_t * mp)
209 {
210   vl_api_ip6nd_send_router_solicitation_reply_t *rmp;
211   icmp6_send_router_solicitation_params_t params;
212   vlib_main_t *vm = vlib_get_main ();
213   int rv = 0;
214
215   VALIDATE_SW_IF_INDEX (mp);
216
217   BAD_SW_IF_INDEX_LABEL;
218   REPLY_MACRO (VL_API_IP6ND_SEND_ROUTER_SOLICITATION_REPLY);
219
220   if (rv != 0)
221     return;
222
223   params.irt = ntohl (mp->irt);
224   params.mrt = ntohl (mp->mrt);
225   params.mrc = ntohl (mp->mrc);
226   params.mrd = ntohl (mp->mrd);
227
228   icmp6_send_router_solicitation (vm, ntohl (mp->sw_if_index), mp->stop,
229                                   &params);
230 }
231
232 static void
233 ip6_ra_handle_report (const ip6_ra_report_t * rap)
234 {
235   /* *INDENT-OFF* */
236   vpe_client_registration_t *rp;
237
238   pool_foreach (rp, vpe_api_main.ip6_ra_events_registrations)
239    {
240     vl_api_registration_t *vl_reg;
241
242     vl_reg = vl_api_client_index_to_registration (rp->client_index);
243
244     if (vl_reg && vl_api_can_send_msg (vl_reg))
245       {
246         vl_api_ip6_ra_prefix_info_t *prefix;
247         vl_api_ip6_ra_event_t *event;
248
249         u32 event_size = (sizeof (vl_api_ip6_ra_event_t) +
250                           vec_len (rap->prefixes) *
251                           sizeof (vl_api_ip6_ra_prefix_info_t));
252         event = vl_msg_api_alloc_zero (event_size);
253
254         event->_vl_msg_id = htons (VL_API_IP6_RA_EVENT + REPLY_MSG_ID_BASE);
255         event->client_index = rp->client_index;
256         event->pid = rp->client_pid;
257         event->sw_if_index = clib_host_to_net_u32 (rap->sw_if_index);
258
259         ip6_address_encode (&rap->router_address,
260                             event->router_addr);
261
262         event->current_hop_limit = rap->current_hop_limit;
263         event->flags = rap->flags;
264         event->router_lifetime_in_sec =
265           clib_host_to_net_u16 (rap->router_lifetime_in_sec);
266         event->neighbor_reachable_time_in_msec =
267           clib_host_to_net_u32 (rap->neighbor_reachable_time_in_msec);
268         event->time_in_msec_between_retransmitted_neighbor_solicitations =
269           clib_host_to_net_u32 (rap->time_in_msec_between_retransmitted_neighbor_solicitations);
270         event->n_prefixes = clib_host_to_net_u32 (vec_len (rap->prefixes));
271
272         prefix = event->prefixes;
273           // (typeof (prefix)) event->prefixes;
274         u32 j;
275         for (j = 0; j < vec_len (rap->prefixes); j++)
276           {
277             ra_report_prefix_info_t *info = &rap->prefixes[j];
278             ip_prefix_encode(&info->prefix, &prefix->prefix);
279             prefix->flags = info->flags;
280             prefix->valid_time = clib_host_to_net_u32 (info->valid_time);
281             prefix->preferred_time =
282               clib_host_to_net_u32 (info->preferred_time);
283             prefix++;
284           }
285
286         vl_api_send_msg (vl_reg, (u8 *) event);
287       }
288   }
289   /* *INDENT-ON* */
290 }
291
292 static void
293 vl_api_want_ip6_ra_events_t_handler (vl_api_want_ip6_ra_events_t * mp)
294 {
295   vpe_api_main_t *am = &vpe_api_main;
296   vl_api_want_ip6_ra_events_reply_t *rmp;
297   int rv = 0, had_reg, have_reg;
298
299   had_reg = hash_elts (am->ip6_ra_events_registration_hash);
300   uword *p = hash_get (am->ip6_ra_events_registration_hash, mp->client_index);
301   vpe_client_registration_t *rp;
302   if (p)
303     {
304       if (mp->enable)
305         {
306           clib_warning ("pid %d: already enabled...", ntohl (mp->pid));
307           rv = VNET_API_ERROR_INVALID_REGISTRATION;
308           goto reply;
309         }
310       else
311         {
312           rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
313           pool_put (am->ip6_ra_events_registrations, rp);
314           hash_unset (am->ip6_ra_events_registration_hash, mp->client_index);
315           goto reply;
316         }
317     }
318   if (mp->enable == 0)
319     {
320       clib_warning ("pid %d: already disabled...", ntohl (mp->pid));
321       rv = VNET_API_ERROR_INVALID_REGISTRATION;
322       goto reply;
323     }
324   pool_get (am->ip6_ra_events_registrations, rp);
325   rp->client_index = mp->client_index;
326   rp->client_pid = ntohl (mp->pid);
327   hash_set (am->ip6_ra_events_registration_hash, rp->client_index,
328             rp - am->ip6_ra_events_registrations);
329
330 reply:
331   have_reg = hash_elts (am->ip6_ra_events_registration_hash);
332
333   if (!had_reg && have_reg)
334     ip6_ra_report_register (ip6_ra_handle_report);
335   else if (had_reg && !have_reg)
336     ip6_ra_report_unregister (ip6_ra_handle_report);
337
338   REPLY_MACRO (VL_API_WANT_IP6_RA_EVENTS_REPLY);
339 }
340
341 static clib_error_t *
342 want_ip6_ra_events_reaper (u32 client_index)
343 {
344   vpe_api_main_t *am = &vpe_api_main;
345   vpe_client_registration_t *rp;
346   uword *p;
347
348   p = hash_get (am->ip6_ra_events_registration_hash, client_index);
349
350   if (p)
351     {
352       rp = pool_elt_at_index (am->ip6_ra_events_registrations, p[0]);
353       pool_put (am->ip6_ra_events_registrations, rp);
354       hash_unset (am->ip6_ra_events_registration_hash, client_index);
355     }
356   return (NULL);
357 }
358
359 VL_MSG_API_REAPER_FUNCTION (want_ip6_ra_events_reaper);
360
361 #include <vnet/ip6-nd/ip6_nd.api.c>
362
363 static clib_error_t *
364 ip6_nd_api_init (vlib_main_t * vm)
365 {
366   /* Ask for a correctly-sized block of API message decode slots */
367   ip6_nd_base_msg_id = setup_message_id_table ();
368
369   return 0;
370 }
371
372 VLIB_INIT_FUNCTION (ip6_nd_api_init);
373
374 /*
375  * fd.io coding-style-patch-verification: ON
376  *
377  * Local Variables:
378  * eval: (c-set-style "gnu")
379  * End:
380  */