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>
23 * @brief IPv4 Source and Port Range Checking.
25 * This file contains the source code for IPv4 source and port range
31 * @brief The pool of range chack DPOs
33 static protocol_port_range_dpo_t *ppr_dpo_pool;
36 * @brief Dynamically registered DPO type
38 static dpo_type_t ppr_dpo_type;
40 vlib_node_registration_t ip4_source_port_and_range_check_rx;
41 vlib_node_registration_t ip4_source_port_and_range_check_tx;
43 #define foreach_ip4_source_and_port_range_check_error \
44 _(CHECK_FAIL, "ip4 source and port range check bad packets") \
45 _(CHECK_OK, "ip4 source and port range check good packets")
49 #define _(sym,str) IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_##sym,
50 foreach_ip4_source_and_port_range_check_error
52 IP4_SOURCE_AND_PORT_RANGE_CHECK_N_ERROR,
53 } ip4_source_and_port_range_check_error_t;
55 static char *ip4_source_and_port_range_check_error_strings[] = {
56 #define _(sym,string) string,
57 foreach_ip4_source_and_port_range_check_error
66 ip4_address_t src_addr;
69 } ip4_source_and_port_range_check_trace_t;
72 format_ip4_source_and_port_range_check_trace (u8 * s, va_list * va)
74 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
75 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
76 ip4_source_and_port_range_check_trace_t *t =
77 va_arg (*va, ip4_source_and_port_range_check_trace_t *);
80 s = format (s, "PASS (bypass case)");
82 s = format (s, "fib %d src ip %U %s dst port %d: %s",
83 t->fib_index, format_ip4_address, &t->src_addr,
84 t->is_tcp ? "TCP" : "UDP", (u32) t->port,
85 (t->pass == 1) ? "PASS" : "FAIL");
91 IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP,
92 IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
93 } ip4_source_and_port_range_check_next_t;
97 check_adj_port_range_x1 (const protocol_port_range_dpo_t * ppr_dpo,
98 u16 dst_port, u32 next)
103 u16x8vec_t sum, sum_equal_diff2;
104 u16 sum_nonzero, sum_equal, winner_mask;
107 if (NULL == ppr_dpo || dst_port == 0)
108 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
110 /* Make the obvious screw-case work. A variant also works w/ no MMX */
111 if (PREDICT_FALSE (dst_port == 65535))
116 i < VLIB_BUFFER_PRE_DATA_SIZE / sizeof (protocol_port_range_t);
119 for (j = 0; j < 8; j++)
120 if (ppr_dpo->blocks[i].low.as_u16[j] == 65535)
123 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
126 key.as_u16x8 = u16x8_splat (dst_port);
128 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
131 u16x8_sub_saturate (ppr_dpo->blocks[i].low.as_u16x8, key.as_u16x8);
133 u16x8_sub_saturate (ppr_dpo->blocks[i].hi.as_u16x8, key.as_u16x8);
134 sum.as_u16x8 = u16x8_add (diff1.as_u16x8, diff2.as_u16x8);
135 sum_equal_diff2.as_u16x8 =
136 u16x8_is_equal (sum.as_u16x8, diff2.as_u16x8);
137 sum_nonzero = ~u16x8_zero_byte_mask (sum.as_u16x8);
138 sum_equal = ~u16x8_zero_byte_mask (sum_equal_diff2.as_u16x8);
139 winner_mask = sum_nonzero & sum_equal;
143 return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
146 always_inline protocol_port_range_dpo_t *
147 protocol_port_range_dpo_get (index_t index)
149 return (pool_elt_at_index (ppr_dpo_pool, index));
153 ip4_source_and_port_range_check_inline (vlib_main_t * vm,
154 vlib_node_runtime_t * node,
155 vlib_frame_t * frame, int is_tx)
157 ip4_main_t *im = &ip4_main;
158 ip_lookup_main_t *lm = &im->lookup_main;
159 ip_config_main_t *rx_cm =
160 &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
161 ip_config_main_t *tx_cm = &lm->feature_config_mains[VNET_IP_TX_FEAT];
162 u32 n_left_from, *from, *to_next;
164 vlib_node_runtime_t *error_node = node;
165 u32 good_packets = 0;
168 from = vlib_frame_vector_args (frame);
169 n_left_from = frame->n_vectors;
170 next_index = node->cached_next_index;
172 while (n_left_from > 0)
176 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
179 /* while (n_left_from >= 4 && n_left_to_next >= 2) */
181 /* vlib_buffer_t *b0, *b1; */
182 /* ip4_header_t *ip0, *ip1; */
183 /* ip4_fib_mtrie_t *mtrie0, *mtrie1; */
184 /* ip4_fib_mtrie_leaf_t leaf0, leaf1; */
185 /* ip_source_and_port_range_check_config_t *c0, *c1; */
186 /* ip_adjacency_t *adj0 = 0, *adj1 = 0; */
187 /* u32 bi0, next0, adj_index0, pass0, save_next0, fib_index0; */
188 /* u32 bi1, next1, adj_index1, pass1, save_next1, fib_index1; */
189 /* udp_header_t *udp0, *udp1; */
191 /* /\* Prefetch next iteration. *\/ */
193 /* vlib_buffer_t *p2, *p3; */
195 /* p2 = vlib_get_buffer (vm, from[2]); */
196 /* p3 = vlib_get_buffer (vm, from[3]); */
198 /* vlib_prefetch_buffer_header (p2, LOAD); */
199 /* vlib_prefetch_buffer_header (p3, LOAD); */
201 /* CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); */
202 /* CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD); */
205 /* bi0 = to_next[0] = from[0]; */
206 /* bi1 = to_next[1] = from[1]; */
209 /* n_left_from -= 2; */
210 /* n_left_to_next -= 2; */
212 /* b0 = vlib_get_buffer (vm, bi0); */
213 /* b1 = vlib_get_buffer (vm, bi1); */
216 /* vec_elt (im->fib_index_by_sw_if_index, */
217 /* vnet_buffer (b0)->sw_if_index[VLIB_RX]); */
219 /* vec_elt (im->fib_index_by_sw_if_index, */
220 /* vnet_buffer (b1)->sw_if_index[VLIB_RX]); */
222 /* ip0 = vlib_buffer_get_current (b0); */
223 /* ip1 = vlib_buffer_get_current (b1); */
227 /* c0 = vnet_get_config_data (&tx_cm->config_main, */
228 /* &b0->current_config_index, */
229 /* &next0, sizeof (c0[0])); */
230 /* c1 = vnet_get_config_data (&tx_cm->config_main, */
231 /* &b1->current_config_index, */
232 /* &next1, sizeof (c1[0])); */
236 /* c0 = vnet_get_config_data (&rx_cm->config_main, */
237 /* &b0->current_config_index, */
238 /* &next0, sizeof (c0[0])); */
239 /* c1 = vnet_get_config_data (&rx_cm->config_main, */
240 /* &b1->current_config_index, */
241 /* &next1, sizeof (c1[0])); */
244 /* /\* we can't use the default VRF here... *\/ */
245 /* for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++) */
247 /* ASSERT (c0->fib_index[i] && c1->fib_index[i]); */
253 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
256 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
257 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
260 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
264 /* if (ip0->protocol == IP_PROTOCOL_UDP) */
267 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
268 /* if (ip0->protocol == IP_PROTOCOL_TCP) */
271 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
274 /* if (PREDICT_TRUE (fib_index0 != ~0)) */
277 /* mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie; */
279 /* leaf0 = IP4_FIB_MTRIE_LEAF_ROOT; */
281 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
282 /* &ip0->src_address, 0); */
284 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
285 /* &ip0->src_address, 1); */
287 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
288 /* &ip0->src_address, 2); */
290 /* leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
291 /* &ip0->src_address, 3); */
293 /* adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); */
295 /* ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0, */
296 /* &ip0->src_address, */
298 /* /\* use dflt rt *\/ */
300 /* adj0 = ip_get_adjacency (lm, adj_index0); */
305 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
308 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
309 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
312 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
316 /* if (ip1->protocol == IP_PROTOCOL_UDP) */
319 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
320 /* if (ip1->protocol == IP_PROTOCOL_TCP) */
323 /* [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
326 /* if (PREDICT_TRUE (fib_index1 != ~0)) */
329 /* mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie; */
331 /* leaf1 = IP4_FIB_MTRIE_LEAF_ROOT; */
333 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
334 /* &ip1->src_address, 0); */
336 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
337 /* &ip1->src_address, 1); */
339 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
340 /* &ip1->src_address, 2); */
342 /* leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
343 /* &ip1->src_address, 3); */
345 /* adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); */
347 /* ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1, */
348 /* &ip1->src_address, */
350 /* adj1 = ip_get_adjacency (lm, adj_index1); */
354 /* pass0 |= adj0 == 0; */
355 /* pass0 |= ip4_address_is_multicast (&ip0->src_address); */
357 /* ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
358 /* pass0 |= (ip0->protocol != IP_PROTOCOL_UDP) */
359 /* && (ip0->protocol != IP_PROTOCOL_TCP); */
362 /* pass1 |= adj1 == 0; */
363 /* pass1 |= ip4_address_is_multicast (&ip1->src_address); */
365 /* ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
366 /* pass1 |= (ip1->protocol != IP_PROTOCOL_UDP) */
367 /* && (ip1->protocol != IP_PROTOCOL_TCP); */
369 /* save_next0 = next0; */
370 /* udp0 = ip4_next_header (ip0); */
371 /* save_next1 = next1; */
372 /* udp1 = ip4_next_header (ip1); */
374 /* if (PREDICT_TRUE (pass0 == 0)) */
376 /* good_packets++; */
377 /* next0 = check_adj_port_range_x1 */
378 /* (adj0, clib_net_to_host_u16 (udp0->dst_port), next0); */
379 /* good_packets -= (save_next0 != next0); */
380 /* b0->error = error_node->errors */
381 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
384 /* if (PREDICT_TRUE (pass1 == 0)) */
386 /* good_packets++; */
387 /* next1 = check_adj_port_range_x1 */
388 /* (adj1, clib_net_to_host_u16 (udp1->dst_port), next1); */
389 /* good_packets -= (save_next1 != next1); */
390 /* b1->error = error_node->errors */
391 /* [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
394 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
395 /* && (b0->flags & VLIB_BUFFER_IS_TRACED))) */
397 /* ip4_source_and_port_range_check_trace_t *t = */
398 /* vlib_add_trace (vm, node, b0, sizeof (*t)); */
399 /* t->pass = next0 == save_next0; */
400 /* t->bypass = pass0; */
401 /* t->fib_index = fib_index0; */
402 /* t->src_addr.as_u32 = ip0->src_address.as_u32; */
403 /* t->port = (pass0 == 0) ? */
404 /* clib_net_to_host_u16 (udp0->dst_port) : 0; */
405 /* t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP; */
408 /* if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
409 /* && (b1->flags & VLIB_BUFFER_IS_TRACED))) */
411 /* ip4_source_and_port_range_check_trace_t *t = */
412 /* vlib_add_trace (vm, node, b1, sizeof (*t)); */
413 /* t->pass = next1 == save_next1; */
414 /* t->bypass = pass1; */
415 /* t->fib_index = fib_index1; */
416 /* t->src_addr.as_u32 = ip1->src_address.as_u32; */
417 /* t->port = (pass1 == 0) ? */
418 /* clib_net_to_host_u16 (udp1->dst_port) : 0; */
419 /* t->is_tcp = ip1->protocol == IP_PROTOCOL_TCP; */
422 /* vlib_validate_buffer_enqueue_x2 (vm, node, next_index, */
423 /* to_next, n_left_to_next, */
424 /* bi0, bi1, next0, next1); */
427 while (n_left_from > 0 && n_left_to_next > 0)
431 ip_source_and_port_range_check_config_t *c0;
432 u32 bi0, next0, lb_index0, pass0, save_next0, fib_index0;
434 const protocol_port_range_dpo_t *ppr_dpo0 = NULL;
444 b0 = vlib_get_buffer (vm, bi0);
447 vec_elt (im->fib_index_by_sw_if_index,
448 vnet_buffer (b0)->sw_if_index[VLIB_RX]);
451 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
453 ip0 = vlib_buffer_get_current (b0);
457 c0 = vnet_get_config_data
458 (&tx_cm->config_main, &b0->current_config_index,
459 &next0, sizeof (c0[0]));
463 c0 = vnet_get_config_data
464 (&rx_cm->config_main, &b0->current_config_index,
465 &next0, sizeof (c0[0]));
468 /* we can't use the default VRF here... */
469 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
471 ASSERT (c0->fib_index[i]);
477 if (ip0->protocol == IP_PROTOCOL_UDP)
480 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN];
481 if (ip0->protocol == IP_PROTOCOL_TCP)
484 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN];
488 if (ip0->protocol == IP_PROTOCOL_UDP)
491 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT];
492 if (ip0->protocol == IP_PROTOCOL_TCP)
495 [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT];
498 if (fib_index0 != ~0)
500 lb_index0 = ip4_fib_forwarding_lookup (fib_index0,
504 load_balance_get_bucket_i (load_balance_get (lb_index0), 0);
506 if (ppr_dpo_type == dpo->dpoi_type)
508 ppr_dpo0 = protocol_port_range_dpo_get (dpo->dpoi_index);
511 * else the lookup hit an enty that was no inserted
512 * by this range checker, which is the default route
516 * $$$ which (src,dst) categories should we always pass?
519 pass0 |= ip4_address_is_multicast (&ip0->src_address);
521 ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
522 pass0 |= (ip0->protocol != IP_PROTOCOL_UDP)
523 && (ip0->protocol != IP_PROTOCOL_TCP);
526 udp0 = ip4_next_header (ip0);
528 if (PREDICT_TRUE (pass0 == 0))
531 next0 = check_adj_port_range_x1
532 (ppr_dpo0, clib_net_to_host_u16 (udp0->dst_port), next0);
533 good_packets -= (save_next0 != next0);
534 b0->error = error_node->errors
535 [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL];
538 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
539 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
541 ip4_source_and_port_range_check_trace_t *t =
542 vlib_add_trace (vm, node, b0, sizeof (*t));
543 t->pass = next0 == save_next0;
545 t->fib_index = fib_index0;
546 t->src_addr.as_u32 = ip0->src_address.as_u32;
547 t->port = (pass0 == 0) ?
548 clib_net_to_host_u16 (udp0->dst_port) : 0;
549 t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP;
553 vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
555 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
556 to_next, n_left_to_next,
560 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
564 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_tx.index,
565 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
568 vlib_node_increment_counter (vm, ip4_source_port_and_range_check_rx.index,
569 IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
571 return frame->n_vectors;
576 ip4_source_and_port_range_check_rx (vlib_main_t * vm,
577 vlib_node_runtime_t * node,
578 vlib_frame_t * frame)
580 return ip4_source_and_port_range_check_inline (vm, node, frame,
585 ip4_source_and_port_range_check_tx (vlib_main_t * vm,
586 vlib_node_runtime_t * node,
587 vlib_frame_t * frame)
589 return ip4_source_and_port_range_check_inline (vm, node, frame,
593 /* Note: Calling same function for both RX and TX nodes
594 as always checking dst_port, although
595 if this changes can easily make new function
599 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_rx) = {
600 .function = ip4_source_and_port_range_check_rx,
601 .name = "ip4-source-and-port-range-check-rx",
602 .vector_size = sizeof (u32),
604 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
605 .error_strings = ip4_source_and_port_range_check_error_strings,
607 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
609 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "error-drop",
612 .format_buffer = format_ip4_header,
613 .format_trace = format_ip4_source_and_port_range_check_trace,
618 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_tx) = {
619 .function = ip4_source_and_port_range_check_tx,
620 .name = "ip4-source-and-port-range-check-tx",
621 .vector_size = sizeof (u32),
623 .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
624 .error_strings = ip4_source_and_port_range_check_error_strings,
626 .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
628 [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "error-drop",
631 .format_buffer = format_ip4_header,
632 .format_trace = format_ip4_source_and_port_range_check_trace,
637 set_ip_source_and_port_range_check (vlib_main_t * vm,
639 u32 sw_if_index, u32 is_add)
641 ip4_main_t *im = &ip4_main;
642 ip_lookup_main_t *lm = &im->lookup_main;
643 ip_config_main_t *rx_cm =
644 &lm->feature_config_mains[VNET_IP_RX_UNICAST_FEAT];
645 ip_config_main_t *tx_cm = &lm->feature_config_mains[VNET_IP_TX_FEAT];
647 ip_source_and_port_range_check_config_t config;
652 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
654 config.fib_index[i] = fib_index[i];
657 /* For OUT we are in the RX path */
658 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] != ~0) ||
659 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] != ~0))
661 feature_index = im->ip4_unicast_rx_feature_source_and_port_range_check;
663 vec_validate (rx_cm->config_index_by_sw_if_index, sw_if_index);
665 ci = rx_cm->config_index_by_sw_if_index[sw_if_index];
667 ? vnet_config_add_feature
668 : vnet_config_del_feature)
669 (vm, &rx_cm->config_main, ci, feature_index, &config,
671 rx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
674 /* For IN we are in the TX path */
675 if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] != ~0) ||
676 (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] != ~0))
678 feature_index = im->ip4_unicast_tx_feature_source_and_port_range_check;
680 vec_validate (tx_cm->config_index_by_sw_if_index, sw_if_index);
682 ci = tx_cm->config_index_by_sw_if_index[sw_if_index];
684 ? vnet_config_add_feature
685 : vnet_config_del_feature)
686 (vm, &tx_cm->config_main, ci, feature_index, &config,
688 tx_cm->config_index_by_sw_if_index[sw_if_index] = ci;
690 vnet_config_update_tx_feature_count (lm, tx_cm, sw_if_index, is_add);
695 static clib_error_t *
696 set_ip_source_and_port_range_check_fn (vlib_main_t * vm,
697 unformat_input_t * input,
698 vlib_cli_command_t * cmd)
700 vnet_main_t *vnm = vnet_get_main ();
701 ip4_main_t *im = &ip4_main;
702 clib_error_t *error = 0;
704 u32 sw_if_index = ~0;
705 u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
706 u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
713 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
719 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
721 if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
726 (input, "tcp-out-vrf %d",
727 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]))
731 (input, "udp-out-vrf %d",
732 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]))
736 (input, "tcp-in-vrf %d",
737 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]))
741 (input, "udp-in-vrf %d",
742 &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]))
744 else if (unformat (input, "del"))
750 if (sw_if_index == ~0)
751 return clib_error_return (0, "Interface required but not specified");
754 return clib_error_return (0,
755 "TCP or UDP VRF ID required but not specified");
757 for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
761 return clib_error_return (0,
762 "TCP, UDP VRF ID should not be 0 (default). Should be distinct VRF for this purpose. ");
766 p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
769 return clib_error_return (0, "Invalid VRF ID %d", vrf_id[i]);
775 set_ip_source_and_port_range_check (vm, fib_index, sw_if_index, is_add);
783 return clib_error_return
785 "set source and port-range on interface returned an unexpected value: %d",
792 * Add the 'ip4-source-and-port-range-check-rx' or
793 * 'ip4-source-and-port-range-check-tx' graph node for a given
794 * interface. 'tcp-out-vrf' and 'udp-out-vrf' will add to
795 * the RX path. 'tcp-in-vrf' and 'udp-in-vrf' will add to
796 * the TX path. A graph node will be inserted into the chain when
797 * the range check is added to the first interface. It will not
798 * be removed from when range check is removed from the last
801 * By adding the range check graph node to the interface, incoming
802 * or outgoing TCP/UDP packets will be validated using the
803 * provided IPv4 FIB table (VRF).
805 * @note 'ip4-source-and-port-range-check-rx' and
806 * 'ip4-source-and-port-range-check-tx' strings are too long, so
807 * they are truncated on the 'show vlib graph' output.
809 * @todo This content needs to be validated and potentially more detail added.
813 * Example of graph node before range checking is enabled:
814 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
816 * ip4-source-and-port-range- error-drop [0]
819 * Example of how to enable range checking on TX:
820 * @cliexcmd{set interface ip source-and-port-range-check GigabitEthernet2/0/0 udp-in-vrf 7}
822 * Example of graph node after range checking is enabled:
823 * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
825 * ip4-source-and-port-range- error-drop [0] ip4-rewrite-local
826 * interface-output [1] ip4-rewrite-transit
829 * Example of how to display the features enabed on an interface:
830 * @cliexstart{show ip interface features GigabitEthernet2/0/0}
831 * IP feature paths configured on GigabitEthernet2/0/0...
834 * ip4-source-and-port-range-check-rx
838 * ip4-lookup-multicast
855 VLIB_CLI_COMMAND (set_interface_ip_source_and_port_range_check_command, static) = {
856 .path = "set interface ip source-and-port-range-check",
857 .function = set_ip_source_and_port_range_check_fn,
858 .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]",
863 format_ppr_dpo (u8 * s, va_list * args)
865 index_t index = va_arg (*args, index_t);
866 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
868 protocol_port_range_dpo_t *ppr_dpo;
872 ppr_dpo = protocol_port_range_dpo_get (index);
874 s = format (s, "allow ");
876 for (i = 0; i < ppr_dpo->n_used_blocks; i++)
878 for (j = 0; j < 8; j++)
880 if (ppr_dpo->blocks[i].low.as_u16[j])
883 s = format (s, ", ");
884 if (ppr_dpo->blocks[i].hi.as_u16[j] >
885 (ppr_dpo->blocks[i].low.as_u16[j] + 1))
887 format (s, "%d-%d", (u32) ppr_dpo->blocks[i].low.as_u16[j],
888 (u32) ppr_dpo->blocks[i].hi.as_u16[j] - 1);
890 s = format (s, "%d", ppr_dpo->blocks[i].low.as_u16[j]);
899 ppr_dpo_lock (dpo_id_t * dpo)
904 ppr_dpo_unlock (dpo_id_t * dpo)
908 const static dpo_vft_t ppr_vft = {
909 .dv_lock = ppr_dpo_lock,
910 .dv_unlock = ppr_dpo_unlock,
911 .dv_format = format_ppr_dpo,
914 const static char *const ppr_ip4_nodes[] = {
915 "ip4-source-and-port-range-check-rx",
919 const static char *const *const ppr_nodes[DPO_PROTO_NUM] = {
920 [DPO_PROTO_IP4] = ppr_ip4_nodes,
924 ip4_source_and_port_range_check_init (vlib_main_t * vm)
926 source_range_check_main_t *srm = &source_range_check_main;
929 srm->vnet_main = vnet_get_main ();
931 ppr_dpo_type = dpo_register_new_type (&ppr_vft, ppr_nodes);
936 VLIB_INIT_FUNCTION (ip4_source_and_port_range_check_init);
938 protocol_port_range_dpo_t *
939 protocol_port_range_dpo_alloc (void)
941 protocol_port_range_dpo_t *ppr_dpo;
943 pool_get_aligned (ppr_dpo_pool, ppr_dpo, CLIB_CACHE_LINE_BYTES);
944 memset (ppr_dpo, 0, sizeof (*ppr_dpo));
946 ppr_dpo->n_free_ranges = N_PORT_RANGES_PER_DPO;
953 add_port_range_adjacency (u32 fib_index,
954 ip4_address_t * address,
955 u32 length, u16 * low_ports, u16 * high_ports)
957 protocol_port_range_dpo_t *ppr_dpo;
958 dpo_id_t dpop = DPO_NULL;
961 fib_node_index_t fei;
963 .fp_proto = FIB_PROTOCOL_IP4,
971 * check to see if we have already sourced this prefix
973 fei = fib_table_lookup_exact_match (fib_index, &pfx);
975 if (FIB_NODE_INDEX_INVALID == fei)
978 * this is a first time add for this prefix.
980 ppr_dpo = protocol_port_range_dpo_alloc ();
985 * the prefix is already there.
986 * check it was sourced by us, and if so get the ragne DPO from it.
988 dpo_id_t dpo = DPO_NULL;
989 const dpo_id_t *bucket;
991 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
994 * there is existing state. we'll want to add the new ranges to it
997 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
998 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1004 * there is no PPR state associated with this prefix,
1005 * so we'll need a new DPO
1007 ppr_dpo = protocol_port_range_dpo_alloc ();
1011 if (vec_len (low_ports) > ppr_dpo->n_free_ranges)
1012 return VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
1016 for (i = 0; i < vec_len (low_ports); i++)
1018 for (; j < N_BLOCKS_PER_DPO; j++)
1022 if (ppr_dpo->blocks[j].low.as_u16[k] == 0)
1024 ppr_dpo->blocks[j].low.as_u16[k] = low_ports[i];
1025 ppr_dpo->blocks[j].hi.as_u16[k] = high_ports[i];
1032 ppr_dpo->n_used_blocks = j + 1;
1035 * add or update the entry in the FIB
1037 dpo_set (&dpop, ppr_dpo_type, DPO_PROTO_IP4, (ppr_dpo - ppr_dpo_pool));
1039 if (FIB_NODE_INDEX_INVALID == fei)
1041 fib_table_entry_special_dpo_add (fib_index,
1044 FIB_ENTRY_FLAG_NONE, &dpop);
1048 fib_table_entry_special_dpo_update (fei,
1050 FIB_ENTRY_FLAG_NONE, &dpop);
1057 remove_port_range_adjacency (u32 fib_index,
1058 ip4_address_t * address,
1059 u32 length, u16 * low_ports, u16 * high_ports)
1061 protocol_port_range_dpo_t *ppr_dpo;
1062 fib_node_index_t fei;
1065 fib_prefix_t pfx = {
1066 .fp_proto = FIB_PROTOCOL_IP4,
1074 * check to see if we have sourced this prefix
1076 fei = fib_table_lookup_exact_match (fib_index, &pfx);
1078 if (FIB_NODE_INDEX_INVALID == fei)
1083 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1088 * the prefix is already there.
1089 * check it was sourced by us
1091 dpo_id_t dpo = DPO_NULL;
1092 const dpo_id_t *bucket;
1094 if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
1097 * there is existing state. we'll want to add the new ranges to it
1100 load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1101 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1109 return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1113 for (i = 0; i < vec_len (low_ports); i++)
1115 for (j = 0; j < N_BLOCKS_PER_DPO; j++)
1117 for (k = 0; k < 8; k++)
1119 if (low_ports[i] == ppr_dpo->blocks[j].low.as_u16[k] &&
1120 high_ports[i] == ppr_dpo->blocks[j].hi.as_u16[k])
1122 ppr_dpo->blocks[j].low.as_u16[k] =
1123 ppr_dpo->blocks[j].hi.as_u16[k] = 0;
1131 ppr_dpo->n_free_ranges = 0;
1133 /* Have we deleted all ranges yet? */
1134 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1136 for (j = 0; j < 8; j++)
1138 if (ppr_dpo->blocks[j].low.as_u16[i] == 0)
1139 ppr_dpo->n_free_ranges++;
1143 if (N_PORT_RANGES_PER_DPO == ppr_dpo->n_free_ranges)
1145 /* Yes, lose the adjacency... */
1146 fib_table_entry_special_remove (fib_index, &pfx, FIB_SOURCE_SPECIAL);
1151 * compact the ranges down to a contiguous block
1159 // This will be moved to another file and implemented post API freeze.
1161 ip6_source_and_port_range_check_add_del (ip6_address_t * address,
1165 u16 * high_ports, int is_add)
1171 ip4_source_and_port_range_check_add_del (ip4_address_t * address,
1175 u16 * high_ports, int is_add)
1179 fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id);
1183 remove_port_range_adjacency (fib_index, address, length,
1184 low_ports, high_ports);
1188 add_port_range_adjacency (fib_index, address, length,
1189 low_ports, high_ports);
1195 static clib_error_t *
1196 ip_source_and_port_range_check_command_fn (vlib_main_t * vm,
1197 unformat_input_t * input,
1198 vlib_cli_command_t * cmd)
1201 u16 *high_ports = 0;
1204 ip4_address_t ip4_addr;
1205 ip6_address_t ip6_addr; //This function will be moved to generic impl when v6 done.
1209 int is_add = 1, ip_ver = ~0;
1213 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1215 if (unformat (input, "%U/%d", unformat_ip4_address, &ip4_addr, &length))
1219 (input, "%U/%d", unformat_ip6_address, &ip6_addr, &length))
1221 else if (unformat (input, "vrf %d", &vrf_id))
1223 else if (unformat (input, "del"))
1225 else if (unformat (input, "port %d", &tmp))
1227 if (tmp == 0 || tmp > 65535)
1228 return clib_error_return (0, "port %d out of range", tmp);
1230 this_hi = this_low + 1;
1231 vec_add1 (low_ports, this_low);
1232 vec_add1 (high_ports, this_hi);
1234 else if (unformat (input, "range %d - %d", &tmp, &tmp2))
1237 return clib_error_return (0, "ports %d and %d out of order",
1239 if (tmp == 0 || tmp > 65535)
1240 return clib_error_return (0, "low port %d out of range", tmp);
1241 if (tmp2 == 0 || tmp2 > 65535)
1242 return clib_error_return (0, "high port %d out of range", tmp2);
1245 vec_add1 (low_ports, this_low);
1246 vec_add1 (high_ports, this_hi);
1253 return clib_error_return (0, " <address>/<mask> not specified");
1256 return clib_error_return (0, " VRF ID required, not specified");
1258 if (vec_len (low_ports) == 0)
1259 return clib_error_return (0,
1260 " Both VRF ID and range/port must be set for a protocol.");
1263 return clib_error_return (0, " VRF ID can not be 0 (default).");
1267 rv = ip4_source_and_port_range_check_add_del
1268 (&ip4_addr, length, vrf_id, low_ports, high_ports, is_add);
1270 return clib_error_return (0, " IPv6 in subsequent patch");
1277 case VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE:
1278 return clib_error_return
1279 (0, " Incorrect adjacency for add/del operation");
1281 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_PORTS_CAPACITY:
1282 return clib_error_return (0, " Too many ports in add/del operation");
1284 case VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY:
1285 return clib_error_return
1286 (0, " Too many ranges requested for add operation");
1289 return clib_error_return (0, " returned an unexpected value: %d", rv);
1296 * This command adds an IP Subnet and range of ports to be validated
1297 * by an IP FIB table (VRF).
1299 * @todo This is incomplete. This needs a detailed description and a
1300 * practical example.
1303 * Example of how to add an IPv4 subnet and single port to an IPv4 FIB table:
1304 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23}
1305 * Example of how to add an IPv4 subnet and range of ports to an IPv4 FIB table:
1306 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100}
1307 * Example of how to delete an IPv4 subnet and single port from an IPv4 FIB table:
1308 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23 del}
1309 * Example of how to delete an IPv4 subnet and range of ports from an IPv4 FIB table:
1310 * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100 del}
1313 VLIB_CLI_COMMAND (ip_source_and_port_range_check_command, static) = {
1314 .path = "set ip source-and-port-range-check",
1315 .function = ip_source_and_port_range_check_command_fn,
1317 "set ip source-and-port-range-check vrf <table-id> <ip-addr>/<mask> {port nn | range <nn> - <nn>} [del]",
1322 static clib_error_t *
1323 show_source_and_port_range_check_fn (vlib_main_t * vm,
1324 unformat_input_t * input,
1325 vlib_cli_command_t * cmd)
1327 protocol_port_range_dpo_t *ppr_dpo;
1333 fib_prefix_t pfx = {
1334 .fp_proto = FIB_PROTOCOL_IP4,
1338 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1340 if (unformat (input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1342 else if (unformat (input, "vrf %d", &vrf_id))
1344 else if (unformat (input, "port %d", &port))
1351 return clib_error_return (0, "<address> not specified");
1354 return clib_error_return (0, "VRF ID required, not specified");
1356 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1357 if (~0 == fib_index)
1358 return clib_error_return (0, "VRF %d not found", vrf_id);
1361 * find the longest prefix match on the address requested,
1362 * check it was sourced by us
1364 dpo_id_t dpo = DPO_NULL;
1365 const dpo_id_t *bucket;
1367 if (!fib_entry_get_dpo_for_source (fib_table_lookup (fib_index, &pfx),
1368 FIB_SOURCE_SPECIAL, &dpo))
1373 vlib_cli_output (vm, "%U: src address drop", format_ip4_address,
1378 bucket = load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1379 ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1384 rv = check_adj_port_range_x1 (ppr_dpo, (u16) port, 1234);
1386 vlib_cli_output (vm, "%U port %d PASS", format_ip4_address,
1387 &pfx.fp_addr.ip4, port);
1389 vlib_cli_output (vm, "%U port %d FAIL", format_ip4_address,
1390 &pfx.fp_addr.ip4, port);
1397 s = format (0, "%U: ", format_ip4_address, &pfx.fp_addr.ip4);
1399 for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1401 for (j = 0; j < 8; j++)
1403 if (ppr_dpo->blocks[i].low.as_u16[j])
1404 s = format (s, "%d - %d ",
1405 (u32) ppr_dpo->blocks[i].low.as_u16[j],
1406 (u32) ppr_dpo->blocks[i].hi.as_u16[j]);
1409 vlib_cli_output (vm, "%s", s);
1417 * Display the range of ports being validated by an IPv4 FIB for a given
1418 * IP or subnet, or test if a given IP and port are being validated.
1420 * @todo This is incomplete. This needs a detailed description and a
1421 * practical example.
1424 * Example of how to display the set of ports being validated for a given
1426 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.0}
1427 * 172.16.2.0: 23 - 101
1429 * Example of how to test to determine of a given Pv4 address and port
1430 * are being validated:
1431 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 23}
1432 * 172.16.2.2 port 23 PASS
1434 * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 250}
1435 * 172.16.2.2 port 250 FAIL
1439 VLIB_CLI_COMMAND (show_source_and_port_range_check, static) = {
1440 .path = "show ip source-and-port-range-check",
1441 .function = show_source_and_port_range_check_fn,
1443 "show ip source-and-port-range-check vrf <table-id> <ip-addr> [port <n>]",
1448 * fd.io coding-style-patch-verification: ON
1451 * eval: (c-set-style "gnu")