FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
igmp_pkt.c
Go to the documentation of this file.
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_pkt.h>
19 
20 static void
22 {
23  b->current_data += l;
24  b->current_length += l;
25 }
26 
27 static vlib_buffer_t *
29 {
30  vlib_main_t *vm;
31  vlib_buffer_t *b;
32  u32 bi;
33 
34  vm = vlib_get_main ();
35 
36  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
37  return (NULL);
38 
39  b = vlib_get_buffer (vm, bi);
41 
42  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
43  b->flags |= VLIB_BUFFER_IS_TRACED;
44 
45  /* clear out stale data */
46  vnet_buffer (b)->sw_if_index[VLIB_RX] = ~0;
47 
48  /*
49  * save progress in the builder
50  */
51  vec_add1 (bk->buffers, bi);
54 
55  return (b);
56 }
57 
58 static vlib_buffer_t *
60  igmp_msg_type_t msg_type,
61  const igmp_group_t * group)
62 {
64  vlib_buffer_t *b;
65  u8 *option;
66 
67  b = igmp_pkt_get_buffer (bk);
68 
69  if (NULL == b)
70  return (NULL);
71 
72  ip4 = vlib_buffer_get_current (b);
73  clib_memset (ip4, 0, sizeof (ip4_header_t));
74  ip4->ip_version_and_header_length = 0x46;
75  ip4->ttl = 1;
76  ip4->protocol = IP_PROTOCOL_IGMP;
77  ip4->tos = 0xc0;
78 
80  bk->sw_if_index, &ip4->src_address);
81 
82  vlib_buffer_append (b, sizeof (*ip4));
83  bk->n_avail -= sizeof (*ip4);
84 
85  switch (msg_type)
86  {
87  case IGMP_MSG_REPORT:
89  break;
90  case IGMP_MSG_QUERY:
91  if (group != NULL)
92  clib_memcpy_fast (&ip4->dst_address, &group->key->ip4,
93  sizeof (ip4_address_t));
94  else
96  break;
97  }
98 
99  /* add the router alert options */
100  option = vlib_buffer_get_current (b);
101  option[0] = 0x80 | 20; // IP4_ROUTER_ALERT_OPTION;
102  option[1] = 4; // length
103  option[2] = option[3] = 0;
104 
105  vlib_buffer_append (b, 4);
106  bk->n_avail -= 4;
107 
108  return (b);
109 }
110 
111 static vlib_buffer_t *
113  const igmp_group_t * group)
114 {
116  vlib_buffer_t *b;
117 
118  b = igmp_pkt_build_ip_header (&br->base, IGMP_MSG_REPORT, group);
119 
120  if (NULL == b)
121  return (NULL);
122 
123  report = vlib_buffer_get_current (b);
124  report->header.type = IGMP_TYPE_membership_report_v3;
125  report->header.code = 0;
126  report->header.checksum = 0;
127  report->unused = 0;
128 
130  br->base.n_avail -= sizeof (igmp_membership_report_v3_t);
131  br->base.n_bytes += sizeof (igmp_membership_report_v3_t);
132 
133  return (b);
134 }
135 
136 static void
138 {
139  const igmp_config_t *config;
140  vlib_buffer_t *b;
141  vlib_main_t *vm;
142  vlib_frame_t *f;
143  u32 *to_next;
144  u32 ii;
145 
146  vm = vlib_get_main ();
147  config = igmp_config_lookup (bk->sw_if_index);
148 
149  if (NULL == config)
150  return;
151 
153  to_next = vlib_frame_vector_args (f);
154 
155  vec_foreach_index (ii, bk->buffers)
156  {
157  b = vlib_get_buffer (vm, bk->buffers[ii]);
158  vnet_buffer (b)->ip.adj_index[VLIB_TX] = config->adj_index;
159  to_next[ii] = bk->buffers[ii];
160  f->n_vectors++;
161  }
162 
164 
166  vnet_get_main (), bk->sw_if_index);
167 
168  vec_free (bk->buffers);
169  bk->buffers = 0;
170 }
171 
172 static vlib_buffer_t *
174 {
175  if (NULL == br->base.buffers)
176  return (NULL);
177 
178  return (vlib_get_buffer (vlib_get_main (),
179  br->base.buffers[vec_len (br->base.buffers) - 1]));
180 }
181 
182 static void
184 {
186  ip4_header_t *ip4;
187  vlib_buffer_t *b;
188 
190 
191  b->current_data = 0;
192 
193  ip4 = vlib_buffer_get_current (b);
194  igmp = (igmp_membership_report_v3_t *) (((u32 *) ip4) + 6);
195 
196  igmp->n_groups = clib_host_to_net_u16 (br->n_groups);
197 
198  igmp->header.checksum =
200 
201  ip4->length = clib_host_to_net_u16 (b->current_length);
202  ip4->checksum = ip4_header_checksum (ip4);
203 
204  br->base.n_bytes = br->base.n_avail = br->n_groups = 0;
205 }
206 
207 void
209 {
210  if (NULL == br->base.buffers)
211  return;
212 
214  igmp_pkt_tx (&br->base);
215 }
216 
217 static u32
219 {
220  ASSERT (IGMP_FILTER_MODE_INCLUDE == group->router_filter_mode);
221 
222  return ((hash_elts (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE]) *
223  sizeof (ip4_address_t)) + sizeof (igmp_membership_group_v3_t));
224 }
225 
228  const ip46_address_t * grp,
230 {
232  vlib_buffer_t *b;
233 
235 
236  if (br->base.n_avail < sizeof (igmp_membership_group_v3_t))
237  {
239  b = igmp_pkt_build_report_v3 (br, NULL);
240  if (NULL == b)
241  return (NULL);
242  }
243  br->base.n_avail -= sizeof (igmp_membership_group_v3_t);
244  br->base.n_bytes += sizeof (igmp_membership_group_v3_t);
245  br->n_groups++;
246  br->n_srcs = 0;
247 
248  igmp_group = vlib_buffer_get_current (b);
250 
251  igmp_group->type = type;
252  igmp_group->n_aux_u32s = 0;
253  igmp_group->n_src_addresses = 0;
254  igmp_group->group_address.as_u32 = grp->ip4.as_u32;
255 
256  return (igmp_group);
257 }
258 
259 /**
260  * 4.2.16
261  " If the set of Group Records required in a Report does not fit within
262  * the size limit of a single Report message (as determined by the MTU
263  * of the network on which it will be sent), the Group Records are sent
264  * in as many Report messages as needed to report the entire set.
265 
266  * If a single Group Record contains so many source addresses that it
267  * does not fit within the size limit of a single Report message, if its
268  * Type is not MODE_IS_EXCLUDE or CHANGE_TO_EXCLUDE_MODE, it is split
269  * into multiple Group Records, each containing a different subset of
270  * the source addresses and each sent in a separate Report message. If
271  * its Type is MODE_IS_EXCLUDE or CHANGE_TO_EXCLUDE_MODE, a single Group
272  * Record is sent, containing as many source addresses as can fit, and
273  * the remaining source addresses are not reported; though the choice of
274  * which sources to report is arbitrary, it is preferable to report the
275  * same set of sources in each subsequent report, rather than reporting
276  * different sources each time."
277  */
281  const ip46_address_t * grp,
283  const ip46_address_t * src)
284 {
285  vlib_buffer_t *b;
286 
288 
289  if (br->base.n_avail < sizeof (ip4_address_t))
290  {
291  igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs);
293  b = igmp_pkt_build_report_v3 (br, NULL);
294  if (NULL == b)
295  return (NULL);
296  igmp_group = igmp_pkt_report_v3_append_group (br, grp, type);
297  }
298 
299  igmp_group->src_addresses[br->n_srcs].as_u32 = src->ip4.as_u32;
300  br->n_srcs++;
301  br->base.n_avail -= sizeof (ip4_address_t);
302  br->base.n_bytes += sizeof (ip4_address_t);
303  vlib_buffer_append (b, sizeof (ip4_address_t));
304 
305  return (igmp_group);
306 }
307 
308 void
310  const ip46_address_t * grp,
311  const ip46_address_t * srcs,
313 {
315  const ip46_address_t *s;
316  vlib_buffer_t *b;
317 
319 
320  if (NULL == b)
321  {
322  b = igmp_pkt_build_report_v3 (br, NULL);
323  if (NULL == b)
324  /* failed to allocate buffer */
325  return;
326  }
327 
328  igmp_group = igmp_pkt_report_v3_append_group (br, grp, type);
329 
330  if (NULL == igmp_group)
331  return;
332 
333  /* *INDENT-OFF* */
334  vec_foreach(s, srcs)
335  {
336  igmp_group = igmp_pkt_report_v3_append_src(br, igmp_group,
337  grp, type, s);
338  if (NULL == igmp_group)
339  return;
340  };
341  /* *INDENT-ON* */
342 
343  igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs);
344 
345  IGMP_DBG (" ..add-group: %U", format_ip46_address, grp, IP46_TYPE_IP4);
346 }
347 
348 void
350  const igmp_group_t * group,
352 {
354  vlib_buffer_t *b;
355  igmp_src_t *src;
356 
358 
359  if (NULL == b)
360  {
361  b = igmp_pkt_build_report_v3 (br, NULL);
362  if (NULL == b)
363  /* failed to allocate buffer */
364  return;
365  }
366 
367  /*
368  * if the group won't fit in a partially full buffer, start again
369  */
370  if ((0 != br->n_groups) &&
371  (igmp_pkt_report_v3_get_size (group) > br->base.n_avail))
372  {
374  b = igmp_pkt_build_report_v3 (br, NULL);
375  if (NULL == b)
376  /* failed to allocate buffer */
377  return;
378  }
379 
380  igmp_group = igmp_pkt_report_v3_append_group (br, group->key, type);
381 
382  /* *INDENT-OFF* */
383  FOR_EACH_SRC (src, group, IGMP_FILTER_MODE_INCLUDE,
384  ({
385  igmp_group = igmp_pkt_report_v3_append_src(br, igmp_group,
386  group->key, type,
387  src->key);
388  if (NULL == igmp_group)
389  return;
390  }));
391  /* *INDENT-ON* */
392  igmp_group->n_src_addresses = clib_host_to_net_u16 (br->n_srcs);
393 
394  IGMP_DBG (" ..add-group: %U srcs:%d",
395  format_igmp_key, group->key,
396  hash_elts (group->igmp_src_by_key[IGMP_FILTER_MODE_INCLUDE]));
397 }
398 
399 void
401 {
402  clib_memset (br, 0, sizeof (*br));
404 }
405 
406 static vlib_buffer_t *
408 {
409  if (NULL == bq->base.buffers)
410  return (NULL);
411 
412  return (vlib_get_buffer (vlib_get_main (),
413  bq->base.buffers[vec_len (bq->base.buffers) - 1]));
414 }
415 
416 static vlib_buffer_t *
418  const igmp_group_t * group)
419 {
421  vlib_buffer_t *b;
422 
423  b = igmp_pkt_build_ip_header (&bq->base, IGMP_MSG_QUERY, group);
424 
425  if (NULL == b)
426  return (NULL);
427 
428  query = vlib_buffer_get_current (b);
429  query->header.type = IGMP_TYPE_membership_query;
430  query->header.code = 0;
431  query->header.checksum = 0;
432  query->qqi_code = 0;
433  query->resv_s_qrv = 0;
434 
435  if (NULL != group)
436  query->group_address.as_u32 = group->key->ip4.as_u32;
437  else
438  query->group_address.as_u32 = 0;
439 
441  bq->base.n_avail -= sizeof (igmp_membership_query_v3_t);
442  bq->base.n_bytes += sizeof (igmp_membership_query_v3_t);
443 
444  return (b);
445 }
446 
447 void
449  const igmp_group_t * group,
450  const ip46_address_t * srcs)
451 {
452  vlib_buffer_t *b;
453 
455 
456  if (NULL == b)
457  {
458  b = igmp_pkt_build_query_v3 (bq, group);
459  if (NULL == b)
460  /* failed to allocate buffer */
461  return;
462  }
463 
464  if (NULL != srcs)
465  {
467  const ip46_address_t *src;
468 
469  query = vlib_buffer_get_current (b);
470 
471  vec_foreach (src, srcs)
472  {
473  query->src_addresses[bq->n_srcs++].as_u32 = src->ip4.as_u32;
474 
475  vlib_buffer_append (b, sizeof (ip4_address_t));
476  bq->base.n_bytes += sizeof (ip4_address_t);
477  bq->base.n_avail += sizeof (ip4_address_t);
478  }
479  }
480  /*
481  * else
482  * general query and we're done
483  */
484 }
485 
486 static void
488 {
490  ip4_header_t *ip4;
491  vlib_buffer_t *b;
492 
494 
495  b->current_data = 0;
496 
497  ip4 = vlib_buffer_get_current (b);
498  // account for options
499  igmp = (igmp_membership_query_v3_t *) (((u32 *) ip4) + 6);
500 
501  igmp->n_src_addresses = clib_host_to_net_u16 (bq->n_srcs);
502 
503  igmp->header.checksum =
505 
506  ip4->length = clib_host_to_net_u16 (b->current_length);
507  ip4->checksum = ip4_header_checksum (ip4);
508 
509  bq->base.n_bytes = bq->base.n_avail = bq->n_srcs = 0;
510 }
511 
512 void
514 {
515  if (NULL == bq->base.buffers)
516  return;
517 
519  igmp_pkt_tx (&bq->base);
520 }
521 
522 void
524 {
525  clib_memset (bq, 0, sizeof (*bq));
527 }
528 
529 /*
530  * fd.io coding-style-patch-verification: ON
531  *
532  * Local Variables:
533  * eval: (c-set-style "gnu")
534  * End:
535  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
igmp_membership_group_v3_type_t type
Definition: igmp_packet.h:149
igmp_pkt_build_t base
Definition: igmp_pkt.h:33
#define vec_foreach_index(var, v)
Iterate over vector indices.
u8 * format_igmp_key(u8 *s, va_list *args)
Definition: igmp_format.c:203
void igmp_pkt_query_v3_send(igmp_pkt_build_query_t *bq)
Definition: igmp_pkt.c:513
ip4_address_t src_address
Definition: ip4_packet.h:125
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
ip4_address_t src_addresses[0]
Definition: igmp_packet.h:115
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static void vlib_buffer_append(vlib_buffer_t *b, uword l)
Definition: igmp_pkt.c:21
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
adj_index_t adj_index
Definition: igmp_config.h:57
void igmp_pkt_report_v3_add_group(igmp_pkt_build_report_t *br, const igmp_group_t *group, igmp_membership_group_v3_type_t type)
Definition: igmp_pkt.c:349
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
typedef igmp_group
Used by a &#39;host&#39; to enable the reception/listening of packets for a specific multicast group...
Definition: igmp.api:66
igmp_membership_group_v3_type_t
Definition: igmp_packet.h:140
vl_api_address_t src
Definition: gre.api:54
ip_lookup_main_t lookup_main
Definition: ip4.h:108
vlib_main_t * vm
Definition: in2out_ed.c:1582
static u32 vnet_sw_interface_get_mtu(vnet_main_t *vnm, u32 sw_if_index, vnet_mtu_t af)
#define IGMP_GENERAL_QUERY_ADDRESS
General Query address - 224.0.0.1 Membership Report address - 224.0.0.22 SSM default range 232/8...
Definition: igmp.h:51
format_function_t format_vnet_sw_if_index_name
unsigned char u8
Definition: types.h:56
void igmp_pkt_build_report_init(igmp_pkt_build_report_t *br, u32 sw_if_index)
Definition: igmp_pkt.c:400
#define IGMP_DBG(...)
Definition: igmp.h:38
vlib_node_registration_t ip4_rewrite_mcast_node
(constructor) VLIB_REGISTER_NODE (ip4_rewrite_mcast_node)
Definition: ip4_forward.c:2690
enum igmp_msg_type_t_ igmp_msg_type_t
ip4_address_t dst_address
Definition: ip4_packet.h:125
static vlib_buffer_t * igmp_pkt_build_ip_header(igmp_pkt_build_t *bk, igmp_msg_type_t msg_type, const igmp_group_t *group)
Definition: igmp_pkt.c:59
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
unsigned int u32
Definition: types.h:88
static vlib_buffer_t * igmp_pkt_build_report_get_active(igmp_pkt_build_report_t *br)
Definition: igmp_pkt.c:173
igmp_pkt_build_t base
Definition: igmp_pkt.h:57
void igmp_pkt_report_v3_send(igmp_pkt_build_report_t *br)
Definition: igmp_pkt.c:208
vl_api_fib_path_type_t type
Definition: fib_types.api:123
igmp_config_t * igmp_config_lookup(u32 sw_if_index)
igmp config lookup
Definition: igmp_config.c:45
static void igmp_pkt_build_query_bake(igmp_pkt_build_query_t *bq)
Definition: igmp_pkt.c:487
static vlib_buffer_t * igmp_pkt_build_query_get_active(igmp_pkt_build_query_t *bq)
Definition: igmp_pkt.c:407
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
#define IGMP_MEMBERSHIP_REPORT_ADDRESS
Definition: igmp.h:52
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:216
static vlib_buffer_t * igmp_pkt_get_buffer(igmp_pkt_build_t *bk)
Definition: igmp_pkt.c:28
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
vl_api_ip4_address_t ip4
Definition: one.api:376
igmp_type_t type
Definition: igmp_packet.h:70
static void igmp_pkt_tx(igmp_pkt_build_t *bk)
Definition: igmp_pkt.c:137
format_function_t format_ip46_address
Definition: ip46_address.h:50
static igmp_membership_group_v3_t * igmp_pkt_report_v3_append_src(igmp_pkt_build_report_t *br, igmp_membership_group_v3_t *igmp_group, const ip46_address_t *grp, igmp_membership_group_v3_type_t type, const ip46_address_t *src)
4.2.16 " If the set of Group Records required in a Report does not fit within the size limit of a sin...
Definition: igmp_pkt.c:279
u16 n_vectors
Definition: node.h:396
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static u32 igmp_pkt_report_v3_get_size(const igmp_group_t *group)
Definition: igmp_pkt.c:218
void igmp_pkt_query_v3_add_group(igmp_pkt_build_query_t *bq, const igmp_group_t *group, const ip46_address_t *srcs)
Definition: igmp_pkt.c:448
static vlib_buffer_t * igmp_pkt_build_query_v3(igmp_pkt_build_query_t *bq, const igmp_group_t *group)
Definition: igmp_pkt.c:417
static uword hash_elts(void *v)
Definition: hash.h:118
#define ASSERT(truth)
void igmp_pkt_report_v3_add_report(igmp_pkt_build_report_t *br, const ip46_address_t *grp, const ip46_address_t *srcs, igmp_membership_group_v3_type_t type)
Definition: igmp_pkt.c:309
ip_dscp_t tos
Definition: ip4_packet.h:96
static vlib_buffer_t * igmp_pkt_build_report_v3(igmp_pkt_build_report_t *br, const igmp_group_t *group)
Definition: igmp_pkt.c:112
ip4_address_t group_address
Definition: igmp_packet.h:158
static igmp_membership_group_v3_t * igmp_pkt_report_v3_append_group(igmp_pkt_build_report_t *br, const ip46_address_t *grp, igmp_membership_group_v3_type_t type)
Definition: igmp_pkt.c:227
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
igmp_filter_mode_t router_filter_mode
The current filter mode of the group (see 6.2.1)
Definition: igmp_group.h:72
IGMP interface configuration.
Definition: igmp_config.h:47
static void igmp_pkt_build_report_bake(igmp_pkt_build_report_t *br)
Definition: igmp_pkt.c:183
Definition: defs.h:47
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define FOR_EACH_SRC(_src, _group, _filter, _body)
Definition: igmp_group.h:90
igmp_key_t * key
The group&#39;s key within the per-interface config.
Definition: igmp_group.h:59
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:492
IGMP group A multicast group address for which reception has been requested.
Definition: igmp_group.h:56
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
igmp_key_t * key
The source&#39;s key.
Definition: igmp_src.h:46
void igmp_pkt_build_query_init(igmp_pkt_build_query_t *bq, u32 sw_if_index)
Definition: igmp_pkt.c:523
#define vnet_buffer(b)
Definition: buffer.h:417
ip4_address_t src_addresses[0]
Definition: igmp_packet.h:160
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1144
#define vec_foreach(var, vec)
Vector iterator.
u8 ip_version_and_header_length
Definition: ip4_packet.h:93
uword * igmp_src_by_key[IGMP_N_FILTER_MODES]
Source list per-filter mode.
Definition: igmp_group.h:87
static int ip4_src_address_for_packet(ip_lookup_main_t *lm, u32 sw_if_index, ip4_address_t *src)
Definition: ip4.h:217
static ip_csum_t ip_incremental_checksum(ip_csum_t sum, void *_data, uword n_bytes)
Definition: ip_packet.h:318
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
IGMP source The representation of a specified source address with in multicast group.
Definition: igmp_src.h:41
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:300
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
Definition: defs.h:46
ip4_address_t group_address
Definition: igmp_packet.h:105