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",
104 t->sw_if_index, t->next_index);
105 s = format (s, "\n%U", format_igmp_query_v3, t->packet_data,
106 sizeof (t->packet_data));
111 igmp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
112 vlib_frame_t * frame)
114 igmp_parse_query_next_t next_index;
115 u32 n_left_from, *from, *to_next;
116 vlib_node_runtime_t *error_node;
119 error = IGMP_ERROR_NONE;
122 from = vlib_frame_vector_args (frame);
123 n_left_from = frame->n_vectors;
124 next_index = node->cached_next_index;
126 while (n_left_from > 0)
130 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
132 while (n_left_from > 0 && n_left_to_next > 0)
141 next = IGMP_INPUT_NEXT_DROP;
149 b = vlib_get_buffer (vm, bi);
150 ip = vlib_buffer_get_current (b);
152 if (ip->protocol != IP_PROTOCOL_IGMP)
154 error = IGMP_ERROR_INVALID_PROTOCOL;
155 next = IGMP_INPUT_NEXT_DROP;
159 vlib_buffer_advance (b, ip4_header_bytes (ip));
161 igmp = vlib_buffer_get_current (b);
163 checksum = igmp->checksum;
165 sum = ip_incremental_checksum (0, igmp,
166 clib_net_to_host_u16 (ip->length) -
167 ip4_header_bytes (ip));
168 igmp->checksum = checksum;
169 csum = ~ip_csum_fold (sum);
170 if (checksum != csum)
172 error = IGMP_ERROR_BAD_CHECKSUM;
173 next = IGMP_INPUT_NEXT_DROP;
176 if (!igmp_config_lookup (vnet_buffer (b)->sw_if_index[VLIB_RX]))
178 error = IGMP_ERROR_NOT_ENABLED;
179 next = IGMP_INPUT_NEXT_DROP;
183 /* TODO: IGMPv2 and IGMPv1 */
186 case IGMP_TYPE_membership_query:
187 next = IGMP_INPUT_NEXT_PARSE_QUERY;
189 case IGMP_TYPE_membership_report_v3:
190 next = IGMP_INPUT_NEXT_PARSE_REPORT;
193 error = IGMP_ERROR_UNKNOWN_TYPE;
194 next = IGMP_INPUT_NEXT_DROP;
198 b->error = error_node->errors[error];
200 if (node->flags & VLIB_NODE_FLAG_TRACE)
202 igmp_input_trace_t *tr;
203 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
204 tr->next_index = next;
205 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
206 clib_memcpy (tr->packet_data, vlib_buffer_get_current (b),
207 sizeof (tr->packet_data));
210 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
211 n_left_to_next, bi, next);
213 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
216 return frame->n_vectors;
220 VLIB_REGISTER_NODE (igmp_input_node) =
222 .function = igmp_input,
223 .name = "igmp-input",
224 .vector_size = sizeof (u32),
226 .format_buffer = format_igmp_header,
227 .format_trace = format_igmp_input_trace,
229 .n_errors = IGMP_N_ERROR,
230 .error_strings = igmp_error_strings,
232 .n_next_nodes = IGMP_INPUT_N_NEXT,
234 [IGMP_INPUT_NEXT_DROP] = "error-drop",
235 [IGMP_INPUT_NEXT_PARSE_QUERY] = "igmp-parse-query",
236 [IGMP_INPUT_NEXT_PARSE_REPORT] = "igmp-parse-report",
242 igmp_parse_query (vlib_main_t * vm, vlib_node_runtime_t * node,
243 vlib_frame_t * frame)
245 u32 n_left_from, *from, *to_next;
246 igmp_parse_query_next_t next_index;
248 from = vlib_frame_vector_args (frame);
249 n_left_from = frame->n_vectors;
250 next_index = node->cached_next_index;
252 while (n_left_from > 0)
256 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
258 while (n_left_from > 0 && n_left_to_next > 0)
260 igmp_membership_query_v3_t *igmp;
261 igmp_query_args_t *args;
265 next = IGMP_PARSE_QUERY_NEXT_DROP;
273 b = vlib_get_buffer (vm, bi);
274 igmp = vlib_buffer_get_current (b);
275 ASSERT (igmp->header.type == IGMP_TYPE_membership_query);
277 if (node->flags & VLIB_NODE_FLAG_TRACE)
279 igmp_input_trace_t *tr;
280 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
281 tr->next_index = next;
282 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
283 clib_memcpy (tr->packet_data, vlib_buffer_get_current (b),
284 sizeof (tr->packet_data));
286 len = igmp_membership_query_v3_length (igmp);
289 * validate that the length on the packet on the wire
290 * corresponds to the length on the calculated v3 query
292 if (vlib_buffer_length_in_chain (vm, b) == len)
295 * copy the contents of the query, and the interface, over
296 * to the main thread for processing
298 vlib_buffer_advance (b, -sizeof (u32));
299 args = vlib_buffer_get_current (b);
300 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
302 vl_api_rpc_call_main_thread (igmp_handle_query,
303 (u8 *) args, sizeof (*args) + len);
306 * else a packet that is reporting more or less sources
307 * than it really has, bin it
310 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
311 n_left_to_next, bi, next);
313 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
316 return frame->n_vectors;
320 VLIB_REGISTER_NODE (igmp_parse_query_node) =
322 .function = igmp_parse_query,
323 .name = "igmp-parse-query",
324 .vector_size = sizeof (u32),
326 .format_buffer = format_igmp_query_v3,
327 .format_trace = format_igmp_parse_query_trace,
329 .n_errors = IGMP_N_ERROR,
330 .error_strings = igmp_error_strings,
332 .n_next_nodes = IGMP_PARSE_QUERY_N_NEXT,
334 [IGMP_PARSE_QUERY_NEXT_DROP] = "error-drop",
340 igmp_parse_report (vlib_main_t * vm, vlib_node_runtime_t * node,
341 vlib_frame_t * frame)
343 u32 n_left_from, *from, *to_next;
344 igmp_input_next_t next_index;
345 vlib_node_runtime_t *error_node =
346 vlib_node_get_runtime (vm, igmp_input_node.index);
349 from = vlib_frame_vector_args (frame);
350 n_left_from = frame->n_vectors;
351 next_index = node->cached_next_index;
353 while (n_left_from > 0)
357 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
359 while (n_left_from > 0 && n_left_to_next > 0)
361 igmp_membership_report_v3_t *igmp;
362 igmp_report_args_t *args;
366 next = IGMP_PARSE_REPORT_NEXT_DROP;
375 b = vlib_get_buffer (vm, bi);
377 error = IGMP_ERROR_NONE;
378 b->error = error_node->errors[error];
379 igmp = vlib_buffer_get_current (b);
380 len = igmp_membership_report_v3_length (igmp);
382 ASSERT (igmp->header.type == IGMP_TYPE_membership_report_v3);
384 if (node->flags & VLIB_NODE_FLAG_TRACE)
386 igmp_input_trace_t *tr;
387 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
388 tr->next_index = next;
389 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
390 clib_memcpy (tr->packet_data, vlib_buffer_get_current (b),
391 sizeof (tr->packet_data));
395 * validate that the length on the packet on the wire
396 * corresponds to the length on the calculated v3 query
398 if (vlib_buffer_length_in_chain (vm, b) == len)
401 * copy the contents of the query, and the interface, over
402 * to the main thread for processing
404 vlib_buffer_advance (b, -sizeof (u32));
405 args = vlib_buffer_get_current (b);
406 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
408 vl_api_rpc_call_main_thread (igmp_handle_report,
409 (u8 *) args, sizeof (*args) + len);
413 * this is a packet with more groups/sources than the
414 * header reports. bin it
416 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
417 n_left_to_next, bi, next);
419 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
422 return frame->n_vectors;
426 VLIB_REGISTER_NODE (igmp_parse_report_node) =
428 .function = igmp_parse_report,
429 .name = "igmp-parse-report",
430 .vector_size = sizeof (u32),
432 .format_buffer = format_igmp_report_v3,
433 .format_trace = format_igmp_parse_report_trace,
435 .n_errors = IGMP_N_ERROR,
436 .error_strings = igmp_error_strings,
438 .n_next_nodes = IGMP_PARSE_REPORT_N_NEXT,
440 [IGMP_PARSE_REPORT_NEXT_DROP] = "error-drop",
445 static clib_error_t *
446 igmp_input_init (vlib_main_t * vm)
450 if ((error = vlib_call_init_function (vm, igmp_init)))
453 ip4_register_protocol (IP_PROTOCOL_IGMP, igmp_input_node.index);
455 IGMP_DBG ("input-initialized");
460 VLIB_INIT_FUNCTION (igmp_input_init);
463 * fd.io coding-style-patch-verification: ON
466 * eval: (c-set-style "gnu")