ikev2: add support for custom ipsec-over-udp port
[vpp.git] / src / plugins / igmp / igmp_api.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <igmp/igmp.h>
19
20 #include <vlibapi/api.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <igmp/igmp_ssm_range.h>
24
25 /* define message IDs */
26 #include <igmp/igmp.api_enum.h>
27 #include <igmp/igmp.api_types.h>
28 #include <vnet/format_fns.h>
29
30 #include <vlibapi/api_helper_macros.h>
31
32 #define IGMP_MSG_ID(_id) (_id + igmp_main.msg_id_base)
33
34 static void
35 vl_api_igmp_listen_t_handler (vl_api_igmp_listen_t * mp)
36 {
37   vlib_main_t *vm = vlib_get_main ();
38   vnet_main_t *vnm = vnet_get_main ();
39   vl_api_igmp_listen_reply_t *rmp;
40   int ii, rv = 0;
41   ip46_address_t gaddr, *saddrs = NULL;
42
43   VALIDATE_SW_IF_INDEX (&mp->group);
44
45   if ((vnet_sw_interface_get_flags (vnm, ntohl (mp->group.sw_if_index)) &&
46        VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0)
47     {
48       // FIXME - don't we clear this state on interface down ...
49       rv = VNET_API_ERROR_UNEXPECTED_INTF_STATE;
50       goto done;
51     }
52
53   clib_memset (&gaddr, 0, sizeof (gaddr));
54   clib_memcpy (&gaddr.ip4, &mp->group.gaddr, sizeof (ip4_address_t));
55
56   vec_validate (saddrs, mp->group.n_srcs - 1);
57
58   vec_foreach_index (ii, saddrs)
59   {
60     clib_memcpy (&saddrs[ii].ip4,
61                  &mp->group.saddrs[ii], sizeof (ip4_address_t));
62   }
63
64   rv = igmp_listen (vm,
65                     (mp->group.filter ?
66                      IGMP_FILTER_MODE_INCLUDE :
67                      IGMP_FILTER_MODE_EXCLUDE),
68                     ntohl (mp->group.sw_if_index), saddrs, &gaddr);
69
70   vec_free (saddrs);
71
72   BAD_SW_IF_INDEX_LABEL;
73 done:;
74   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_LISTEN_REPLY));
75 }
76
77 static void
78 vl_api_igmp_enable_disable_t_handler (vl_api_igmp_enable_disable_t * mp)
79 {
80   vl_api_igmp_enable_disable_reply_t *rmp;
81   int rv = 0;
82
83   VALIDATE_SW_IF_INDEX (mp);
84
85   rv = igmp_enable_disable (ntohl (mp->sw_if_index),
86                             mp->enable,
87                             (mp->mode ? IGMP_MODE_HOST : IGMP_MODE_ROUTER));
88
89   BAD_SW_IF_INDEX_LABEL;
90
91   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_ENABLE_DISABLE_REPLY));
92 }
93
94 static void
95 vl_api_igmp_proxy_device_add_del_t_handler (vl_api_igmp_proxy_device_add_del_t
96                                             * mp)
97 {
98   vl_api_igmp_proxy_device_add_del_reply_t *rmp;
99   int rv = 0;
100
101   VALIDATE_SW_IF_INDEX (mp);
102
103   rv =
104     igmp_proxy_device_add_del (ntohl (mp->vrf_id), ntohl (mp->sw_if_index),
105                                mp->add);
106
107   BAD_SW_IF_INDEX_LABEL;
108
109   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_REPLY));
110 }
111
112 static void
113   vl_api_igmp_proxy_device_add_del_interface_t_handler
114   (vl_api_igmp_proxy_device_add_del_interface_t * mp)
115 {
116   vl_api_igmp_proxy_device_add_del_interface_reply_t *rmp;
117   int rv = 0;
118
119   VALIDATE_SW_IF_INDEX (mp);
120
121   rv =
122     igmp_proxy_device_add_del_interface (ntohl (mp->vrf_id),
123                                          ntohl (mp->sw_if_index), mp->add);
124
125   BAD_SW_IF_INDEX_LABEL;
126
127   REPLY_MACRO (IGMP_MSG_ID
128                (VL_API_IGMP_PROXY_DEVICE_ADD_DEL_INTERFACE_REPLY));
129 }
130
131 static void
132 send_igmp_details (vl_api_registration_t * rp, igmp_main_t * im,
133                    igmp_config_t * config, igmp_group_t * group,
134                    igmp_src_t * src, u32 context)
135 {
136   vl_api_igmp_details_t *mp;
137
138   mp = vl_msg_api_alloc (sizeof (*mp));
139   clib_memset (mp, 0, sizeof (*mp));
140
141   mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
142   mp->context = context;
143   mp->sw_if_index = htonl (config->sw_if_index);
144   clib_memcpy (&mp->saddr, &src->key->ip4, sizeof (src->key->ip4));
145   clib_memcpy (&mp->gaddr, &group->key->ip4, sizeof (group->key->ip4));
146
147   vl_api_send_msg (rp, (u8 *) mp);
148 }
149
150 static void
151 igmp_config_dump (igmp_main_t * im,
152                   vl_api_registration_t * rp,
153                   u32 context, igmp_config_t * config)
154 {
155   igmp_group_t *group;
156   igmp_src_t *src;
157
158   /* *INDENT-OFF* */
159   FOR_EACH_GROUP (group, config,
160     ({
161       FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
162         ({
163           send_igmp_details (rp, im, config, group, src, context);
164         }));
165     }));
166   /* *INDENT-ON* */
167 }
168
169 static void
170 vl_api_igmp_dump_t_handler (vl_api_igmp_dump_t * mp)
171 {
172   igmp_main_t *im = &igmp_main;
173   igmp_config_t *config;
174   u32 sw_if_index;
175   vl_api_registration_t *rp;
176
177   rp = vl_api_client_index_to_registration (mp->client_index);
178   if (rp == 0)
179     return;
180
181   sw_if_index = ntohl (mp->sw_if_index);
182   if (~0 == sw_if_index)
183     {
184       /* *INDENT-OFF* */
185       pool_foreach (config, im->configs,
186         ({
187           igmp_config_dump(im, rp, mp->context, config);
188         }));
189       /* *INDENT-ON* */
190     }
191   else
192     {
193       config = igmp_config_lookup (sw_if_index);
194       if (config)
195         {
196           igmp_config_dump (im, rp, mp->context, config);
197         }
198     }
199 }
200
201 static void
202 vl_api_igmp_clear_interface_t_handler (vl_api_igmp_clear_interface_t * mp)
203 {
204   vl_api_igmp_clear_interface_reply_t *rmp;
205   igmp_config_t *config;
206   int rv = 0;
207
208   config = igmp_config_lookup (ntohl (mp->sw_if_index));
209   if (config)
210     igmp_clear_config (config);
211
212   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_CLEAR_INTERFACE_REPLY));
213 }
214
215 static vl_api_group_prefix_type_t
216 igmp_group_type_int_to_api (igmp_group_prefix_type_t t)
217 {
218   switch (t)
219     {
220     case IGMP_GROUP_PREFIX_TYPE_ASM:
221       return (htonl (ASM));
222     case IGMP_GROUP_PREFIX_TYPE_SSM:
223       return (htonl (SSM));
224     }
225
226   return (SSM);
227 }
228
229 static igmp_group_prefix_type_t
230 igmp_group_type_api_to_int (vl_api_group_prefix_type_t t)
231 {
232   switch (htonl (t))
233     {
234     case ASM:
235       return (IGMP_GROUP_PREFIX_TYPE_ASM);
236     case SSM:
237       return (IGMP_GROUP_PREFIX_TYPE_SSM);
238     }
239
240   return (IGMP_GROUP_PREFIX_TYPE_SSM);
241 }
242
243 static void
244 vl_api_igmp_group_prefix_set_t_handler (vl_api_igmp_group_prefix_set_t * mp)
245 {
246   vl_api_igmp_group_prefix_set_reply_t *rmp;
247   fib_prefix_t pfx;
248   int rv = 0;
249
250   ip_prefix_decode (&mp->gp.prefix, &pfx);
251   igmp_group_prefix_set (&pfx, igmp_group_type_api_to_int (mp->gp.type));
252
253   REPLY_MACRO (IGMP_MSG_ID (VL_API_IGMP_GROUP_PREFIX_SET_REPLY));
254 }
255
256 typedef struct igmp_ssm_range_walk_ctx_t_
257 {
258   vl_api_registration_t *rp;
259   u32 context;
260 } igmp_ssm_range_walk_ctx_t;
261
262 static walk_rc_t
263 igmp_ssm_range_walk_dump (const fib_prefix_t * pfx,
264                           igmp_group_prefix_type_t type, void *args)
265 {
266   igmp_ssm_range_walk_ctx_t *ctx = args;
267   vl_api_igmp_group_prefix_details_t *mp;
268
269   mp = vl_msg_api_alloc (sizeof (*mp));
270   clib_memset (mp, 0, sizeof (*mp));
271
272   mp->_vl_msg_id = htons (IGMP_MSG_ID (VL_API_IGMP_DETAILS));
273   mp->context = ctx->context;
274   mp->gp.type = igmp_group_type_int_to_api (type);
275   ip_prefix_encode (pfx, &mp->gp.prefix);
276
277   vl_api_send_msg (ctx->rp, (u8 *) mp);
278
279   return (WALK_CONTINUE);
280 }
281
282 static void
283 vl_api_igmp_group_prefix_dump_t_handler (vl_api_igmp_dump_t * mp)
284 {
285   vl_api_registration_t *rp;
286
287   rp = vl_api_client_index_to_registration (mp->client_index);
288   if (rp == 0)
289     return;
290
291   igmp_ssm_range_walk_ctx_t ctx = {
292     .rp = rp,
293     .context = mp->context,
294   };
295
296   igmp_ssm_range_walk (igmp_ssm_range_walk_dump, &ctx);
297 }
298
299 static vpe_client_registration_t *
300 igmp_api_client_lookup (igmp_main_t * im, u32 client_index)
301 {
302   uword *p;
303   vpe_client_registration_t *api_client = NULL;
304
305   p = hash_get (im->igmp_api_client_by_client_index, client_index);
306   if (p)
307     api_client = vec_elt_at_index (im->api_clients, p[0]);
308
309   return api_client;
310 }
311
312 static void
313 vl_api_want_igmp_events_t_handler (vl_api_want_igmp_events_t * mp)
314 {
315   igmp_main_t *im = &igmp_main;
316   vpe_client_registration_t *api_client;
317   vl_api_want_igmp_events_reply_t *rmp;
318   int rv = 0;
319
320   api_client = igmp_api_client_lookup (im, mp->client_index);
321   if (api_client)
322     {
323       if (mp->enable)
324         {
325           rv = VNET_API_ERROR_INVALID_REGISTRATION;
326           goto done;
327         }
328       hash_unset (im->igmp_api_client_by_client_index,
329                   api_client->client_index);
330       pool_put (im->api_clients, api_client);
331       goto done;
332     }
333   if (mp->enable)
334     {
335       pool_get (im->api_clients, api_client);
336       clib_memset (api_client, 0, sizeof (vpe_client_registration_t));
337       api_client->client_index = mp->client_index;
338       api_client->client_pid = mp->pid;
339       hash_set (im->igmp_api_client_by_client_index,
340                 mp->client_index, api_client - im->api_clients);
341       goto done;
342     }
343   rv = VNET_API_ERROR_INVALID_REGISTRATION;
344
345 done:
346   REPLY_MACRO (VL_API_WANT_IGMP_EVENTS_REPLY + im->msg_id_base);
347 }
348
349 static clib_error_t *
350 want_igmp_events_reaper (u32 client_index)
351 {
352   igmp_main_t *im = &igmp_main;
353   vpe_client_registration_t *api_client;
354   uword *p;
355
356   p = hash_get (im->igmp_api_client_by_client_index, client_index);
357
358   if (p)
359     {
360       api_client = pool_elt_at_index (im->api_clients, p[0]);
361       pool_put (im->api_clients, api_client);
362       hash_unset (im->igmp_api_client_by_client_index, client_index);
363     }
364   return (NULL);
365 }
366
367 VL_MSG_API_REAPER_FUNCTION (want_igmp_events_reaper);
368
369 void
370 send_igmp_event (vl_api_registration_t * rp,
371                  igmp_filter_mode_t filter,
372                  u32 sw_if_index,
373                  const ip46_address_t * saddr, const ip46_address_t * gaddr)
374 {
375   vl_api_igmp_event_t *mp = vl_msg_api_alloc (sizeof (*mp));
376   clib_memset (mp, 0, sizeof (*mp));
377
378   mp->_vl_msg_id = ntohs ((VL_API_IGMP_EVENT) + igmp_main.msg_id_base);
379   mp->sw_if_index = htonl (sw_if_index);
380   mp->filter = htonl (filter);
381   clib_memcpy (&mp->saddr, &saddr->ip4, sizeof (ip4_address_t));
382   clib_memcpy (&mp->gaddr, &gaddr->ip4, sizeof (ip4_address_t));
383
384   vl_api_send_msg (rp, (u8 *) mp);
385 }
386
387 void
388 igmp_event (igmp_filter_mode_t filter,
389             u32 sw_if_index,
390             const ip46_address_t * saddr, const ip46_address_t * gaddr)
391 {
392   vpe_client_registration_t *api_client;
393   vl_api_registration_t *rp;
394   igmp_main_t *im;
395
396   im = &igmp_main;
397
398   IGMP_DBG ("event: (%U, %U) %U %U",
399             format_ip46_address, saddr, IP46_TYPE_ANY,
400             format_ip46_address, saddr, IP46_TYPE_ANY,
401             format_vnet_sw_if_index_name,
402             vnet_get_main (), sw_if_index, format_igmp_filter_mode, filter);
403
404
405   /* *INDENT-OFF* */
406   pool_foreach (api_client, im->api_clients,
407     ({
408       rp = vl_api_client_index_to_registration (api_client->client_index);
409       if (rp)
410         send_igmp_event (rp, filter, sw_if_index, saddr, gaddr);
411     }));
412   /* *INDENT-ON* */
413 }
414
415 /* Set up the API message handling tables */
416 #include <igmp/igmp.api.c>
417 static clib_error_t *
418 igmp_plugin_api_hookup (vlib_main_t * vm)
419 {
420   igmp_main_t *im = &igmp_main;
421
422   /* Ask for a correctly-sized block of API message decode slots */
423   im->msg_id_base = setup_message_id_table ();
424
425   return 0;
426 }
427
428 VLIB_API_INIT_FUNCTION (igmp_plugin_api_hookup);
429
430 /*
431  * fd.io coding-style-patch-verification: ON
432  *
433  * Local Variables:
434  * eval: (c-set-style "gnu")
435  * End:
436  */