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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
18 #include <vlib/vlib.h>
19 #include <vlibmemory/api.h>
20 #include <vnet/plugin/plugin.h>
21 #include <vpp/app/version.h>
22 #include <vnet/ip/ip.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/adj/adj_mcast.h>
26 #include <igmp/igmp.h>
27 #include <igmp/igmp_pkt.h>
28 #include <igmp/igmp_query.h>
29 #include <igmp/igmp_report.h>
30 #include <igmp/igmp_error.h>
37 IGMP_INPUT_NEXT_PARSE_QUERY,
38 IGMP_INPUT_NEXT_PARSE_REPORT,
44 IGMP_PARSE_QUERY_NEXT_DROP,
45 IGMP_PARSE_QUERY_N_NEXT,
46 } igmp_parse_query_next_t;
50 IGMP_PARSE_REPORT_NEXT_DROP,
51 IGMP_PARSE_REPORT_N_NEXT,
52 } igmp_parse_report_next_t;
54 char *igmp_error_strings[] = {
55 #define _(sym,string) string,
69 format_igmp_input_trace (u8 * s, va_list * va)
71 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
72 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
73 igmp_input_trace_t *t = va_arg (*va, igmp_input_trace_t *);
75 s = format (s, "sw_if_index %u next-index %u",
76 t->sw_if_index, t->next_index);
77 s = format (s, "\n%U", format_igmp_header, t->packet_data,
78 sizeof (t->packet_data));
83 format_igmp_parse_report_trace (u8 * s, va_list * va)
85 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
86 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
87 igmp_input_trace_t *t = va_arg (*va, igmp_input_trace_t *);
89 s = format (s, "sw_if_index %u next-index %u",
90 t->sw_if_index, t->next_index);
91 s = format (s, "\n%U", format_igmp_report_v3, t->packet_data,
92 sizeof (t->packet_data));
97 format_igmp_parse_query_trace (u8 * s, va_list * va)
99 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
100 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
101 igmp_input_trace_t *t = va_arg (*va, igmp_input_trace_t *);
103 s = format (s, "sw_if_index %u next-input %u len %u",
104 t->sw_if_index, t->next_index, t->len);
105 s = format (s, "\n%U", format_igmp_query_v3, t->packet_data,
106 sizeof (t->packet_data));
107 s = format (s, "\n%U", format_hex_bytes,
108 t->packet_data, sizeof (t->packet_data));
113 igmp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
114 vlib_frame_t * frame)
116 igmp_parse_query_next_t next_index;
117 u32 n_left_from, *from, *to_next;
118 vlib_node_runtime_t *error_node;
121 error = IGMP_ERROR_NONE;
124 from = vlib_frame_vector_args (frame);
125 n_left_from = frame->n_vectors;
126 next_index = node->cached_next_index;
128 while (n_left_from > 0)
132 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
134 while (n_left_from > 0 && n_left_to_next > 0)
143 next = IGMP_INPUT_NEXT_DROP;
151 b = vlib_get_buffer (vm, bi);
152 ip = vlib_buffer_get_current (b);
154 if (ip->protocol != IP_PROTOCOL_IGMP)
156 error = IGMP_ERROR_INVALID_PROTOCOL;
157 next = IGMP_INPUT_NEXT_DROP;
161 vlib_buffer_advance (b, ip4_header_bytes (ip));
163 igmp = vlib_buffer_get_current (b);
165 checksum = igmp->checksum;
167 sum = ip_incremental_checksum (0, igmp,
168 clib_net_to_host_u16 (ip->length) -
169 ip4_header_bytes (ip));
170 igmp->checksum = checksum;
171 csum = ~ip_csum_fold (sum);
172 if (checksum != csum)
174 error = IGMP_ERROR_BAD_CHECKSUM;
175 next = IGMP_INPUT_NEXT_DROP;
178 if (!igmp_config_lookup (vnet_buffer (b)->sw_if_index[VLIB_RX]))
180 error = IGMP_ERROR_NOT_ENABLED;
181 next = IGMP_INPUT_NEXT_DROP;
185 /* TODO: IGMPv2 and IGMPv1 */
188 case IGMP_TYPE_membership_query:
189 next = IGMP_INPUT_NEXT_PARSE_QUERY;
191 case IGMP_TYPE_membership_report_v3:
192 next = IGMP_INPUT_NEXT_PARSE_REPORT;
195 error = IGMP_ERROR_UNKNOWN_TYPE;
196 next = IGMP_INPUT_NEXT_DROP;
200 b->error = error_node->errors[error];
202 if (node->flags & VLIB_NODE_FLAG_TRACE)
204 igmp_input_trace_t *tr;
205 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
206 tr->next_index = next;
207 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
208 tr->len = vlib_buffer_length_in_chain (vm, b);
209 clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b),
210 sizeof (tr->packet_data));
213 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
214 n_left_to_next, bi, next);
216 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
219 return frame->n_vectors;
223 VLIB_REGISTER_NODE (igmp_input_node) =
225 .function = igmp_input,
226 .name = "igmp-input",
227 .vector_size = sizeof (u32),
229 .format_buffer = format_igmp_header,
230 .format_trace = format_igmp_input_trace,
232 .n_errors = IGMP_N_ERROR,
233 .error_strings = igmp_error_strings,
235 .n_next_nodes = IGMP_INPUT_N_NEXT,
237 [IGMP_INPUT_NEXT_DROP] = "error-drop",
238 [IGMP_INPUT_NEXT_PARSE_QUERY] = "igmp-parse-query",
239 [IGMP_INPUT_NEXT_PARSE_REPORT] = "igmp-parse-report",
245 igmp_parse_query (vlib_main_t * vm, vlib_node_runtime_t * node,
246 vlib_frame_t * frame)
248 u32 n_left_from, *from, *to_next;
249 igmp_parse_query_next_t next_index;
251 from = vlib_frame_vector_args (frame);
252 n_left_from = frame->n_vectors;
253 next_index = node->cached_next_index;
255 while (n_left_from > 0)
259 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
261 while (n_left_from > 0 && n_left_to_next > 0)
263 igmp_membership_query_v3_t *igmp;
264 igmp_query_args_t *args;
268 next = IGMP_PARSE_QUERY_NEXT_DROP;
276 b = vlib_get_buffer (vm, bi);
277 igmp = vlib_buffer_get_current (b);
278 ASSERT (igmp->header.type == IGMP_TYPE_membership_query);
279 len = igmp_membership_query_v3_length (igmp);
281 if (node->flags & VLIB_NODE_FLAG_TRACE)
283 igmp_input_trace_t *tr;
284 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
285 tr->next_index = next;
286 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
288 clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b),
289 sizeof (tr->packet_data));
293 * validate that the length on the packet on the wire corresponds
294 * to at least the length of the calculated v3 query.
295 * If there's extra, then it will be ignored.
297 if (vlib_buffer_length_in_chain (vm, b) >= len)
300 * copy the contents of the query, and the interface, over
301 * to the main thread for processing
303 vlib_buffer_advance (b, -sizeof (u32));
304 args = vlib_buffer_get_current (b);
305 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
307 vl_api_rpc_call_main_thread (igmp_handle_query,
308 (u8 *) args, sizeof (*args) + len);
313 * else a packet that is reporting more sources than it really
316 b->error = node->errors[IGMP_ERROR_BAD_LENGTH];
319 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
320 n_left_to_next, bi, next);
322 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
325 return frame->n_vectors;
329 VLIB_REGISTER_NODE (igmp_parse_query_node) =
331 .function = igmp_parse_query,
332 .name = "igmp-parse-query",
333 .vector_size = sizeof (u32),
335 .format_buffer = format_igmp_query_v3,
336 .format_trace = format_igmp_parse_query_trace,
338 .n_errors = IGMP_N_ERROR,
339 .error_strings = igmp_error_strings,
341 .n_next_nodes = IGMP_PARSE_QUERY_N_NEXT,
343 [IGMP_PARSE_QUERY_NEXT_DROP] = "error-drop",
349 igmp_parse_report (vlib_main_t * vm, vlib_node_runtime_t * node,
350 vlib_frame_t * frame)
352 u32 n_left_from, *from, *to_next;
353 igmp_input_next_t next_index;
354 vlib_node_runtime_t *error_node =
355 vlib_node_get_runtime (vm, igmp_input_node.index);
358 from = vlib_frame_vector_args (frame);
359 n_left_from = frame->n_vectors;
360 next_index = node->cached_next_index;
362 while (n_left_from > 0)
366 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
368 while (n_left_from > 0 && n_left_to_next > 0)
370 igmp_membership_report_v3_t *igmp;
371 igmp_report_args_t *args;
375 next = IGMP_PARSE_REPORT_NEXT_DROP;
384 b = vlib_get_buffer (vm, bi);
386 error = IGMP_ERROR_NONE;
387 b->error = error_node->errors[error];
388 igmp = vlib_buffer_get_current (b);
389 len = igmp_membership_report_v3_length (igmp);
391 ASSERT (igmp->header.type == IGMP_TYPE_membership_report_v3);
393 if (node->flags & VLIB_NODE_FLAG_TRACE)
395 igmp_input_trace_t *tr;
396 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
397 tr->next_index = next;
399 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
400 clib_memcpy_fast (tr->packet_data, vlib_buffer_get_current (b),
401 sizeof (tr->packet_data));
405 * validate that the length on the packet on the wire
406 * corresponds to the length on the calculated v3 query
408 if (vlib_buffer_length_in_chain (vm, b) >= len)
411 * copy the contents of the query, and the interface, over
412 * to the main thread for processing
414 vlib_buffer_advance (b, -sizeof (u32));
415 args = vlib_buffer_get_current (b);
416 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
418 vl_api_rpc_call_main_thread (igmp_handle_report,
419 (u8 *) args, sizeof (*args) + len);
424 * this is a packet with more groups/sources than the
425 * header reports. bin it
427 b->error = node->errors[IGMP_ERROR_BAD_LENGTH];
430 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
431 n_left_to_next, bi, next);
433 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
436 return frame->n_vectors;
440 VLIB_REGISTER_NODE (igmp_parse_report_node) =
442 .function = igmp_parse_report,
443 .name = "igmp-parse-report",
444 .vector_size = sizeof (u32),
446 .format_buffer = format_igmp_report_v3,
447 .format_trace = format_igmp_parse_report_trace,
449 .n_errors = IGMP_N_ERROR,
450 .error_strings = igmp_error_strings,
452 .n_next_nodes = IGMP_PARSE_REPORT_N_NEXT,
454 [IGMP_PARSE_REPORT_NEXT_DROP] = "error-drop",
459 static clib_error_t *
460 igmp_input_init (vlib_main_t * vm)
462 ip4_register_protocol (IP_PROTOCOL_IGMP, igmp_input_node.index);
464 IGMP_DBG ("input-initialized");
470 VLIB_INIT_FUNCTION (igmp_input_init) =
472 .runs_after = VLIB_INITS("igmp_init"),
477 * fd.io coding-style-patch-verification: ON
480 * eval: (c-set-style "gnu")