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)
105 u16x8vec_t sum, sum_equal_diff2;
106 u16 sum_nonzero, sum_equal, winner_mask;
109 if (NULL == ppr_dpo || dst_port == 0)
110 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
112 /* Make the obvious screw-case work. A variant also works w/ no MMX */
113 if (PREDICT_FALSE (dst_port == 65535))
118 i < VLIB_BUFFER_PRE_DATA_SIZE / sizeof (protocol_port_range_t);
121 for (j = 0; j < 8; j++)
122 if (ppr_dpo->blocks[i].low.as_u16[j] == 65535)
125 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
128 key.as_u16x8 = u16x8_splat (dst_port);
130 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
133 u16x8_sub_saturate (ppr_dpo->blocks[i].low.as_u16x8, key.as_u16x8);
135 u16x8_sub_saturate (ppr_dpo->blocks[i].hi.as_u16x8, key.as_u16x8);
136 sum.as_u16x8 = diff1.as_u16x8 + diff2.as_u16x8;
137 sum_equal_diff2.as_u16x8 = (sum.as_u16x8 == diff2.as_u16x8);
138 sum_nonzero = ~u16x8_zero_byte_mask (sum.as_u16x8);
139 sum_equal = ~u16x8_zero_byte_mask (sum_equal_diff2.as_u16x8);
140 winner_mask = sum_nonzero & sum_equal;
144 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
147 always_inline protocol_port_range_dpo_t *
148 protocol_port_range_dpo_get (index_t index)
150 return (pool_elt_at_index (ppr_dpo_pool, index));
154 ip4_source_and_port_range_check_inline (vlib_main_t * vm,
155 vlib_node_runtime_t * node,
156 vlib_frame_t * frame, int is_tx)
158 ip4_main_t *im = &ip4_main;
159 u32 n_left_from, *from, *to_next;
161 vlib_node_runtime_t *error_node = node;
162 u32 good_packets = 0;
165 from = vlib_frame_vector_args (frame);
166 n_left_from = frame->n_vectors;
167 next_index = node->cached_next_index;
169 while (n_left_from > 0)
173 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
176 /* while (n_left_from >= 4 && n_left_to_next >= 2) */
178 /* vlib_buffer_t *b0, *b1; */
179 /* ip4_header_t *ip0, *ip1; */
180 /* ip4_fib_mtrie_t *mtrie0, *mtrie1; */
181 /* ip4_fib_mtrie_leaf_t leaf0, leaf1; */
182 /* ip_source_and_port_range_check_config_t *c0, *c1; */
183 /* ip_adjacency_t *adj0 = 0, *adj1 = 0; */
184 /* u32 bi0, next0, adj_index0, pass0, save_next0, fib_index0; */
185 /* u32 bi1, next1, adj_index1, pass1, save_next1, fib_index1; */
186 /* udp_header_t *udp0, *udp1; */
188 /* /\* Prefetch next iteration. *\/ */
190 /* vlib_buffer_t *p2, *p3; */
192 /* p2 = vlib_get_buffer (vm, from[2]); */
193 /* p3 = vlib_get_buffer (vm, from[3]); */
195 /* vlib_prefetch_buffer_header (p2, LOAD); */
196 /* vlib_prefetch_buffer_header (p3, LOAD); */
198 /* CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); */
199 /* CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD); */
202 /* bi0 = to_next[0] = from[0]; */
203 /* bi1 = to_next[1] = from[1]; */
206 /* n_left_from -= 2; */
207 /* n_left_to_next -= 2; */
209 /* b0 = vlib_get_buffer (vm, bi0); */
210 /* b1 = vlib_get_buffer (vm, bi1); */
213 /* vec_elt (im->fib_index_by_sw_if_index, */
214 /* vnet_buffer (b0)->sw_if_index[VLIB_RX]); */
216 /* vec_elt (im->fib_index_by_sw_if_index, */
217 /* vnet_buffer (b1)->sw_if_index[VLIB_RX]); */
219 /* ip0 = vlib_buffer_get_current (b0); */
220 /* ip1 = vlib_buffer_get_current (b1); */
224 /* c0 = vnet_get_config_data (&tx_cm->config_main, */
225 /* &b0->current_config_index, */
226 /* &next0, sizeof (c0[0])); */
227 /* c1 = vnet_get_config_data (&tx_cm->config_main, */
228 /* &b1->current_config_index, */
229 /* &next1, sizeof (c1[0])); */
233 /* c0 = vnet_get_config_data (&rx_cm->config_main, */
234 /* &b0->current_config_index, */
235 /* &next0, sizeof (c0[0])); */
236 /* c1 = vnet_get_config_data (&rx_cm->config_main, */
237 /* &b1->current_config_index, */
238 /* &next1, sizeof (c1[0])); */
241 /* /\* we can't use the default VRF here... *\/ */
242 /* for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++) */
244 /* ASSERT (c0->fib_index[i] && c1->fib_index[i]); */
250 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
253 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
254 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
257 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
261 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
264 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
265 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
268 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
271 /* if (PREDICT_TRUE (fib_index0 != ~0)) */
274 /* mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie; */
276 /* leaf0 = IP4_FIB_MTRIE_LEAF_ROOT; */
278 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
279 /* &ip0->src_address, 0); */
281 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
282 /* &ip0->src_address, 1); */
284 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
285 /* &ip0->src_address, 2); */
287 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
288 /* &ip0->src_address, 3); */
290 /* adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); */
292 /* ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0, */
293 /* &ip0->src_address, */
295 /* /\* use dflt rt *\/ */
297 /* adj0 = ip_get_adjacency (lm, adj_index0); */
302 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
305 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
306 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
309 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
313 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
316 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
317 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
320 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
323 /* if (PREDICT_TRUE (fib_index1 != ~0)) */
326 /* mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie; */
328 /* leaf1 = IP4_FIB_MTRIE_LEAF_ROOT; */
330 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
331 /* &ip1->src_address, 0); */
333 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
334 /* &ip1->src_address, 1); */
336 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
337 /* &ip1->src_address, 2); */
339 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
340 /* &ip1->src_address, 3); */
342 /* adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); */
344 /* ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1, */
345 /* &ip1->src_address, */
347 /* adj1 = ip_get_adjacency (lm, adj_index1); */
351 /* pass0 |= adj0 == 0; */
352 /* pass0 |= ip4_address_is_multicast (&ip0->src_address); */
354 /* ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
355 /* pass0 |= (ip0->protocol != IP_PROTOCOL_UDP) */
356 /* && (ip0->protocol != IP_PROTOCOL_TCP); */
359 /* pass1 |= adj1 == 0; */
360 /* pass1 |= ip4_address_is_multicast (&ip1->src_address); */
362 /* ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
363 /* pass1 |= (ip1->protocol != IP_PROTOCOL_UDP) */
364 /* && (ip1->protocol != IP_PROTOCOL_TCP); */
366 /* save_next0 = next0; */
367 /* udp0 = ip4_next_header (ip0); */
368 /* save_next1 = next1; */
369 /* udp1 = ip4_next_header (ip1); */
371 /* if (PREDICT_TRUE (pass0 == 0)) */
373 /* good_packets++; */
374 /* next0 = check_adj_port_range_x1 */
375 /* (adj0, clib_net_to_host_u16 (udp0->dst_port), next0); */
376 /* good_packets -= (save_next0 != next0); */
377 /* b0->error = error_node->errors */
378 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
381 /* if (PREDICT_TRUE (pass1 == 0)) */
383 /* good_packets++; */
384 /* next1 = check_adj_port_range_x1 */
385 /* (adj1, clib_net_to_host_u16 (udp1->dst_port), next1); */
386 /* good_packets -= (save_next1 != next1); */
387 /* b1->error = error_node->errors */
388 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
391 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
392 /* && (b0->flags & VLIB_BUFFER_IS_TRACED))) */
394 /* ip4_source_and_port_range_check_trace_t *t = */
395 /* vlib_add_trace (vm, node, b0, sizeof (*t)); */
396 /* t->pass = next0 == save_next0; */
397 /* t->bypass = pass0; */
398 /* t->fib_index = fib_index0; */
399 /* t->src_addr.as_u32 = ip0->src_address.as_u32; */
400 /* t->port = (pass0 == 0) ? */
401 /* clib_net_to_host_u16 (udp0->dst_port) : 0; */
402 /* t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP; */
405 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
406 /* && (b1->flags & VLIB_BUFFER_IS_TRACED))) */
408 /* ip4_source_and_port_range_check_trace_t *t = */
409 /* vlib_add_trace (vm, node, b1, sizeof (*t)); */
410 /* t->pass = next1 == save_next1; */
411 /* t->bypass = pass1; */
412 /* t->fib_index = fib_index1; */
413 /* t->src_addr.as_u32 = ip1->src_address.as_u32; */
414 /* t->port = (pass1 == 0) ? */
415 /* clib_net_to_host_u16 (udp1->dst_port) : 0; */
416 /* t->is_tcp = ip1->protocol == IP_PROTOCOL_TCP; */
419 /* vlib_validate_buffer_enqueue_x2 (vm, node, next_index, */
420 /* to_next, n_left_to_next, */
421 /* bi0, bi1, next0, next1); */
424 while (n_left_from > 0 && n_left_to_next > 0)
428 ip_source_and_port_range_check_config_t *c0;
429 u32 bi0, next0, lb_index0, pass0, save_next0, fib_index0;
431 const protocol_port_range_dpo_t *ppr_dpo0 = NULL;
442 b0 = vlib_get_buffer (vm, bi0);
443 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
445 fib_index0 = vec_elt (im->fib_index_by_sw_if_index, sw_if_index0);
448 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
450 ip0 = vlib_buffer_get_current (b0);
452 c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
454 /* we can't use the default VRF here... */
455 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
457 ASSERT (c0->fib_index[i]);
463 if (ip0->protocol == IP_PROTOCOL_UDP)
466 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN];
467 if (ip0->protocol == IP_PROTOCOL_TCP)
470 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN];
474 if (ip0->protocol == IP_PROTOCOL_UDP)
477 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT];
478 if (ip0->protocol == IP_PROTOCOL_TCP)
481 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT];
484 if (fib_index0 != ~0)
486 lb_index0 = ip4_fib_forwarding_lookup (fib_index0,
490 load_balance_get_bucket_i (load_balance_get (lb_index0), 0);
492 if (ppr_dpo_type == dpo->dpoi_type)
494 ppr_dpo0 = protocol_port_range_dpo_get (dpo->dpoi_index);
497 * else the lookup hit an enty that was no inserted
498 * by this range checker, which is the default route
502 * $$$ which (src,dst) categories should we always pass?
505 pass0 |= ip4_address_is_multicast (&ip0->src_address);
507 ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
508 pass0 |= (ip0->protocol != IP_PROTOCOL_UDP)
509 && (ip0->protocol != IP_PROTOCOL_TCP);
512 udp0 = ip4_next_header (ip0);
514 if (PREDICT_TRUE (pass0 == 0))
517 next0 = check_adj_port_range_x1
518 (ppr_dpo0, clib_net_to_host_u16 (udp0->dst_port), next0);
519 good_packets -= (save_next0 != next0);
520 b0->error = error_node->errors
521 [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL];
524 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
525 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
527 ip4_source_and_port_range_check_trace_t *t =
528 vlib_add_trace (vm, node, b0, sizeof (*t));
529 t->pass = next0 == save_next0;
531 t->fib_index = fib_index0;
532 t->src_addr.as_u32 = ip0->src_address.as_u32;
533 t->port = (pass0 == 0) ?
534 clib_net_to_host_u16 (udp0->dst_port) : 0;
535 t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP;
539 vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
541 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
542 to_next, n_left_to_next,
546 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
550 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_tx.index,
551 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
554 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_rx.index,
555 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
557 return frame->n_vectors;
561 ip4_source_and_port_range_check_rx (vlib_main_t * vm,
562 vlib_node_runtime_t * node,
563 vlib_frame_t * frame)
565 return ip4_source_and_port_range_check_inline (vm, node, frame,
570 ip4_source_and_port_range_check_tx (vlib_main_t * vm,
571 vlib_node_runtime_t * node,
572 vlib_frame_t * frame)
574 return ip4_source_and_port_range_check_inline (vm, node, frame,
578 /* Note: Calling same function for both RX and TX nodes
579 as always checking dst_port, although
580 if this changes can easily make new function
584 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_rx) = {
585 .function = ip4_source_and_port_range_check_rx,
586 .name = "ip4-source-and-port-range-check-rx",
587 .vector_size = sizeof (u32),
589 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
590 .error_strings = ip4_source_and_port_range_check_error_strings,
592 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
594 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
597 .format_buffer = format_ip4_header,
598 .format_trace = format_ip4_source_and_port_range_check_trace,
603 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_tx) = {
604 .function = ip4_source_and_port_range_check_tx,
605 .name = "ip4-source-and-port-range-check-tx",
606 .vector_size = sizeof (u32),
608 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
609 .error_strings = ip4_source_and_port_range_check_error_strings,
611 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
613 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
616 .format_buffer = format_ip4_header,
617 .format_trace = format_ip4_source_and_port_range_check_trace,
622 set_ip_source_and_port_range_check (vlib_main_t * vm,
624 u32 sw_if_index, u32 is_add)
626 ip_source_and_port_range_check_config_t config;
630 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
632 config.fib_index[i] = fib_index[i];
635 /* For OUT we are in the RX path */
636 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] != ~0) ||
637 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] != ~0))
639 vnet_feature_enable_disable ("ip4-unicast",
640 "ip4-source-and-port-range-check-rx",
641 sw_if_index, is_add, &config,
645 /* For IN we are in the TX path */
646 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] != ~0) ||
647 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] != ~0))
649 vnet_feature_enable_disable ("ip4-output",
650 "ip4-source-and-port-range-check-tx",
651 sw_if_index, is_add, &config,
657 static clib_error_t *
658 set_ip_source_and_port_range_check_fn (vlib_main_t * vm,
659 unformat_input_t * input,
660 vlib_cli_command_t * cmd)
662 vnet_main_t *vnm = vnet_get_main ();
663 ip4_main_t *im = &ip4_main;
664 clib_error_t *error = 0;
666 u32 sw_if_index = ~0;
667 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
668 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
675 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
681 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
683 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
688 (input, "tcp-out-vrf %d",
689 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]))
693 (input, "udp-out-vrf %d",
694 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]))
698 (input, "tcp-in-vrf %d",
699 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]))
703 (input, "udp-in-vrf %d",
704 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]))
706 else if (unformat (input, "del"))
712 if (sw_if_index == ~0)
713 return clib_error_return (0, "Interface required but not specified");
716 return clib_error_return (0,
717 "TCP or UDP VRF ID required but not specified");
719 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
723 return clib_error_return (0,
724 "TCP, UDP VRF ID should not be 0 (default). Should be distinct VRF for this purpose. ");
728 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
731 return clib_error_return (0, "Invalid VRF ID %d", vrf_id[i]);
737 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index, is_add);
745 return clib_error_return
747 "set source and port-range on interface returned an unexpected value: %d",
754 * Add the 'ip4-source-and-port-range-check-rx' or
755 * 'ip4-source-and-port-range-check-tx' graph node for a given
756 * interface. 'tcp-out-vrf' and 'udp-out-vrf' will add to
757 * the RX path. 'tcp-in-vrf' and 'udp-in-vrf' will add to
758 * the TX path. A graph node will be inserted into the chain when
759 * the range check is added to the first interface. It will not
760 * be removed from when range check is removed from the last
763 * By adding the range check graph node to the interface, incoming
764 * or outgoing TCP/UDP packets will be validated using the
765 * provided IPv4 FIB table (VRF).
767 * @note 'ip4-source-and-port-range-check-rx' and
768 * 'ip4-source-and-port-range-check-tx' strings are too long, so
769 * they are truncated on the 'show vlib graph' output.
771 * @todo This content needs to be validated and potentially more detail added.
775 * Example of graph node before range checking is enabled:
776 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
778 * ip4-source-and-port-range- ip4-drop [0]
781 * Example of how to enable range checking on TX:
782 * @cliexcmd{set interface ip source-and-port-range-check GigabitEthernet2/0/0 udp-in-vrf 7}
784 * Example of graph node after range checking is enabled:
785 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
787 * ip4-source-and-port-range- ip4-drop [0] ip4-rewrite
788 * interface-output [1]
791 * Example of how to display the features enabed on an interface:
792 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
793 * IP feature paths configured on GigabitEthernet2/0/0...
796 * ip4-source-and-port-range-check-rx
800 * ip4-lookup-multicast
817 VLIB_CLI_COMMAND (set_interface_ip_source_and_port_range_check_command, static) = {
818 .path = "set interface ip source-and-port-range-check",
819 .function = set_ip_source_and_port_range_check_fn,
820 .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]",
825 format_ppr_dpo (u8 * s, va_list * args)
827 index_t index = va_arg (*args, index_t);
828 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
830 protocol_port_range_dpo_t *ppr_dpo;
834 ppr_dpo = protocol_port_range_dpo_get (index);
836 s = format (s, "allow ");
838 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
840 for (j = 0; j < 8; j++)
842 if (ppr_dpo->blocks[i].low.as_u16[j])
845 s = format (s, ", ");
846 if (ppr_dpo->blocks[i].hi.as_u16[j] >
847 (ppr_dpo->blocks[i].low.as_u16[j] + 1))
849 format (s, "%d-%d", (u32) ppr_dpo->blocks[i].low.as_u16[j],
850 (u32) ppr_dpo->blocks[i].hi.as_u16[j] - 1);
852 s = format (s, "%d", ppr_dpo->blocks[i].low.as_u16[j]);
861 ppr_dpo_lock (dpo_id_t * dpo)
866 ppr_dpo_unlock (dpo_id_t * dpo)
870 const static dpo_vft_t ppr_vft = {
871 .dv_lock = ppr_dpo_lock,
872 .dv_unlock = ppr_dpo_unlock,
873 .dv_format = format_ppr_dpo,
876 const static char *const ppr_ip4_nodes[] = {
877 "ip4-source-and-port-range-check-rx",
881 const static char *const *const ppr_nodes[DPO_PROTO_NUM] = {
882 [DPO_PROTO_IP4] = ppr_ip4_nodes,
886 ip4_source_and_port_range_check_init (vlib_main_t * vm)
888 source_range_check_main_t *srm = &source_range_check_main;
891 srm->vnet_main = vnet_get_main ();
893 ppr_dpo_type = dpo_register_new_type (&ppr_vft, ppr_nodes);
898 VLIB_INIT_FUNCTION (ip4_source_and_port_range_check_init);
900 protocol_port_range_dpo_t *
901 protocol_port_range_dpo_alloc (void)
903 protocol_port_range_dpo_t *ppr_dpo;
905 pool_get_aligned (ppr_dpo_pool, ppr_dpo, CLIB_CACHE_LINE_BYTES);
906 clib_memset (ppr_dpo, 0, sizeof (*ppr_dpo));
908 ppr_dpo->n_free_ranges = N_PORT_RANGES_PER_DPO;
915 add_port_range_adjacency (u32 fib_index,
916 ip4_address_t * address,
917 u32 length, u16 * low_ports, u16 * high_ports)
919 protocol_port_range_dpo_t *ppr_dpo;
920 dpo_id_t dpop = DPO_INVALID;
923 fib_node_index_t fei;
925 .fp_proto = FIB_PROTOCOL_IP4,
933 * check to see if we have already sourced this prefix
935 fei = fib_table_lookup_exact_match (fib_index, &pfx);
937 if (FIB_NODE_INDEX_INVALID == fei)
940 * this is a first time add for this prefix.
942 ppr_dpo = protocol_port_range_dpo_alloc ();
947 * the prefix is already there.
948 * check it was sourced by us, and if so get the ragne DPO from it.
950 dpo_id_t dpo = DPO_INVALID;
951 const dpo_id_t *bucket;
953 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
956 * there is existing state. we'll want to add the new ranges to it
959 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
960 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
966 * there is no PPR state associated with this prefix,
967 * so we'll need a new DPO
969 ppr_dpo = protocol_port_range_dpo_alloc ();
973 if (vec_len (low_ports) > ppr_dpo->n_free_ranges)
974 return VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
978 for (i = 0; i < vec_len (low_ports); i++)
980 for (; j < N_BLOCKS_PER_DPO; j++)
984 if (ppr_dpo->blocks[j].low.as_u16[k] == 0)
986 ppr_dpo->blocks[j].low.as_u16[k] = low_ports[i];
987 ppr_dpo->blocks[j].hi.as_u16[k] = high_ports[i];
994 ppr_dpo->n_used_blocks = j + 1;
997 * add or update the entry in the FIB
999 dpo_set (&dpop, ppr_dpo_type, DPO_PROTO_IP4, (ppr_dpo - ppr_dpo_pool));
1001 if (FIB_NODE_INDEX_INVALID == fei)
1003 fib_table_entry_special_dpo_add (fib_index,
1006 FIB_ENTRY_FLAG_NONE, &dpop);
1010 fib_entry_special_update (fei,
1012 FIB_ENTRY_FLAG_NONE, &dpop);
1019 remove_port_range_adjacency (u32 fib_index,
1020 ip4_address_t * address,
1021 u32 length, u16 * low_ports, u16 * high_ports)
1023 protocol_port_range_dpo_t *ppr_dpo;
1024 fib_node_index_t fei;
1027 fib_prefix_t pfx = {
1028 .fp_proto = FIB_PROTOCOL_IP4,
1036 * check to see if we have sourced this prefix
1038 fei = fib_table_lookup_exact_match (fib_index, &pfx);
1040 if (FIB_NODE_INDEX_INVALID == fei)
1045 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1050 * the prefix is already there.
1051 * check it was sourced by us
1053 dpo_id_t dpo = DPO_INVALID;
1054 const dpo_id_t *bucket;
1056 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
1059 * there is existing state. we'll want to add the new ranges to it
1062 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1063 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1071 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1075 for (i = 0; i < vec_len (low_ports); i++)
1077 for (j = 0; j < N_BLOCKS_PER_DPO; j++)
1079 for (k = 0; k < 8; k++)
1081 if (low_ports[i] == ppr_dpo->blocks[j].low.as_u16[k] &&
1082 high_ports[i] == ppr_dpo->blocks[j].hi.as_u16[k])
1084 ppr_dpo->blocks[j].low.as_u16[k] =
1085 ppr_dpo->blocks[j].hi.as_u16[k] = 0;
1093 ppr_dpo->n_free_ranges = 0;
1095 /* Have we deleted all ranges yet? */
1096 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1098 for (j = 0; j < 8; j++)
1100 if (ppr_dpo->blocks[j].low.as_u16[i] == 0)
1101 ppr_dpo->n_free_ranges++;
1105 if (N_PORT_RANGES_PER_DPO == ppr_dpo->n_free_ranges)
1107 /* Yes, lose the adjacency... */
1108 fib_table_entry_special_remove (fib_index, &pfx, FIB_SOURCE_SPECIAL);
1113 * compact the ranges down to a contiguous block
1121 // This will be moved to another file and implemented post API freeze.
1123 ip6_source_and_port_range_check_add_del (ip6_address_t * address,
1127 u16 * high_ports, int is_add)
1131 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1133 ASSERT (~0 != fib_index);
1135 fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY);
1141 ip4_source_and_port_range_check_add_del (ip4_address_t * address,
1145 u16 * high_ports, int is_add)
1149 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1150 FIB_SOURCE_CLASSIFY);
1154 remove_port_range_adjacency (fib_index, address, length,
1155 low_ports, high_ports);
1159 add_port_range_adjacency (fib_index, address, length,
1160 low_ports, high_ports);
1166 static clib_error_t *
1167 ip_source_and_port_range_check_command_fn (vlib_main_t * vm,
1168 unformat_input_t * input,
1169 vlib_cli_command_t * cmd)
1172 u16 *high_ports = 0;
1175 ip4_address_t ip4_addr;
1176 ip6_address_t ip6_addr; //This function will be moved to generic impl when v6 done.
1180 int is_add = 1, ip_ver = ~0;
1184 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1186 if (unformat (input, "%U/%d", unformat_ip4_address, &ip4_addr, &length))
1190 (input, "%U/%d", unformat_ip6_address, &ip6_addr, &length))
1192 else if (unformat (input, "vrf %d", &vrf_id))
1194 else if (unformat (input, "del"))
1196 else if (unformat (input, "port %d", &tmp))
1198 if (tmp == 0 || tmp > 65535)
1199 return clib_error_return (0, "port %d out of range", tmp);
1201 this_hi = this_low + 1;
1202 vec_add1 (low_ports, this_low);
1203 vec_add1 (high_ports, this_hi);
1205 else if (unformat (input, "range %d - %d", &tmp, &tmp2))
1208 return clib_error_return (0, "ports %d and %d out of order",
1210 if (tmp == 0 || tmp > 65535)
1211 return clib_error_return (0, "low port %d out of range", tmp);
1212 if (tmp2 == 0 || tmp2 > 65535)
1213 return clib_error_return (0, "high port %d out of range", tmp2);
1216 vec_add1 (low_ports, this_low);
1217 vec_add1 (high_ports, this_hi);
1224 return clib_error_return (0, " <address>/<mask> not specified");
1227 return clib_error_return (0, " VRF ID required, not specified");
1229 if (vec_len (low_ports) == 0)
1230 return clib_error_return (0,
1231 " Both VRF ID and range/port must be set for a protocol.");
1234 return clib_error_return (0, " VRF ID can not be 0 (default).");
1238 rv = ip4_source_and_port_range_check_add_del
1239 (&ip4_addr, length, vrf_id, low_ports, high_ports, is_add);
1241 return clib_error_return (0, " IPv6 in subsequent patch");
1248 case VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE:
1249 return clib_error_return
1250 (0, " Incorrect adjacency for add/del operation");
1252 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_PORTS_CAPACITY:
1253 return clib_error_return (0, " Too many ports in add/del operation");
1255 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY:
1256 return clib_error_return
1257 (0, " Too many ranges requested for add operation");
1260 return clib_error_return (0, " returned an unexpected value: %d", rv);
1267 * This command adds an IP Subnet and range of ports to be validated
1268 * by an IP FIB table (VRF).
1270 * @todo This is incomplete. This needs a detailed description and a
1271 * practical example.
1274 * Example of how to add an IPv4 subnet and single port to an IPv4 FIB table:
1275 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23}
1276 * Example of how to add an IPv4 subnet and range of ports to an IPv4 FIB table:
1277 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100}
1278 * Example of how to delete an IPv4 subnet and single port from an IPv4 FIB table:
1279 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23 del}
1280 * Example of how to delete an IPv4 subnet and range of ports from an IPv4 FIB table:
1281 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100 del}
1284 VLIB_CLI_COMMAND (ip_source_and_port_range_check_command, static) = {
1285 .path = "set ip source-and-port-range-check",
1286 .function = ip_source_and_port_range_check_command_fn,
1288 "set ip source-and-port-range-check vrf <table-id> <ip-addr>/<mask> {port nn | range <nn> - <nn>} [del]",
1293 static clib_error_t *
1294 show_source_and_port_range_check_fn (vlib_main_t * vm,
1295 unformat_input_t * input,
1296 vlib_cli_command_t * cmd)
1298 protocol_port_range_dpo_t *ppr_dpo;
1304 fib_prefix_t pfx = {
1305 .fp_proto = FIB_PROTOCOL_IP4,
1309 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1311 if (unformat (input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1313 else if (unformat (input, "vrf %d", &vrf_id))
1315 else if (unformat (input, "port %d", &port))
1322 return clib_error_return (0, "<address> not specified");
1325 return clib_error_return (0, "VRF ID required, not specified");
1327 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1328 if (~0 == fib_index)
1329 return clib_error_return (0, "VRF %d not found", vrf_id);
1332 * find the longest prefix match on the address requested,
1333 * check it was sourced by us
1335 dpo_id_t dpo = DPO_INVALID;
1336 const dpo_id_t *bucket;
1338 if (!fib_entry_get_dpo_for_source (fib_table_lookup (fib_index, &pfx),
1339 FIB_SOURCE_SPECIAL, &dpo))
1344 vlib_cli_output (vm, "%U: src address drop", format_ip4_address,
1349 bucket = load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1350 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1355 rv = check_adj_port_range_x1 (ppr_dpo, (u16) port, 1234);
1357 vlib_cli_output (vm, "%U port %d PASS", format_ip4_address,
1358 &pfx.fp_addr.ip4, port);
1360 vlib_cli_output (vm, "%U port %d FAIL", format_ip4_address,
1361 &pfx.fp_addr.ip4, port);
1368 s = format (0, "%U: ", format_ip4_address, &pfx.fp_addr.ip4);
1370 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1372 for (j = 0; j < 8; j++)
1374 if (ppr_dpo->blocks[i].low.as_u16[j])
1375 s = format (s, "%d - %d ",
1376 (u32) ppr_dpo->blocks[i].low.as_u16[j],
1377 (u32) ppr_dpo->blocks[i].hi.as_u16[j]);
1380 vlib_cli_output (vm, "%s", s);
1388 * Display the range of ports being validated by an IPv4 FIB for a given
1389 * IP or subnet, or test if a given IP and port are being validated.
1391 * @todo This is incomplete. This needs a detailed description and a
1392 * practical example.
1395 * Example of how to display the set of ports being validated for a given
1397 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.0}
1398 * 172.16.2.0: 23 - 101
1400 * Example of how to test to determine of a given Pv4 address and port
1401 * are being validated:
1402 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 23}
1403 * 172.16.2.2 port 23 PASS
1405 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 250}
1406 * 172.16.2.2 port 250 FAIL
1410 VLIB_CLI_COMMAND (show_source_and_port_range_check, static) = {
1411 .path = "show ip source-and-port-range-check",
1412 .function = show_source_and_port_range_check_fn,
1414 "show ip source-and-port-range-check vrf <table-id> <ip-addr> [port <n>]",
1419 * fd.io coding-style-patch-verification: ON
1422 * eval: (c-set-style "gnu")