2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/ip/ip.h>
16 #include <vnet/ip/ip_source_and_port_range_check.h>
17 #include <vnet/dpo/load_balance.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/fib/ip4_fib.h>
21 source_range_check_main_t source_range_check_main;
25 * @brief IPv4 Source and Port Range Checking.
27 * This file contains the source code for IPv4 source and port range
33 * @brief The pool of range chack DPOs
35 static protocol_port_range_dpo_t *ppr_dpo_pool;
38 * @brief Dynamically registered DPO type
40 static dpo_type_t ppr_dpo_type;
42 vlib_node_registration_t ip4_source_port_and_range_check_rx;
43 vlib_node_registration_t ip4_source_port_and_range_check_tx;
45 #define foreach_ip4_source_and_port_range_check_error \
46 _(CHECK_FAIL, "ip4 source and port range check bad packets") \
47 _(CHECK_OK, "ip4 source and port range check good packets")
51 #define _(sym,str) IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_##sym,
52 foreach_ip4_source_and_port_range_check_error
54 IP4_SOURCE_AND_PORT_RANGE_CHECK_N_ERROR,
55 } ip4_source_and_port_range_check_error_t;
57 static char *ip4_source_and_port_range_check_error_strings[] = {
58 #define _(sym,string) string,
59 foreach_ip4_source_and_port_range_check_error
68 ip4_address_t src_addr;
71 } ip4_source_and_port_range_check_trace_t;
74 format_ip4_source_and_port_range_check_trace (u8 * s, va_list * va)
76 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
77 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
78 ip4_source_and_port_range_check_trace_t *t =
79 va_arg (*va, ip4_source_and_port_range_check_trace_t *);
82 s = format (s, "PASS (bypass case)");
84 s = format (s, "fib %d src ip %U %s dst port %d: %s",
85 t->fib_index, format_ip4_address, &t->src_addr,
86 t->is_tcp ? "TCP" : "UDP", (u32) t->port,
87 (t->pass == 1) ? "PASS" : "FAIL");
93 IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP,
94 IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
95 } ip4_source_and_port_range_check_next_t;
99 check_adj_port_range_x1 (const protocol_port_range_dpo_t * ppr_dpo,
100 u16 dst_port, u32 next)
102 #ifdef CLIB_HAVE_VEC128
103 u16x8 key = u16x8_splat (dst_port);
107 if (NULL == ppr_dpo || dst_port == 0)
108 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
111 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
112 #ifdef CLIB_HAVE_VEC128
113 if (!u16x8_is_all_zero ((ppr_dpo->blocks[i].low.as_u16x8 <= key) &
114 (ppr_dpo->blocks[i].hi.as_u16x8 >= key)))
118 for (int j = 0; j < 8; j++)
120 if ((ppr_dpo->blocks[i].low.as_u16[j] <= dst_port) &&
121 (ppr_dpo->blocks[i].hi.as_u16[j] >= dst_port))
127 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
130 always_inline protocol_port_range_dpo_t *
131 protocol_port_range_dpo_get (index_t index)
133 return (pool_elt_at_index (ppr_dpo_pool, index));
137 ip4_source_and_port_range_check_inline (vlib_main_t * vm,
138 vlib_node_runtime_t * node,
139 vlib_frame_t * frame, int is_tx)
141 ip4_main_t *im = &ip4_main;
142 u32 n_left_from, *from, *to_next;
144 vlib_node_runtime_t *error_node = node;
145 u32 good_packets = 0;
148 from = vlib_frame_vector_args (frame);
149 n_left_from = frame->n_vectors;
150 next_index = node->cached_next_index;
152 while (n_left_from > 0)
156 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
159 /* while (n_left_from >= 4 && n_left_to_next >= 2) */
161 /* vlib_buffer_t *b0, *b1; */
162 /* ip4_header_t *ip0, *ip1; */
163 /* ip4_fib_mtrie_t *mtrie0, *mtrie1; */
164 /* ip4_fib_mtrie_leaf_t leaf0, leaf1; */
165 /* ip_source_and_port_range_check_config_t *c0, *c1; */
166 /* ip_adjacency_t *adj0 = 0, *adj1 = 0; */
167 /* u32 bi0, next0, adj_index0, pass0, save_next0, fib_index0; */
168 /* u32 bi1, next1, adj_index1, pass1, save_next1, fib_index1; */
169 /* udp_header_t *udp0, *udp1; */
171 /* /\* Prefetch next iteration. *\/ */
173 /* vlib_buffer_t *p2, *p3; */
175 /* p2 = vlib_get_buffer (vm, from[2]); */
176 /* p3 = vlib_get_buffer (vm, from[3]); */
178 /* vlib_prefetch_buffer_header (p2, LOAD); */
179 /* vlib_prefetch_buffer_header (p3, LOAD); */
181 /* CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); */
182 /* CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD); */
185 /* bi0 = to_next[0] = from[0]; */
186 /* bi1 = to_next[1] = from[1]; */
189 /* n_left_from -= 2; */
190 /* n_left_to_next -= 2; */
192 /* b0 = vlib_get_buffer (vm, bi0); */
193 /* b1 = vlib_get_buffer (vm, bi1); */
196 /* vec_elt (im->fib_index_by_sw_if_index, */
197 /* vnet_buffer (b0)->sw_if_index[VLIB_RX]); */
199 /* vec_elt (im->fib_index_by_sw_if_index, */
200 /* vnet_buffer (b1)->sw_if_index[VLIB_RX]); */
202 /* ip0 = vlib_buffer_get_current (b0); */
203 /* ip1 = vlib_buffer_get_current (b1); */
207 /* c0 = vnet_get_config_data (&tx_cm->config_main, */
208 /* &b0->current_config_index, */
209 /* &next0, sizeof (c0[0])); */
210 /* c1 = vnet_get_config_data (&tx_cm->config_main, */
211 /* &b1->current_config_index, */
212 /* &next1, sizeof (c1[0])); */
216 /* c0 = vnet_get_config_data (&rx_cm->config_main, */
217 /* &b0->current_config_index, */
218 /* &next0, sizeof (c0[0])); */
219 /* c1 = vnet_get_config_data (&rx_cm->config_main, */
220 /* &b1->current_config_index, */
221 /* &next1, sizeof (c1[0])); */
224 /* /\* we can't use the default VRF here... *\/ */
225 /* for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++) */
227 /* ASSERT (c0->fib_index[i] && c1->fib_index[i]); */
233 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
236 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
237 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
240 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
244 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
247 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
248 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
251 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
254 /* if (PREDICT_TRUE (fib_index0 != ~0)) */
257 /* mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie; */
259 /* leaf0 = IP4_FIB_MTRIE_LEAF_ROOT; */
261 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
262 /* &ip0->src_address, 0); */
264 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
265 /* &ip0->src_address, 1); */
267 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
268 /* &ip0->src_address, 2); */
270 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
271 /* &ip0->src_address, 3); */
273 /* adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); */
275 /* ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0, */
276 /* &ip0->src_address, */
278 /* /\* use dflt rt *\/ */
280 /* adj0 = ip_get_adjacency (lm, adj_index0); */
285 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
288 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
289 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
292 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
296 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
299 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
300 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
303 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
306 /* if (PREDICT_TRUE (fib_index1 != ~0)) */
309 /* mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie; */
311 /* leaf1 = IP4_FIB_MTRIE_LEAF_ROOT; */
313 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
314 /* &ip1->src_address, 0); */
316 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
317 /* &ip1->src_address, 1); */
319 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
320 /* &ip1->src_address, 2); */
322 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
323 /* &ip1->src_address, 3); */
325 /* adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); */
327 /* ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1, */
328 /* &ip1->src_address, */
330 /* adj1 = ip_get_adjacency (lm, adj_index1); */
334 /* pass0 |= adj0 == 0; */
335 /* pass0 |= ip4_address_is_multicast (&ip0->src_address); */
337 /* ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
338 /* pass0 |= (ip0->protocol != IP_PROTOCOL_UDP) */
339 /* && (ip0->protocol != IP_PROTOCOL_TCP); */
342 /* pass1 |= adj1 == 0; */
343 /* pass1 |= ip4_address_is_multicast (&ip1->src_address); */
345 /* ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
346 /* pass1 |= (ip1->protocol != IP_PROTOCOL_UDP) */
347 /* && (ip1->protocol != IP_PROTOCOL_TCP); */
349 /* save_next0 = next0; */
350 /* udp0 = ip4_next_header (ip0); */
351 /* save_next1 = next1; */
352 /* udp1 = ip4_next_header (ip1); */
354 /* if (PREDICT_TRUE (pass0 == 0)) */
356 /* good_packets++; */
357 /* next0 = check_adj_port_range_x1 */
358 /* (adj0, clib_net_to_host_u16 (udp0->dst_port), next0); */
359 /* good_packets -= (save_next0 != next0); */
360 /* b0->error = error_node->errors */
361 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
364 /* if (PREDICT_TRUE (pass1 == 0)) */
366 /* good_packets++; */
367 /* next1 = check_adj_port_range_x1 */
368 /* (adj1, clib_net_to_host_u16 (udp1->dst_port), next1); */
369 /* good_packets -= (save_next1 != next1); */
370 /* b1->error = error_node->errors */
371 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
374 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
375 /* && (b0->flags & VLIB_BUFFER_IS_TRACED))) */
377 /* ip4_source_and_port_range_check_trace_t *t = */
378 /* vlib_add_trace (vm, node, b0, sizeof (*t)); */
379 /* t->pass = next0 == save_next0; */
380 /* t->bypass = pass0; */
381 /* t->fib_index = fib_index0; */
382 /* t->src_addr.as_u32 = ip0->src_address.as_u32; */
383 /* t->port = (pass0 == 0) ? */
384 /* clib_net_to_host_u16 (udp0->dst_port) : 0; */
385 /* t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP; */
388 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
389 /* && (b1->flags & VLIB_BUFFER_IS_TRACED))) */
391 /* ip4_source_and_port_range_check_trace_t *t = */
392 /* vlib_add_trace (vm, node, b1, sizeof (*t)); */
393 /* t->pass = next1 == save_next1; */
394 /* t->bypass = pass1; */
395 /* t->fib_index = fib_index1; */
396 /* t->src_addr.as_u32 = ip1->src_address.as_u32; */
397 /* t->port = (pass1 == 0) ? */
398 /* clib_net_to_host_u16 (udp1->dst_port) : 0; */
399 /* t->is_tcp = ip1->protocol == IP_PROTOCOL_TCP; */
402 /* vlib_validate_buffer_enqueue_x2 (vm, node, next_index, */
403 /* to_next, n_left_to_next, */
404 /* bi0, bi1, next0, next1); */
407 while (n_left_from > 0 && n_left_to_next > 0)
411 ip_source_and_port_range_check_config_t *c0;
412 u32 bi0, next0, lb_index0, pass0, save_next0, fib_index0;
414 const protocol_port_range_dpo_t *ppr_dpo0 = NULL;
425 b0 = vlib_get_buffer (vm, bi0);
426 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
428 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, sw_if_index0);
431 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
433 ip0 = vlib_buffer_get_current (b0);
435 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
437 /* we can't use the default VRF here... */
438 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
440 ASSERT (c0->fib_index[i]);
446 if (ip0->protocol == IP_PROTOCOL_UDP)
449 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN];
450 if (ip0->protocol == IP_PROTOCOL_TCP)
453 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN];
457 if (ip0->protocol == IP_PROTOCOL_UDP)
460 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT];
461 if (ip0->protocol == IP_PROTOCOL_TCP)
464 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT];
467 if (fib_index0 != ~0)
469 lb_index0 = ip4_fib_forwarding_lookup (fib_index0,
473 load_balance_get_bucket_i (load_balance_get (lb_index0), 0);
475 if (ppr_dpo_type == dpo->dpoi_type)
477 ppr_dpo0 = protocol_port_range_dpo_get (dpo->dpoi_index);
480 * else the lookup hit an enty that was no inserted
481 * by this range checker, which is the default route
485 * $$$ which (src,dst) categories should we always pass?
488 pass0 |= ip4_address_is_multicast (&ip0->src_address);
490 ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
491 pass0 |= (ip0->protocol != IP_PROTOCOL_UDP)
492 && (ip0->protocol != IP_PROTOCOL_TCP);
495 udp0 = ip4_next_header (ip0);
497 if (PREDICT_TRUE (pass0 == 0))
500 next0 = check_adj_port_range_x1
501 (ppr_dpo0, clib_net_to_host_u16 (udp0->dst_port), next0);
502 good_packets -= (save_next0 != next0);
503 b0->error = error_node->errors
504 [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL];
507 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
508 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
510 ip4_source_and_port_range_check_trace_t *t =
511 vlib_add_trace (vm, node, b0, sizeof (*t));
512 t->pass = next0 == save_next0;
514 t->fib_index = fib_index0;
515 t->src_addr.as_u32 = ip0->src_address.as_u32;
516 t->port = (pass0 == 0) ?
517 clib_net_to_host_u16 (udp0->dst_port) : 0;
518 t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP;
522 vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
524 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
525 to_next, n_left_to_next,
529 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
533 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_tx.index,
534 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
537 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_rx.index,
538 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
540 return frame->n_vectors;
544 ip4_source_and_port_range_check_rx (vlib_main_t * vm,
545 vlib_node_runtime_t * node,
546 vlib_frame_t * frame)
548 return ip4_source_and_port_range_check_inline (vm, node, frame,
553 ip4_source_and_port_range_check_tx (vlib_main_t * vm,
554 vlib_node_runtime_t * node,
555 vlib_frame_t * frame)
557 return ip4_source_and_port_range_check_inline (vm, node, frame,
561 /* Note: Calling same function for both RX and TX nodes
562 as always checking dst_port, although
563 if this changes can easily make new function
566 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_rx) = {
567 .function = ip4_source_and_port_range_check_rx,
568 .name = "ip4-source-and-port-range-check-rx",
569 .vector_size = sizeof (u32),
571 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
572 .error_strings = ip4_source_and_port_range_check_error_strings,
574 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
576 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
579 .format_buffer = format_ip4_header,
580 .format_trace = format_ip4_source_and_port_range_check_trace,
583 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_tx) = {
584 .function = ip4_source_and_port_range_check_tx,
585 .name = "ip4-source-and-port-range-check-tx",
586 .vector_size = sizeof (u32),
588 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
589 .error_strings = ip4_source_and_port_range_check_error_strings,
591 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
593 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
596 .format_buffer = format_ip4_header,
597 .format_trace = format_ip4_source_and_port_range_check_trace,
601 set_ip_source_and_port_range_check (vlib_main_t * vm,
603 u32 sw_if_index, u32 is_add)
605 ip_source_and_port_range_check_config_t config;
609 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
611 config.fib_index[i] = fib_index[i];
614 /* For OUT we are in the RX path */
615 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] != ~0) ||
616 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] != ~0))
618 vnet_feature_enable_disable ("ip4-unicast",
619 "ip4-source-and-port-range-check-rx",
620 sw_if_index, is_add, &config,
624 /* For IN we are in the TX path */
625 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] != ~0) ||
626 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] != ~0))
628 vnet_feature_enable_disable ("ip4-output",
629 "ip4-source-and-port-range-check-tx",
630 sw_if_index, is_add, &config,
636 static clib_error_t *
637 set_ip_source_and_port_range_check_fn (vlib_main_t * vm,
638 unformat_input_t * input,
639 vlib_cli_command_t * cmd)
641 vnet_main_t *vnm = vnet_get_main ();
642 ip4_main_t *im = &ip4_main;
643 clib_error_t *error = 0;
645 u32 sw_if_index = ~0;
646 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
647 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
654 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
660 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
662 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
667 (input, "tcp-out-vrf %d",
668 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]))
672 (input, "udp-out-vrf %d",
673 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]))
677 (input, "tcp-in-vrf %d",
678 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]))
682 (input, "udp-in-vrf %d",
683 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]))
685 else if (unformat (input, "del"))
691 if (sw_if_index == ~0)
692 return clib_error_return (0, "Interface required but not specified");
695 return clib_error_return (0,
696 "TCP or UDP VRF ID required but not specified");
698 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
702 return clib_error_return (0,
703 "TCP, UDP VRF ID should not be 0 (default). Should be distinct VRF for this purpose. ");
707 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
710 return clib_error_return (0, "Invalid VRF ID %d", vrf_id[i]);
716 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index, is_add);
724 return clib_error_return
726 "set source and port-range on interface returned an unexpected value: %d",
733 * Add the 'ip4-source-and-port-range-check-rx' or
734 * 'ip4-source-and-port-range-check-tx' graph node for a given
735 * interface. 'tcp-out-vrf' and 'udp-out-vrf' will add to
736 * the RX path. 'tcp-in-vrf' and 'udp-in-vrf' will add to
737 * the TX path. A graph node will be inserted into the chain when
738 * the range check is added to the first interface. It will not
739 * be removed from when range check is removed from the last
742 * By adding the range check graph node to the interface, incoming
743 * or outgoing TCP/UDP packets will be validated using the
744 * provided IPv4 FIB table (VRF).
746 * @note 'ip4-source-and-port-range-check-rx' and
747 * 'ip4-source-and-port-range-check-tx' strings are too long, so
748 * they are truncated on the 'show vlib graph' output.
750 * @todo This content needs to be validated and potentially more detail added.
754 * Example of graph node before range checking is enabled:
755 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
757 * ip4-source-and-port-range- ip4-drop [0]
760 * Example of how to enable range checking on TX:
761 * @cliexcmd{set interface ip source-and-port-range-check GigabitEthernet2/0/0
764 * Example of graph node after range checking is enabled:
765 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
767 * ip4-source-and-port-range- ip4-drop [0] ip4-rewrite
768 * interface-output [1]
771 * Example of how to display the features enabled on an interface:
772 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
773 * IP feature paths configured on GigabitEthernet2/0/0...
776 * ip4-source-and-port-range-check-rx
780 * ip4-lookup-multicast
796 VLIB_CLI_COMMAND (set_interface_ip_source_and_port_range_check_command, static) = {
797 .path = "set interface ip source-and-port-range-check",
798 .function = set_ip_source_and_port_range_check_fn,
799 .short_help = "set interface ip source-and-port-range-check <interface> [tcp-out-vrf <table-id>] [udp-out-vrf <table-id>] [tcp-in-vrf <table-id>] [udp-in-vrf <table-id>] [del]",
803 format_ppr_dpo (u8 * s, va_list * args)
805 index_t index = va_arg (*args, index_t);
806 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
808 protocol_port_range_dpo_t *ppr_dpo;
812 ppr_dpo = protocol_port_range_dpo_get (index);
814 s = format (s, "allow ");
816 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
818 for (j = 0; j < 8; j++)
820 if (ppr_dpo->blocks[i].low.as_u16[j])
823 s = format (s, ", ");
824 if (ppr_dpo->blocks[i].hi.as_u16[j] >
825 (ppr_dpo->blocks[i].low.as_u16[j] + 1))
827 format (s, "%d-%d", (u32) ppr_dpo->blocks[i].low.as_u16[j],
828 (u32) ppr_dpo->blocks[i].hi.as_u16[j] - 1);
830 s = format (s, "%d", ppr_dpo->blocks[i].low.as_u16[j]);
839 ppr_dpo_lock (dpo_id_t * dpo)
844 ppr_dpo_unlock (dpo_id_t * dpo)
848 const static dpo_vft_t ppr_vft = {
849 .dv_lock = ppr_dpo_lock,
850 .dv_unlock = ppr_dpo_unlock,
851 .dv_format = format_ppr_dpo,
854 const static char *const ppr_ip4_nodes[] = {
855 "ip4-source-and-port-range-check-rx",
859 const static char *const *const ppr_nodes[DPO_PROTO_NUM] = {
860 [DPO_PROTO_IP4] = ppr_ip4_nodes,
864 ip4_source_and_port_range_check_init (vlib_main_t * vm)
866 source_range_check_main_t *srm = &source_range_check_main;
869 srm->vnet_main = vnet_get_main ();
871 ppr_dpo_type = dpo_register_new_type (&ppr_vft, ppr_nodes);
876 VLIB_INIT_FUNCTION (ip4_source_and_port_range_check_init);
878 protocol_port_range_dpo_t *
879 protocol_port_range_dpo_alloc (void)
881 protocol_port_range_dpo_t *ppr_dpo;
883 pool_get_aligned (ppr_dpo_pool, ppr_dpo, CLIB_CACHE_LINE_BYTES);
884 clib_memset (ppr_dpo, 0, sizeof (*ppr_dpo));
886 ppr_dpo->n_free_ranges = N_PORT_RANGES_PER_DPO;
893 add_port_range_adjacency (u32 fib_index,
894 ip4_address_t * address,
895 u32 length, u16 * low_ports, u16 * high_ports)
897 protocol_port_range_dpo_t *ppr_dpo;
898 dpo_id_t dpop = DPO_INVALID;
901 fib_node_index_t fei;
903 .fp_proto = FIB_PROTOCOL_IP4,
911 * check to see if we have already sourced this prefix
913 fei = fib_table_lookup_exact_match (fib_index, &pfx);
915 if (FIB_NODE_INDEX_INVALID == fei)
918 * this is a first time add for this prefix.
920 ppr_dpo = protocol_port_range_dpo_alloc ();
925 * the prefix is already there.
926 * check it was sourced by us, and if so get the ragne DPO from it.
928 dpo_id_t dpo = DPO_INVALID;
929 const dpo_id_t *bucket;
931 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
934 * there is existing state. we'll want to add the new ranges to it
937 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
938 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
944 * there is no PPR state associated with this prefix,
945 * so we'll need a new DPO
947 ppr_dpo = protocol_port_range_dpo_alloc ();
951 if (vec_len (low_ports) > ppr_dpo->n_free_ranges)
952 return VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
956 for (i = 0; i < vec_len (low_ports); i++)
958 for (; j < N_BLOCKS_PER_DPO; j++)
962 if (ppr_dpo->blocks[j].low.as_u16[k] == 0)
964 ppr_dpo->blocks[j].low.as_u16[k] = low_ports[i];
965 ppr_dpo->blocks[j].hi.as_u16[k] = high_ports[i];
972 ppr_dpo->n_used_blocks = j + 1;
975 * add or update the entry in the FIB
977 dpo_set (&dpop, ppr_dpo_type, DPO_PROTO_IP4, (ppr_dpo - ppr_dpo_pool));
979 if (FIB_NODE_INDEX_INVALID == fei)
981 fib_table_entry_special_dpo_add (fib_index,
984 FIB_ENTRY_FLAG_NONE, &dpop);
988 fib_entry_special_update (fei,
990 FIB_ENTRY_FLAG_NONE, &dpop);
997 remove_port_range_adjacency (u32 fib_index,
998 ip4_address_t * address,
999 u32 length, u16 * low_ports, u16 * high_ports)
1001 protocol_port_range_dpo_t *ppr_dpo;
1002 fib_node_index_t fei;
1005 fib_prefix_t pfx = {
1006 .fp_proto = FIB_PROTOCOL_IP4,
1014 * check to see if we have sourced this prefix
1016 fei = fib_table_lookup_exact_match (fib_index, &pfx);
1018 if (FIB_NODE_INDEX_INVALID == fei)
1023 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1028 * the prefix is already there.
1029 * check it was sourced by us
1031 dpo_id_t dpo = DPO_INVALID;
1032 const dpo_id_t *bucket;
1034 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
1037 * there is existing state. we'll want to add the new ranges to it
1040 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1041 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1049 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1053 for (i = 0; i < vec_len (low_ports); i++)
1055 for (j = 0; j < N_BLOCKS_PER_DPO; j++)
1057 for (k = 0; k < 8; k++)
1059 if (low_ports[i] == ppr_dpo->blocks[j].low.as_u16[k] &&
1060 high_ports[i] == ppr_dpo->blocks[j].hi.as_u16[k])
1062 ppr_dpo->blocks[j].low.as_u16[k] =
1063 ppr_dpo->blocks[j].hi.as_u16[k] = 0;
1071 ppr_dpo->n_free_ranges = 0;
1073 /* Have we deleted all ranges yet? */
1074 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1076 for (j = 0; j < 8; j++)
1078 if (ppr_dpo->blocks[j].low.as_u16[i] == 0)
1079 ppr_dpo->n_free_ranges++;
1083 if (N_PORT_RANGES_PER_DPO == ppr_dpo->n_free_ranges)
1085 /* Yes, lose the adjacency... */
1086 fib_table_entry_special_remove (fib_index, &pfx, FIB_SOURCE_SPECIAL);
1091 * compact the ranges down to a contiguous block
1099 // This will be moved to another file and implemented post API freeze.
1101 ip6_source_and_port_range_check_add_del (ip6_address_t * address,
1105 u16 * high_ports, int is_add)
1109 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1111 ASSERT (~0 != fib_index);
1113 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY);
1119 ip4_source_and_port_range_check_add_del (ip4_address_t * address,
1123 u16 * high_ports, int is_add)
1127 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1128 FIB_SOURCE_CLASSIFY);
1132 remove_port_range_adjacency (fib_index, address, length,
1133 low_ports, high_ports);
1137 add_port_range_adjacency (fib_index, address, length,
1138 low_ports, high_ports);
1144 static clib_error_t *
1145 ip_source_and_port_range_check_command_fn (vlib_main_t * vm,
1146 unformat_input_t * input,
1147 vlib_cli_command_t * cmd)
1150 u16 *high_ports = 0;
1153 ip4_address_t ip4_addr;
1154 ip6_address_t ip6_addr; //This function will be moved to generic impl when v6 done.
1158 int is_add = 1, ip_ver = ~0;
1162 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1164 if (unformat (input, "%U/%d", unformat_ip4_address, &ip4_addr, &length))
1168 (input, "%U/%d", unformat_ip6_address, &ip6_addr, &length))
1170 else if (unformat (input, "vrf %d", &vrf_id))
1172 else if (unformat (input, "del"))
1174 else if (unformat (input, "port %d", &tmp))
1176 if (tmp == 0 || tmp > 65535)
1177 return clib_error_return (0, "port %d out of range", tmp);
1179 this_hi = this_low + 1;
1180 vec_add1 (low_ports, this_low);
1181 vec_add1 (high_ports, this_hi);
1183 else if (unformat (input, "range %d - %d", &tmp, &tmp2))
1186 return clib_error_return (0, "ports %d and %d out of order",
1188 if (tmp == 0 || tmp > 65535)
1189 return clib_error_return (0, "low port %d out of range", tmp);
1190 if (tmp2 == 0 || tmp2 > 65535)
1191 return clib_error_return (0, "high port %d out of range", tmp2);
1194 vec_add1 (low_ports, this_low);
1195 vec_add1 (high_ports, this_hi);
1202 return clib_error_return (0, " <address>/<mask> not specified");
1205 return clib_error_return (0, " VRF ID required, not specified");
1207 if (vec_len (low_ports) == 0)
1208 return clib_error_return (0,
1209 " Both VRF ID and range/port must be set for a protocol.");
1212 return clib_error_return (0, " VRF ID can not be 0 (default).");
1216 rv = ip4_source_and_port_range_check_add_del
1217 (&ip4_addr, length, vrf_id, low_ports, high_ports, is_add);
1219 return clib_error_return (0, " IPv6 in subsequent patch");
1226 case VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE:
1227 return clib_error_return
1228 (0, " Incorrect adjacency for add/del operation");
1230 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_PORTS_CAPACITY:
1231 return clib_error_return (0, " Too many ports in add/del operation");
1233 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY:
1234 return clib_error_return
1235 (0, " Too many ranges requested for add operation");
1238 return clib_error_return (0, " returned an unexpected value: %d", rv);
1245 * This command adds an IP Subnet and range of ports to be validated
1246 * by an IP FIB table (VRF).
1248 * @todo This is incomplete. This needs a detailed description and a
1249 * practical example.
1252 * Example of how to add an IPv4 subnet and single port to an IPv4 FIB table:
1253 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23}
1254 * Example of how to add an IPv4 subnet and range of ports to an IPv4 FIB table:
1255 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100}
1256 * Example of how to delete an IPv4 subnet and single port from an IPv4 FIB table:
1257 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23 del}
1258 * Example of how to delete an IPv4 subnet and range of ports from an IPv4 FIB table:
1259 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100 del}
1261 VLIB_CLI_COMMAND (ip_source_and_port_range_check_command, static) = {
1262 .path = "set ip source-and-port-range-check",
1263 .function = ip_source_and_port_range_check_command_fn,
1265 "set ip source-and-port-range-check vrf <table-id> <ip-addr>/<mask> {port nn | range <nn> - <nn>} [del]",
1269 static clib_error_t *
1270 show_source_and_port_range_check_fn (vlib_main_t * vm,
1271 unformat_input_t * input,
1272 vlib_cli_command_t * cmd)
1274 protocol_port_range_dpo_t *ppr_dpo;
1280 fib_prefix_t pfx = {
1281 .fp_proto = FIB_PROTOCOL_IP4,
1285 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1287 if (unformat (input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1289 else if (unformat (input, "vrf %d", &vrf_id))
1291 else if (unformat (input, "port %d", &port))
1298 return clib_error_return (0, "<address> not specified");
1301 return clib_error_return (0, "VRF ID required, not specified");
1303 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1304 if (~0 == fib_index)
1305 return clib_error_return (0, "VRF %d not found", vrf_id);
1308 * find the longest prefix match on the address requested,
1309 * check it was sourced by us
1311 dpo_id_t dpo = DPO_INVALID;
1312 const dpo_id_t *bucket;
1314 if (!fib_entry_get_dpo_for_source (fib_table_lookup (fib_index, &pfx),
1315 FIB_SOURCE_SPECIAL, &dpo))
1320 vlib_cli_output (vm, "%U: src address drop", format_ip4_address,
1325 bucket = load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1326 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1331 rv = check_adj_port_range_x1 (ppr_dpo, (u16) port, 1234);
1333 vlib_cli_output (vm, "%U port %d PASS", format_ip4_address,
1334 &pfx.fp_addr.ip4, port);
1336 vlib_cli_output (vm, "%U port %d FAIL", format_ip4_address,
1337 &pfx.fp_addr.ip4, port);
1344 s = format (0, "%U: ", format_ip4_address, &pfx.fp_addr.ip4);
1346 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1348 for (j = 0; j < 8; j++)
1350 if (ppr_dpo->blocks[i].low.as_u16[j])
1351 s = format (s, "%d - %d ",
1352 (u32) ppr_dpo->blocks[i].low.as_u16[j],
1353 (u32) ppr_dpo->blocks[i].hi.as_u16[j]);
1356 vlib_cli_output (vm, "%s", s);
1364 * Display the range of ports being validated by an IPv4 FIB for a given
1365 * IP or subnet, or test if a given IP and port are being validated.
1367 * @todo This is incomplete. This needs a detailed description and a
1368 * practical example.
1371 * Example of how to display the set of ports being validated for a given
1373 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.0}
1374 * 172.16.2.0: 23 - 101
1376 * Example of how to test to determine of a given iPv4 address and port
1377 * are being validated:
1378 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 23}
1379 * 172.16.2.2 port 23 PASS
1381 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 250}
1382 * 172.16.2.2 port 250 FAIL
1385 VLIB_CLI_COMMAND (show_source_and_port_range_check, static) = {
1386 .path = "show ip source-and-port-range-check",
1387 .function = show_source_and_port_range_check_fn,
1389 "show ip source-and-port-range-check vrf <table-id> <ip-addr> [port <n>]",
1393 * fd.io coding-style-patch-verification: ON
1396 * eval: (c-set-style "gnu")