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));
288 * copy the contents of the query, and the interface, over
289 * to the main thread for processing
291 vlib_buffer_advance (b, -sizeof (u32));
292 args = vlib_buffer_get_current (b);
293 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
295 vl_api_rpc_call_main_thread (igmp_handle_query,
298 igmp_membership_query_v3_length
301 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
302 n_left_to_next, bi, next);
304 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
307 return frame->n_vectors;
311 VLIB_REGISTER_NODE (igmp_parse_query_node) =
313 .function = igmp_parse_query,
314 .name = "igmp-parse-query",
315 .vector_size = sizeof (u32),
317 .format_buffer = format_igmp_query_v3,
318 .format_trace = format_igmp_parse_query_trace,
320 .n_errors = IGMP_N_ERROR,
321 .error_strings = igmp_error_strings,
323 .n_next_nodes = IGMP_PARSE_QUERY_N_NEXT,
325 [IGMP_PARSE_QUERY_NEXT_DROP] = "error-drop",
331 igmp_parse_report (vlib_main_t * vm, vlib_node_runtime_t * node,
332 vlib_frame_t * frame)
334 u32 n_left_from, *from, *to_next;
335 igmp_input_next_t next_index;
336 vlib_node_runtime_t *error_node =
337 vlib_node_get_runtime (vm, igmp_input_node.index);
340 from = vlib_frame_vector_args (frame);
341 n_left_from = frame->n_vectors;
342 next_index = node->cached_next_index;
344 while (n_left_from > 0)
348 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
350 while (n_left_from > 0 && n_left_to_next > 0)
352 igmp_membership_report_v3_t *igmp;
353 igmp_report_args_t *args;
357 next = IGMP_PARSE_REPORT_NEXT_DROP;
366 b = vlib_get_buffer (vm, bi);
368 error = IGMP_ERROR_NONE;
369 b->error = error_node->errors[error];
370 igmp = vlib_buffer_get_current (b);
372 ASSERT (igmp->header.type == IGMP_TYPE_membership_report_v3);
374 if (node->flags & VLIB_NODE_FLAG_TRACE)
376 igmp_input_trace_t *tr;
377 tr = vlib_add_trace (vm, node, b, sizeof (*tr));
378 tr->next_index = next;
379 tr->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
380 clib_memcpy (tr->packet_data, vlib_buffer_get_current (b),
381 sizeof (tr->packet_data));
385 * copy the contents of the query, and the interface, over
386 * to the main thread for processing
388 vlib_buffer_advance (b, -sizeof (u32));
389 args = vlib_buffer_get_current (b);
390 args->sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
392 vl_api_rpc_call_main_thread (igmp_handle_report,
395 igmp_membership_report_v3_length
398 vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
399 n_left_to_next, bi, next);
401 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
404 return frame->n_vectors;
408 VLIB_REGISTER_NODE (igmp_parse_report_node) =
410 .function = igmp_parse_report,
411 .name = "igmp-parse-report",
412 .vector_size = sizeof (u32),
414 .format_buffer = format_igmp_report_v3,
415 .format_trace = format_igmp_parse_report_trace,
417 .n_errors = IGMP_N_ERROR,
418 .error_strings = igmp_error_strings,
420 .n_next_nodes = IGMP_PARSE_REPORT_N_NEXT,
422 [IGMP_PARSE_REPORT_NEXT_DROP] = "error-drop",
427 static clib_error_t *
428 igmp_input_init (vlib_main_t * vm)
432 if ((error = vlib_call_init_function (vm, igmp_init)))
435 ip4_register_protocol (IP_PROTOCOL_IGMP, igmp_input_node.index);
437 IGMP_DBG ("input-initialized");
442 VLIB_INIT_FUNCTION (igmp_input_init);
445 * fd.io coding-style-patch-verification: ON
448 * eval: (c-set-style "gnu")