docs: vnet comment nitfixes
[vpp.git] / src / vnet / ip / ip4_source_and_port_range_check.c
1 /*
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:
6  *
7  *         http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
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>
20
21 source_range_check_main_t source_range_check_main;
22
23 /**
24  * @file
25  * @brief IPv4 Source and Port Range Checking.
26  *
27  * This file contains the source code for IPv4 source and port range
28  * checking.
29  */
30
31
32 /**
33  * @brief The pool of range chack DPOs
34  */
35 static protocol_port_range_dpo_t *ppr_dpo_pool;
36
37 /**
38  * @brief Dynamically registered DPO type
39  */
40 static dpo_type_t ppr_dpo_type;
41
42 vlib_node_registration_t ip4_source_port_and_range_check_rx;
43 vlib_node_registration_t ip4_source_port_and_range_check_tx;
44
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")
48
49 typedef enum
50 {
51 #define _(sym,str) IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_##sym,
52   foreach_ip4_source_and_port_range_check_error
53 #undef _
54     IP4_SOURCE_AND_PORT_RANGE_CHECK_N_ERROR,
55 } ip4_source_and_port_range_check_error_t;
56
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
60 #undef _
61 };
62
63 typedef struct
64 {
65   u32 pass;
66   u32 bypass;
67   u32 is_tcp;
68   ip4_address_t src_addr;
69   u16 port;
70   u32 fib_index;
71 } ip4_source_and_port_range_check_trace_t;
72
73 static u8 *
74 format_ip4_source_and_port_range_check_trace (u8 * s, va_list * va)
75 {
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 *);
80
81   if (t->bypass)
82     s = format (s, "PASS (bypass case)");
83   else
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");
88   return s;
89 }
90
91 typedef enum
92 {
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;
96
97
98 static inline u32
99 check_adj_port_range_x1 (const protocol_port_range_dpo_t * ppr_dpo,
100                          u16 dst_port, u32 next)
101 {
102   u16x8 key = u16x8_splat (dst_port);
103   int i;
104
105   if (NULL == ppr_dpo || dst_port == 0)
106     return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
107
108
109   for (i = 0; i < ppr_dpo->n_used_blocks; i++)
110     if (!u16x8_is_all_zero ((ppr_dpo->blocks[i].low.as_u16x8 <= key) &
111                             (ppr_dpo->blocks[i].hi.as_u16x8 >= key)))
112       return next;
113
114   return IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP;
115 }
116
117 always_inline protocol_port_range_dpo_t *
118 protocol_port_range_dpo_get (index_t index)
119 {
120   return (pool_elt_at_index (ppr_dpo_pool, index));
121 }
122
123 always_inline uword
124 ip4_source_and_port_range_check_inline (vlib_main_t * vm,
125                                         vlib_node_runtime_t * node,
126                                         vlib_frame_t * frame, int is_tx)
127 {
128   ip4_main_t *im = &ip4_main;
129   u32 n_left_from, *from, *to_next;
130   u32 next_index;
131   vlib_node_runtime_t *error_node = node;
132   u32 good_packets = 0;
133   int i;
134
135   from = vlib_frame_vector_args (frame);
136   n_left_from = frame->n_vectors;
137   next_index = node->cached_next_index;
138
139   while (n_left_from > 0)
140     {
141       u32 n_left_to_next;
142
143       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
144
145
146       /*     while (n_left_from >= 4 && n_left_to_next >= 2) */
147       /*       { */
148       /*         vlib_buffer_t *b0, *b1; */
149       /*         ip4_header_t *ip0, *ip1; */
150       /*         ip4_fib_mtrie_t *mtrie0, *mtrie1; */
151       /*         ip4_fib_mtrie_leaf_t leaf0, leaf1; */
152       /*         ip_source_and_port_range_check_config_t *c0, *c1; */
153       /*         ip_adjacency_t *adj0 = 0, *adj1 = 0; */
154       /*         u32 bi0, next0, adj_index0, pass0, save_next0, fib_index0; */
155       /*         u32 bi1, next1, adj_index1, pass1, save_next1, fib_index1; */
156       /*         udp_header_t *udp0, *udp1; */
157
158       /*         /\* Prefetch next iteration. *\/ */
159       /*         { */
160       /*           vlib_buffer_t *p2, *p3; */
161
162       /*           p2 = vlib_get_buffer (vm, from[2]); */
163       /*           p3 = vlib_get_buffer (vm, from[3]); */
164
165       /*           vlib_prefetch_buffer_header (p2, LOAD); */
166       /*           vlib_prefetch_buffer_header (p3, LOAD); */
167
168       /*           CLIB_PREFETCH (p2->data, sizeof (ip0[0]), LOAD); */
169       /*           CLIB_PREFETCH (p3->data, sizeof (ip1[0]), LOAD); */
170       /*         } */
171
172       /*         bi0 = to_next[0] = from[0]; */
173       /*         bi1 = to_next[1] = from[1]; */
174       /*         from += 2; */
175       /*         to_next += 2; */
176       /*         n_left_from -= 2; */
177       /*         n_left_to_next -= 2; */
178
179       /*         b0 = vlib_get_buffer (vm, bi0); */
180       /*         b1 = vlib_get_buffer (vm, bi1); */
181
182       /*         fib_index0 = */
183       /*           vec_elt (im->fib_index_by_sw_if_index, */
184       /*                 vnet_buffer (b0)->sw_if_index[VLIB_RX]); */
185       /*         fib_index1 = */
186       /*           vec_elt (im->fib_index_by_sw_if_index, */
187       /*                 vnet_buffer (b1)->sw_if_index[VLIB_RX]); */
188
189       /*         ip0 = vlib_buffer_get_current (b0); */
190       /*         ip1 = vlib_buffer_get_current (b1); */
191
192       /*         if (is_tx) */
193       /*           { */
194       /*             c0 = vnet_get_config_data (&tx_cm->config_main, */
195       /*                                     &b0->current_config_index, */
196       /*                                     &next0, sizeof (c0[0])); */
197       /*             c1 = vnet_get_config_data (&tx_cm->config_main, */
198       /*                                     &b1->current_config_index, */
199       /*                                     &next1, sizeof (c1[0])); */
200       /*           } */
201       /*         else */
202       /*           { */
203       /*             c0 = vnet_get_config_data (&rx_cm->config_main, */
204       /*                                     &b0->current_config_index, */
205       /*                                     &next0, sizeof (c0[0])); */
206       /*             c1 = vnet_get_config_data (&rx_cm->config_main, */
207       /*                                     &b1->current_config_index, */
208       /*                                     &next1, sizeof (c1[0])); */
209       /*           } */
210
211       /*         /\* we can't use the default VRF here... *\/ */
212       /*         for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++) */
213       /*           { */
214       /*             ASSERT (c0->fib_index[i] && c1->fib_index[i]); */
215       /*           } */
216
217
218       /*         if (is_tx) */
219       /*           { */
220       /*             if (ip0->protocol == IP_PROTOCOL_UDP) */
221       /*            fib_index0 = */
222       /*              c0->fib_index */
223       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
224       /*             if (ip0->protocol == IP_PROTOCOL_TCP) */
225       /*            fib_index0 = */
226       /*              c0->fib_index */
227       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
228       /*           } */
229       /*         else */
230       /*           { */
231       /*             if (ip0->protocol == IP_PROTOCOL_UDP) */
232       /*            fib_index0 = */
233       /*              c0->fib_index */
234       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
235       /*             if (ip0->protocol == IP_PROTOCOL_TCP) */
236       /*            fib_index0 = */
237       /*              c0->fib_index */
238       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
239       /*           } */
240
241       /*         if (PREDICT_TRUE (fib_index0 != ~0)) */
242       /*           { */
243
244       /*             mtrie0 = &vec_elt_at_index (im->fibs, fib_index0)->mtrie; */
245
246       /*             leaf0 = IP4_FIB_MTRIE_LEAF_ROOT; */
247
248       /*             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
249       /*                                             &ip0->src_address, 0); */
250
251       /*             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
252       /*                                             &ip0->src_address, 1); */
253
254       /*             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
255       /*                                             &ip0->src_address, 2); */
256
257       /*             leaf0 = ip4_fib_mtrie_lookup_step (mtrie0, leaf0, */
258       /*                                             &ip0->src_address, 3); */
259
260       /*             adj_index0 = ip4_fib_mtrie_leaf_get_adj_index (leaf0); */
261
262       /*             ASSERT (adj_index0 == ip4_fib_lookup_with_table (im, fib_index0, */
263       /*                                                           &ip0->src_address, */
264       /*                                                           0 */
265       /*                                                           /\* use dflt rt *\/ */
266       /*                  )); */
267       /*             adj0 = ip_get_adjacency (lm, adj_index0); */
268       /*           } */
269
270       /*         if (is_tx) */
271       /*           { */
272       /*             if (ip1->protocol == IP_PROTOCOL_UDP) */
273       /*            fib_index1 = */
274       /*              c1->fib_index */
275       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]; */
276       /*             if (ip1->protocol == IP_PROTOCOL_TCP) */
277       /*            fib_index1 = */
278       /*              c1->fib_index */
279       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]; */
280       /*           } */
281       /*         else */
282       /*           { */
283       /*             if (ip1->protocol == IP_PROTOCOL_UDP) */
284       /*            fib_index1 = */
285       /*              c1->fib_index */
286       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]; */
287       /*             if (ip1->protocol == IP_PROTOCOL_TCP) */
288       /*            fib_index1 = */
289       /*              c1->fib_index */
290       /*              [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]; */
291       /*           } */
292
293       /*         if (PREDICT_TRUE (fib_index1 != ~0)) */
294       /*           { */
295
296       /*             mtrie1 = &vec_elt_at_index (im->fibs, fib_index1)->mtrie; */
297
298       /*             leaf1 = IP4_FIB_MTRIE_LEAF_ROOT; */
299
300       /*             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
301       /*                                             &ip1->src_address, 0); */
302
303       /*             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
304       /*                                             &ip1->src_address, 1); */
305
306       /*             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
307       /*                                             &ip1->src_address, 2); */
308
309       /*             leaf1 = ip4_fib_mtrie_lookup_step (mtrie1, leaf1, */
310       /*                                             &ip1->src_address, 3); */
311
312       /*             adj_index1 = ip4_fib_mtrie_leaf_get_adj_index (leaf1); */
313
314       /*             ASSERT (adj_index1 == ip4_fib_lookup_with_table (im, fib_index1, */
315       /*                                                           &ip1->src_address, */
316       /*                                                           0)); */
317       /*             adj1 = ip_get_adjacency (lm, adj_index1); */
318       /*           } */
319
320       /*         pass0 = 0; */
321       /*         pass0 |= adj0 == 0; */
322       /*         pass0 |= ip4_address_is_multicast (&ip0->src_address); */
323       /*         pass0 |= */
324       /*           ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
325       /*         pass0 |= (ip0->protocol != IP_PROTOCOL_UDP) */
326       /*           && (ip0->protocol != IP_PROTOCOL_TCP); */
327
328       /*         pass1 = 0; */
329       /*         pass1 |= adj1 == 0; */
330       /*         pass1 |= ip4_address_is_multicast (&ip1->src_address); */
331       /*         pass1 |= */
332       /*           ip1->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF); */
333       /*         pass1 |= (ip1->protocol != IP_PROTOCOL_UDP) */
334       /*           && (ip1->protocol != IP_PROTOCOL_TCP); */
335
336       /*         save_next0 = next0; */
337       /*         udp0 = ip4_next_header (ip0); */
338       /*         save_next1 = next1; */
339       /*         udp1 = ip4_next_header (ip1); */
340
341       /*         if (PREDICT_TRUE (pass0 == 0)) */
342       /*           { */
343       /*             good_packets++; */
344       /*             next0 = check_adj_port_range_x1 */
345       /*            (adj0, clib_net_to_host_u16 (udp0->dst_port), next0); */
346       /*             good_packets -= (save_next0 != next0); */
347       /*             b0->error = error_node->errors */
348       /*            [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
349       /*           } */
350
351       /*         if (PREDICT_TRUE (pass1 == 0)) */
352       /*           { */
353       /*             good_packets++; */
354       /*             next1 = check_adj_port_range_x1 */
355       /*            (adj1, clib_net_to_host_u16 (udp1->dst_port), next1); */
356       /*             good_packets -= (save_next1 != next1); */
357       /*             b1->error = error_node->errors */
358       /*            [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL]; */
359       /*           } */
360
361       /*         if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
362       /*                         && (b0->flags & VLIB_BUFFER_IS_TRACED))) */
363       /*           { */
364       /*             ip4_source_and_port_range_check_trace_t *t = */
365       /*            vlib_add_trace (vm, node, b0, sizeof (*t)); */
366       /*             t->pass = next0 == save_next0; */
367       /*             t->bypass = pass0; */
368       /*             t->fib_index = fib_index0; */
369       /*             t->src_addr.as_u32 = ip0->src_address.as_u32; */
370       /*             t->port = (pass0 == 0) ? */
371       /*            clib_net_to_host_u16 (udp0->dst_port) : 0; */
372       /*             t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP; */
373       /*           } */
374
375       /*         if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) */
376       /*                         && (b1->flags & VLIB_BUFFER_IS_TRACED))) */
377       /*           { */
378       /*             ip4_source_and_port_range_check_trace_t *t = */
379       /*            vlib_add_trace (vm, node, b1, sizeof (*t)); */
380       /*             t->pass = next1 == save_next1; */
381       /*             t->bypass = pass1; */
382       /*             t->fib_index = fib_index1; */
383       /*             t->src_addr.as_u32 = ip1->src_address.as_u32; */
384       /*             t->port = (pass1 == 0) ? */
385       /*            clib_net_to_host_u16 (udp1->dst_port) : 0; */
386       /*             t->is_tcp = ip1->protocol == IP_PROTOCOL_TCP; */
387       /*           } */
388
389       /*         vlib_validate_buffer_enqueue_x2 (vm, node, next_index, */
390       /*                                       to_next, n_left_to_next, */
391       /*                                       bi0, bi1, next0, next1); */
392       /*       } */
393
394       while (n_left_from > 0 && n_left_to_next > 0)
395         {
396           vlib_buffer_t *b0;
397           ip4_header_t *ip0;
398           ip_source_and_port_range_check_config_t *c0;
399           u32 bi0, next0, lb_index0, pass0, save_next0, fib_index0;
400           udp_header_t *udp0;
401           const protocol_port_range_dpo_t *ppr_dpo0 = NULL;
402           const dpo_id_t *dpo;
403           u32 sw_if_index0;
404
405           bi0 = from[0];
406           to_next[0] = bi0;
407           from += 1;
408           to_next += 1;
409           n_left_from -= 1;
410           n_left_to_next -= 1;
411
412           b0 = vlib_get_buffer (vm, bi0);
413           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
414
415           fib_index0 = vec_elt (im->fib_index_by_sw_if_index, sw_if_index0);
416
417           if (is_tx)
418             vlib_buffer_advance (b0, sizeof (ethernet_header_t));
419
420           ip0 = vlib_buffer_get_current (b0);
421
422           c0 = vnet_feature_next_with_data (&next0, b0, sizeof (c0[0]));
423
424           /* we can't use the default VRF here... */
425           for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
426             {
427               ASSERT (c0->fib_index[i]);
428             }
429
430
431           if (is_tx)
432             {
433               if (ip0->protocol == IP_PROTOCOL_UDP)
434                 fib_index0 =
435                   c0->fib_index
436                   [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN];
437               if (ip0->protocol == IP_PROTOCOL_TCP)
438                 fib_index0 =
439                   c0->fib_index
440                   [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN];
441             }
442           else
443             {
444               if (ip0->protocol == IP_PROTOCOL_UDP)
445                 fib_index0 =
446                   c0->fib_index
447                   [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT];
448               if (ip0->protocol == IP_PROTOCOL_TCP)
449                 fib_index0 =
450                   c0->fib_index
451                   [IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT];
452             }
453
454           if (fib_index0 != ~0)
455             {
456               lb_index0 = ip4_fib_forwarding_lookup (fib_index0,
457                                                      &ip0->src_address);
458
459               dpo =
460                 load_balance_get_bucket_i (load_balance_get (lb_index0), 0);
461
462               if (ppr_dpo_type == dpo->dpoi_type)
463                 {
464                   ppr_dpo0 = protocol_port_range_dpo_get (dpo->dpoi_index);
465                 }
466               /*
467                * else the lookup hit an enty that was no inserted
468                * by this range checker, which is the default route
469                */
470             }
471           /*
472            * $$$ which (src,dst) categories should we always pass?
473            */
474           pass0 = 0;
475           pass0 |= ip4_address_is_multicast (&ip0->src_address);
476           pass0 |=
477             ip0->src_address.as_u32 == clib_host_to_net_u32 (0xFFFFFFFF);
478           pass0 |= (ip0->protocol != IP_PROTOCOL_UDP)
479             && (ip0->protocol != IP_PROTOCOL_TCP);
480
481           save_next0 = next0;
482           udp0 = ip4_next_header (ip0);
483
484           if (PREDICT_TRUE (pass0 == 0))
485             {
486               good_packets++;
487               next0 = check_adj_port_range_x1
488                 (ppr_dpo0, clib_net_to_host_u16 (udp0->dst_port), next0);
489               good_packets -= (save_next0 != next0);
490               b0->error = error_node->errors
491                 [IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_FAIL];
492             }
493
494           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
495                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
496             {
497               ip4_source_and_port_range_check_trace_t *t =
498                 vlib_add_trace (vm, node, b0, sizeof (*t));
499               t->pass = next0 == save_next0;
500               t->bypass = pass0;
501               t->fib_index = fib_index0;
502               t->src_addr.as_u32 = ip0->src_address.as_u32;
503               t->port = (pass0 == 0) ?
504                 clib_net_to_host_u16 (udp0->dst_port) : 0;
505               t->is_tcp = ip0->protocol == IP_PROTOCOL_TCP;
506             }
507
508           if (is_tx)
509             vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
510
511           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
512                                            to_next, n_left_to_next,
513                                            bi0, next0);
514         }
515
516       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
517     }
518
519   if (is_tx)
520     vlib_node_increment_counter (vm, ip4_source_port_and_range_check_tx.index,
521                                  IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
522                                  good_packets);
523   else
524     vlib_node_increment_counter (vm, ip4_source_port_and_range_check_rx.index,
525                                  IP4_SOURCE_AND_PORT_RANGE_CHECK_ERROR_CHECK_OK,
526                                  good_packets);
527   return frame->n_vectors;
528 }
529
530 static uword
531 ip4_source_and_port_range_check_rx (vlib_main_t * vm,
532                                     vlib_node_runtime_t * node,
533                                     vlib_frame_t * frame)
534 {
535   return ip4_source_and_port_range_check_inline (vm, node, frame,
536                                                  0 /* !is_tx */ );
537 }
538
539 static uword
540 ip4_source_and_port_range_check_tx (vlib_main_t * vm,
541                                     vlib_node_runtime_t * node,
542                                     vlib_frame_t * frame)
543 {
544   return ip4_source_and_port_range_check_inline (vm, node, frame,
545                                                  1 /* is_tx */ );
546 }
547
548 /* Note: Calling same function for both RX and TX nodes
549    as always checking dst_port, although
550    if this changes can easily make new function
551 */
552
553 /* *INDENT-OFF* */
554 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_rx) = {
555   .function = ip4_source_and_port_range_check_rx,
556   .name = "ip4-source-and-port-range-check-rx",
557   .vector_size = sizeof (u32),
558
559   .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
560   .error_strings = ip4_source_and_port_range_check_error_strings,
561
562   .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
563   .next_nodes = {
564     [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
565   },
566
567   .format_buffer = format_ip4_header,
568   .format_trace = format_ip4_source_and_port_range_check_trace,
569 };
570 /* *INDENT-ON* */
571
572 /* *INDENT-OFF* */
573 VLIB_REGISTER_NODE (ip4_source_port_and_range_check_tx) = {
574   .function = ip4_source_and_port_range_check_tx,
575   .name = "ip4-source-and-port-range-check-tx",
576   .vector_size = sizeof (u32),
577
578   .n_errors = ARRAY_LEN(ip4_source_and_port_range_check_error_strings),
579   .error_strings = ip4_source_and_port_range_check_error_strings,
580
581   .n_next_nodes = IP4_SOURCE_AND_PORT_RANGE_CHECK_N_NEXT,
582   .next_nodes = {
583     [IP4_SOURCE_AND_PORT_RANGE_CHECK_NEXT_DROP] = "ip4-drop",
584   },
585
586   .format_buffer = format_ip4_header,
587   .format_trace = format_ip4_source_and_port_range_check_trace,
588 };
589 /* *INDENT-ON* */
590
591 int
592 set_ip_source_and_port_range_check (vlib_main_t * vm,
593                                     u32 * fib_index,
594                                     u32 sw_if_index, u32 is_add)
595 {
596   ip_source_and_port_range_check_config_t config;
597   int rv = 0;
598   int i;
599
600   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
601     {
602       config.fib_index[i] = fib_index[i];
603     }
604
605   /* For OUT we are in the RX path */
606   if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT] != ~0) ||
607       (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT] != ~0))
608     {
609       vnet_feature_enable_disable ("ip4-unicast",
610                                    "ip4-source-and-port-range-check-rx",
611                                    sw_if_index, is_add, &config,
612                                    sizeof (config));
613     }
614
615   /* For IN we are in the TX path */
616   if ((fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN] != ~0) ||
617       (fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN] != ~0))
618     {
619       vnet_feature_enable_disable ("ip4-output",
620                                    "ip4-source-and-port-range-check-tx",
621                                    sw_if_index, is_add, &config,
622                                    sizeof (config));
623     }
624   return rv;
625 }
626
627 static clib_error_t *
628 set_ip_source_and_port_range_check_fn (vlib_main_t * vm,
629                                        unformat_input_t * input,
630                                        vlib_cli_command_t * cmd)
631 {
632   vnet_main_t *vnm = vnet_get_main ();
633   ip4_main_t *im = &ip4_main;
634   clib_error_t *error = 0;
635   u8 is_add = 1;
636   u32 sw_if_index = ~0;
637   u32 vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
638   u32 fib_index[IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS];
639   int vrf_set = 0;
640   uword *p;
641   int rv = 0;
642   int i;
643
644   sw_if_index = ~0;
645   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
646     {
647       fib_index[i] = ~0;
648       vrf_id[i] = ~0;
649     }
650
651   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
652     {
653       if (unformat (input, "%U", unformat_vnet_sw_interface, vnm,
654                     &sw_if_index))
655         ;
656       else
657         if (unformat
658             (input, "tcp-out-vrf %d",
659              &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_OUT]))
660         vrf_set = 1;
661       else
662         if (unformat
663             (input, "udp-out-vrf %d",
664              &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_OUT]))
665         vrf_set = 1;
666       else
667         if (unformat
668             (input, "tcp-in-vrf %d",
669              &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_TCP_IN]))
670         vrf_set = 1;
671       else
672         if (unformat
673             (input, "udp-in-vrf %d",
674              &vrf_id[IP_SOURCE_AND_PORT_RANGE_CHECK_PROTOCOL_UDP_IN]))
675         vrf_set = 1;
676       else if (unformat (input, "del"))
677         is_add = 0;
678       else
679         break;
680     }
681
682   if (sw_if_index == ~0)
683     return clib_error_return (0, "Interface required but not specified");
684
685   if (!vrf_set)
686     return clib_error_return (0,
687                               "TCP or UDP VRF ID required but not specified");
688
689   for (i = 0; i < IP_SOURCE_AND_PORT_RANGE_CHECK_N_PROTOCOLS; i++)
690     {
691
692       if (vrf_id[i] == 0)
693         return clib_error_return (0,
694                                   "TCP, UDP VRF ID should not be 0 (default). Should be distinct VRF for this purpose. ");
695
696       if (vrf_id[i] != ~0)
697         {
698           p = hash_get (im->fib_index_by_table_id, vrf_id[i]);
699
700           if (p == 0)
701             return clib_error_return (0, "Invalid VRF ID %d", vrf_id[i]);
702
703           fib_index[i] = p[0];
704         }
705     }
706   rv =
707     set_ip_source_and_port_range_check (vm, fib_index, sw_if_index, is_add);
708
709   switch (rv)
710     {
711     case 0:
712       break;
713
714     default:
715       return clib_error_return
716         (0,
717          "set source and port-range on interface returned an unexpected value: %d",
718          rv);
719     }
720   return error;
721 }
722
723 /*?
724  * Add the 'ip4-source-and-port-range-check-rx' or
725  * 'ip4-source-and-port-range-check-tx' graph node for a given
726  * interface. 'tcp-out-vrf' and 'udp-out-vrf' will add to
727  * the RX path. 'tcp-in-vrf' and 'udp-in-vrf' will add to
728  * the TX path. A graph node will be inserted into the chain when
729  * the range check is added to the first interface. It will not
730  * be removed from when range check is removed from the last
731  * interface.
732  *
733  * By adding the range check graph node to the interface, incoming
734  * or outgoing TCP/UDP packets will be validated using the
735  * provided IPv4 FIB table (VRF).
736  *
737  * @note 'ip4-source-and-port-range-check-rx' and
738  * 'ip4-source-and-port-range-check-tx' strings are too long, so
739  * they are truncated on the 'show vlib graph' output.
740  *
741  * @todo This content needs to be validated and potentially more detail added.
742  *
743  * @cliexpar
744  * @parblock
745  * Example of graph node before range checking is enabled:
746  * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
747  *            Name                      Next                    Previous
748  * ip4-source-and-port-range-      ip4-drop [0]
749  * @cliexend
750  *
751  * Example of how to enable range checking on TX:
752  * @cliexcmd{set interface ip source-and-port-range-check GigabitEthernet2/0/0
753  * udp-in-vrf 7}
754  *
755  * Example of graph node after range checking is enabled:
756  * @cliexstart{show vlib graph ip4-source-and-port-range-check-tx}
757  *            Name                      Next                    Previous
758  * ip4-source-and-port-range-      ip4-drop [0]                ip4-rewrite
759  *                              interface-output [1]
760  * @cliexend
761  *
762  * Example of how to display the features enabled on an interface:
763  * @cliexstart{show ip interface features GigabitEthernet2/0/0}
764  * IP feature paths configured on GigabitEthernet2/0/0...
765  *
766  * ipv4 unicast:
767  *   ip4-source-and-port-range-check-rx
768  *   ip4-lookup
769  *
770  * ipv4 multicast:
771  *   ip4-lookup-multicast
772  *
773  * ipv4 multicast:
774  *   interface-output
775  *
776  * ipv6 unicast:
777  *   ip6-lookup
778  *
779  * ipv6 multicast:
780  *   ip6-lookup
781  *
782  * ipv6 multicast:
783  *   interface-output
784  * @cliexend
785  * @endparblock
786 ?*/
787 /* *INDENT-OFF* */
788 VLIB_CLI_COMMAND (set_interface_ip_source_and_port_range_check_command, static) = {
789   .path = "set interface ip source-and-port-range-check",
790   .function = set_ip_source_and_port_range_check_fn,
791   .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]",
792 };
793 /* *INDENT-ON* */
794
795 static u8 *
796 format_ppr_dpo (u8 * s, va_list * args)
797 {
798   index_t index = va_arg (*args, index_t);
799   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
800
801   protocol_port_range_dpo_t *ppr_dpo;
802   int i, j;
803   int printed = 0;
804
805   ppr_dpo = protocol_port_range_dpo_get (index);
806
807   s = format (s, "allow ");
808
809   for (i = 0; i < ppr_dpo->n_used_blocks; i++)
810     {
811       for (j = 0; j < 8; j++)
812         {
813           if (ppr_dpo->blocks[i].low.as_u16[j])
814             {
815               if (printed)
816                 s = format (s, ", ");
817               if (ppr_dpo->blocks[i].hi.as_u16[j] >
818                   (ppr_dpo->blocks[i].low.as_u16[j] + 1))
819                 s =
820                   format (s, "%d-%d", (u32) ppr_dpo->blocks[i].low.as_u16[j],
821                           (u32) ppr_dpo->blocks[i].hi.as_u16[j] - 1);
822               else
823                 s = format (s, "%d", ppr_dpo->blocks[i].low.as_u16[j]);
824               printed = 1;
825             }
826         }
827     }
828   return s;
829 }
830
831 static void
832 ppr_dpo_lock (dpo_id_t * dpo)
833 {
834 }
835
836 static void
837 ppr_dpo_unlock (dpo_id_t * dpo)
838 {
839 }
840
841 const static dpo_vft_t ppr_vft = {
842   .dv_lock = ppr_dpo_lock,
843   .dv_unlock = ppr_dpo_unlock,
844   .dv_format = format_ppr_dpo,
845 };
846
847 const static char *const ppr_ip4_nodes[] = {
848   "ip4-source-and-port-range-check-rx",
849   NULL,
850 };
851
852 const static char *const *const ppr_nodes[DPO_PROTO_NUM] = {
853   [DPO_PROTO_IP4] = ppr_ip4_nodes,
854 };
855
856 clib_error_t *
857 ip4_source_and_port_range_check_init (vlib_main_t * vm)
858 {
859   source_range_check_main_t *srm = &source_range_check_main;
860
861   srm->vlib_main = vm;
862   srm->vnet_main = vnet_get_main ();
863
864   ppr_dpo_type = dpo_register_new_type (&ppr_vft, ppr_nodes);
865
866   return 0;
867 }
868
869 VLIB_INIT_FUNCTION (ip4_source_and_port_range_check_init);
870
871 protocol_port_range_dpo_t *
872 protocol_port_range_dpo_alloc (void)
873 {
874   protocol_port_range_dpo_t *ppr_dpo;
875
876   pool_get_aligned (ppr_dpo_pool, ppr_dpo, CLIB_CACHE_LINE_BYTES);
877   clib_memset (ppr_dpo, 0, sizeof (*ppr_dpo));
878
879   ppr_dpo->n_free_ranges = N_PORT_RANGES_PER_DPO;
880
881   return (ppr_dpo);
882 }
883
884
885 static int
886 add_port_range_adjacency (u32 fib_index,
887                           ip4_address_t * address,
888                           u32 length, u16 * low_ports, u16 * high_ports)
889 {
890   protocol_port_range_dpo_t *ppr_dpo;
891   dpo_id_t dpop = DPO_INVALID;
892   int i, j, k;
893
894   fib_node_index_t fei;
895   fib_prefix_t pfx = {
896     .fp_proto = FIB_PROTOCOL_IP4,
897     .fp_len = length,
898     .fp_addr = {
899                 .ip4 = *address,
900                 },
901   };
902
903   /*
904    * check to see if we have already sourced this prefix
905    */
906   fei = fib_table_lookup_exact_match (fib_index, &pfx);
907
908   if (FIB_NODE_INDEX_INVALID == fei)
909     {
910       /*
911        * this is a first time add for this prefix.
912        */
913       ppr_dpo = protocol_port_range_dpo_alloc ();
914     }
915   else
916     {
917       /*
918        * the prefix is already there.
919        * check it was sourced by us, and if so get the ragne DPO from it.
920        */
921       dpo_id_t dpo = DPO_INVALID;
922       const dpo_id_t *bucket;
923
924       if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
925         {
926           /*
927            * there is existing state. we'll want to add the new ranges to it
928            */
929           bucket =
930             load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
931           ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
932           dpo_reset (&dpo);
933         }
934       else
935         {
936           /*
937            * there is no PPR state associated with this prefix,
938            * so we'll need a new DPO
939            */
940           ppr_dpo = protocol_port_range_dpo_alloc ();
941         }
942     }
943
944   if (vec_len (low_ports) > ppr_dpo->n_free_ranges)
945     return VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY;
946
947   j = k = 0;
948
949   for (i = 0; i < vec_len (low_ports); i++)
950     {
951       for (; j < N_BLOCKS_PER_DPO; j++)
952         {
953           for (; k < 8; k++)
954             {
955               if (ppr_dpo->blocks[j].low.as_u16[k] == 0)
956                 {
957                   ppr_dpo->blocks[j].low.as_u16[k] = low_ports[i];
958                   ppr_dpo->blocks[j].hi.as_u16[k] = high_ports[i];
959                   goto doublebreak;
960                 }
961             }
962         }
963     doublebreak:;
964     }
965   ppr_dpo->n_used_blocks = j + 1;
966
967   /*
968    * add or update the entry in the FIB
969    */
970   dpo_set (&dpop, ppr_dpo_type, DPO_PROTO_IP4, (ppr_dpo - ppr_dpo_pool));
971
972   if (FIB_NODE_INDEX_INVALID == fei)
973     {
974       fib_table_entry_special_dpo_add (fib_index,
975                                        &pfx,
976                                        FIB_SOURCE_SPECIAL,
977                                        FIB_ENTRY_FLAG_NONE, &dpop);
978     }
979   else
980     {
981       fib_entry_special_update (fei,
982                                 FIB_SOURCE_SPECIAL,
983                                 FIB_ENTRY_FLAG_NONE, &dpop);
984     }
985
986   return 0;
987 }
988
989 static int
990 remove_port_range_adjacency (u32 fib_index,
991                              ip4_address_t * address,
992                              u32 length, u16 * low_ports, u16 * high_ports)
993 {
994   protocol_port_range_dpo_t *ppr_dpo;
995   fib_node_index_t fei;
996   int i, j, k;
997
998   fib_prefix_t pfx = {
999     .fp_proto = FIB_PROTOCOL_IP4,
1000     .fp_len = length,
1001     .fp_addr = {
1002                 .ip4 = *address,
1003                 },
1004   };
1005
1006   /*
1007    * check to see if we have sourced this prefix
1008    */
1009   fei = fib_table_lookup_exact_match (fib_index, &pfx);
1010
1011   if (FIB_NODE_INDEX_INVALID == fei)
1012     {
1013       /*
1014        * not one of ours
1015        */
1016       return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1017     }
1018   else
1019     {
1020       /*
1021        * the prefix is already there.
1022        * check it was sourced by us
1023        */
1024       dpo_id_t dpo = DPO_INVALID;
1025       const dpo_id_t *bucket;
1026
1027       if (fib_entry_get_dpo_for_source (fei, FIB_SOURCE_SPECIAL, &dpo))
1028         {
1029           /*
1030            * there is existing state. we'll want to add the new ranges to it
1031            */
1032           bucket =
1033             load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1034           ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1035           dpo_reset (&dpo);
1036         }
1037       else
1038         {
1039           /*
1040            * not one of ours
1041            */
1042           return VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE;
1043         }
1044     }
1045
1046   for (i = 0; i < vec_len (low_ports); i++)
1047     {
1048       for (j = 0; j < N_BLOCKS_PER_DPO; j++)
1049         {
1050           for (k = 0; k < 8; k++)
1051             {
1052               if (low_ports[i] == ppr_dpo->blocks[j].low.as_u16[k] &&
1053                   high_ports[i] == ppr_dpo->blocks[j].hi.as_u16[k])
1054                 {
1055                   ppr_dpo->blocks[j].low.as_u16[k] =
1056                     ppr_dpo->blocks[j].hi.as_u16[k] = 0;
1057                   goto doublebreak;
1058                 }
1059             }
1060         }
1061     doublebreak:;
1062     }
1063
1064   ppr_dpo->n_free_ranges = 0;
1065
1066   /* Have we deleted all ranges yet? */
1067   for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1068     {
1069       for (j = 0; j < 8; j++)
1070         {
1071           if (ppr_dpo->blocks[j].low.as_u16[i] == 0)
1072             ppr_dpo->n_free_ranges++;
1073         }
1074     }
1075
1076   if (N_PORT_RANGES_PER_DPO == ppr_dpo->n_free_ranges)
1077     {
1078       /* Yes, lose the adjacency... */
1079       fib_table_entry_special_remove (fib_index, &pfx, FIB_SOURCE_SPECIAL);
1080     }
1081   else
1082     {
1083       /*
1084        * compact the ranges down to a contiguous block
1085        */
1086       // FIXME. TODO.
1087     }
1088
1089   return 0;
1090 }
1091
1092 // This will be moved to another file and implemented post API freeze.
1093 int
1094 ip6_source_and_port_range_check_add_del (ip6_address_t * address,
1095                                          u32 length,
1096                                          u32 vrf_id,
1097                                          u16 * low_ports,
1098                                          u16 * high_ports, int is_add)
1099 {
1100   u32 fib_index;
1101
1102   fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1103
1104   ASSERT (~0 != fib_index);
1105
1106   fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_CLASSIFY);
1107
1108   return 0;
1109 }
1110
1111 int
1112 ip4_source_and_port_range_check_add_del (ip4_address_t * address,
1113                                          u32 length,
1114                                          u32 vrf_id,
1115                                          u16 * low_ports,
1116                                          u16 * high_ports, int is_add)
1117 {
1118   u32 fib_index;
1119
1120   fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id,
1121                                                  FIB_SOURCE_CLASSIFY);
1122
1123   if (is_add == 0)
1124     {
1125       remove_port_range_adjacency (fib_index, address, length,
1126                                    low_ports, high_ports);
1127     }
1128   else
1129     {
1130       add_port_range_adjacency (fib_index, address, length,
1131                                 low_ports, high_ports);
1132     }
1133
1134   return 0;
1135 }
1136
1137 static clib_error_t *
1138 ip_source_and_port_range_check_command_fn (vlib_main_t * vm,
1139                                            unformat_input_t * input,
1140                                            vlib_cli_command_t * cmd)
1141 {
1142   u16 *low_ports = 0;
1143   u16 *high_ports = 0;
1144   u16 this_low;
1145   u16 this_hi;
1146   ip4_address_t ip4_addr;
1147   ip6_address_t ip6_addr;       //This function will be moved to generic impl when v6 done.
1148   u32 length;
1149   u32 tmp, tmp2;
1150   u32 vrf_id = ~0;
1151   int is_add = 1, ip_ver = ~0;
1152   int rv;
1153
1154
1155   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1156     {
1157       if (unformat (input, "%U/%d", unformat_ip4_address, &ip4_addr, &length))
1158         ip_ver = 4;
1159       else
1160         if (unformat
1161             (input, "%U/%d", unformat_ip6_address, &ip6_addr, &length))
1162         ip_ver = 6;
1163       else if (unformat (input, "vrf %d", &vrf_id))
1164         ;
1165       else if (unformat (input, "del"))
1166         is_add = 0;
1167       else if (unformat (input, "port %d", &tmp))
1168         {
1169           if (tmp == 0 || tmp > 65535)
1170             return clib_error_return (0, "port %d out of range", tmp);
1171           this_low = tmp;
1172           this_hi = this_low + 1;
1173           vec_add1 (low_ports, this_low);
1174           vec_add1 (high_ports, this_hi);
1175         }
1176       else if (unformat (input, "range %d - %d", &tmp, &tmp2))
1177         {
1178           if (tmp > tmp2)
1179             return clib_error_return (0, "ports %d and %d out of order",
1180                                       tmp, tmp2);
1181           if (tmp == 0 || tmp > 65535)
1182             return clib_error_return (0, "low port %d out of range", tmp);
1183           if (tmp2 == 0 || tmp2 > 65535)
1184             return clib_error_return (0, "high port %d out of range", tmp2);
1185           this_low = tmp;
1186           this_hi = tmp2 + 1;
1187           vec_add1 (low_ports, this_low);
1188           vec_add1 (high_ports, this_hi);
1189         }
1190       else
1191         break;
1192     }
1193
1194   if (ip_ver == ~0)
1195     return clib_error_return (0, " <address>/<mask> not specified");
1196
1197   if (vrf_id == ~0)
1198     return clib_error_return (0, " VRF ID required, not specified");
1199
1200   if (vec_len (low_ports) == 0)
1201     return clib_error_return (0,
1202                               " Both VRF ID and range/port must be set for a protocol.");
1203
1204   if (vrf_id == 0)
1205     return clib_error_return (0, " VRF ID can not be 0 (default).");
1206
1207
1208   if (ip_ver == 4)
1209     rv = ip4_source_and_port_range_check_add_del
1210       (&ip4_addr, length, vrf_id, low_ports, high_ports, is_add);
1211   else
1212     return clib_error_return (0, " IPv6 in subsequent patch");
1213
1214   switch (rv)
1215     {
1216     case 0:
1217       break;
1218
1219     case VNET_API_ERROR_INCORRECT_ADJACENCY_TYPE:
1220       return clib_error_return
1221         (0, " Incorrect adjacency for add/del operation");
1222
1223     case VNET_API_ERROR_EXCEEDED_NUMBER_OF_PORTS_CAPACITY:
1224       return clib_error_return (0, " Too many ports in add/del operation");
1225
1226     case VNET_API_ERROR_EXCEEDED_NUMBER_OF_RANGES_CAPACITY:
1227       return clib_error_return
1228         (0, " Too many ranges requested for add operation");
1229
1230     default:
1231       return clib_error_return (0, " returned an unexpected value: %d", rv);
1232     }
1233
1234   return 0;
1235 }
1236
1237 /*?
1238  * This command adds an IP Subnet and range of ports to be validated
1239  * by an IP FIB table (VRF).
1240  *
1241  * @todo This is incomplete. This needs a detailed description and a
1242  * practical example.
1243  *
1244  * @cliexpar
1245  * Example of how to add an IPv4 subnet and single port to an IPv4 FIB table:
1246  * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23}
1247  * Example of how to add an IPv4 subnet and range of ports to an IPv4 FIB table:
1248  * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100}
1249  * Example of how to delete an IPv4 subnet and single port from an IPv4 FIB table:
1250  * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 port 23 del}
1251  * Example of how to delete an IPv4 subnet and range of ports from an IPv4 FIB table:
1252  * @cliexcmd{set ip source-and-port-range-check vrf 7 172.16.1.0/24 range 23 - 100 del}
1253 ?*/
1254 /* *INDENT-OFF* */
1255 VLIB_CLI_COMMAND (ip_source_and_port_range_check_command, static) = {
1256   .path = "set ip source-and-port-range-check",
1257   .function = ip_source_and_port_range_check_command_fn,
1258   .short_help =
1259   "set ip source-and-port-range-check vrf <table-id> <ip-addr>/<mask> {port nn | range <nn> - <nn>} [del]",
1260 };
1261 /* *INDENT-ON* */
1262
1263
1264 static clib_error_t *
1265 show_source_and_port_range_check_fn (vlib_main_t * vm,
1266                                      unformat_input_t * input,
1267                                      vlib_cli_command_t * cmd)
1268 {
1269   protocol_port_range_dpo_t *ppr_dpo;
1270   u32 fib_index;
1271   u8 addr_set = 0;
1272   u32 vrf_id = ~0;
1273   int rv, i, j;
1274   u32 port = 0;
1275   fib_prefix_t pfx = {
1276     .fp_proto = FIB_PROTOCOL_IP4,
1277     .fp_len = 32,
1278   };
1279
1280   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1281     {
1282       if (unformat (input, "%U", unformat_ip4_address, &pfx.fp_addr.ip4))
1283         addr_set = 1;
1284       else if (unformat (input, "vrf %d", &vrf_id))
1285         ;
1286       else if (unformat (input, "port %d", &port))
1287         ;
1288       else
1289         break;
1290     }
1291
1292   if (addr_set == 0)
1293     return clib_error_return (0, "<address> not specified");
1294
1295   if (vrf_id == ~0)
1296     return clib_error_return (0, "VRF ID required, not specified");
1297
1298   fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id);
1299   if (~0 == fib_index)
1300     return clib_error_return (0, "VRF %d not found", vrf_id);
1301
1302   /*
1303    * find the longest prefix match on the address requested,
1304    * check it was sourced by us
1305    */
1306   dpo_id_t dpo = DPO_INVALID;
1307   const dpo_id_t *bucket;
1308
1309   if (!fib_entry_get_dpo_for_source (fib_table_lookup (fib_index, &pfx),
1310                                      FIB_SOURCE_SPECIAL, &dpo))
1311     {
1312       /*
1313        * not one of ours
1314        */
1315       vlib_cli_output (vm, "%U: src address drop", format_ip4_address,
1316                        &pfx.fp_addr.ip4);
1317       return 0;
1318     }
1319
1320   bucket = load_balance_get_bucket_i (load_balance_get (dpo.dpoi_index), 0);
1321   ppr_dpo = protocol_port_range_dpo_get (bucket->dpoi_index);
1322   dpo_reset (&dpo);
1323
1324   if (port)
1325     {
1326       rv = check_adj_port_range_x1 (ppr_dpo, (u16) port, 1234);
1327       if (rv == 1234)
1328         vlib_cli_output (vm, "%U port %d PASS", format_ip4_address,
1329                          &pfx.fp_addr.ip4, port);
1330       else
1331         vlib_cli_output (vm, "%U port %d FAIL", format_ip4_address,
1332                          &pfx.fp_addr.ip4, port);
1333       return 0;
1334     }
1335   else
1336     {
1337       u8 *s;
1338
1339       s = format (0, "%U: ", format_ip4_address, &pfx.fp_addr.ip4);
1340
1341       for (i = 0; i < N_BLOCKS_PER_DPO; i++)
1342         {
1343           for (j = 0; j < 8; j++)
1344             {
1345               if (ppr_dpo->blocks[i].low.as_u16[j])
1346                 s = format (s, "%d - %d ",
1347                             (u32) ppr_dpo->blocks[i].low.as_u16[j],
1348                             (u32) ppr_dpo->blocks[i].hi.as_u16[j]);
1349             }
1350         }
1351       vlib_cli_output (vm, "%s", s);
1352       vec_free (s);
1353     }
1354
1355   return 0;
1356 }
1357
1358 /*?
1359  * Display the range of ports being validated by an IPv4 FIB for a given
1360  * IP or subnet, or test if a given IP and port are being validated.
1361  *
1362  * @todo This is incomplete. This needs a detailed description and a
1363  * practical example.
1364  *
1365  * @cliexpar
1366  * Example of how to display the set of ports being validated for a given
1367  * IPv4 subnet:
1368  * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.0}
1369  * 172.16.2.0: 23 - 101
1370  * @cliexend
1371  * Example of how to test to determine of a given iPv4 address and port
1372  * are being validated:
1373  * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 23}
1374  * 172.16.2.2 port 23 PASS
1375  * @cliexend
1376  * @cliexstart{show ip source-and-port-range-check vrf 7 172.16.2.2 port 250}
1377  * 172.16.2.2 port 250 FAIL
1378  * @cliexend
1379  ?*/
1380 /* *INDENT-OFF* */
1381 VLIB_CLI_COMMAND (show_source_and_port_range_check, static) = {
1382   .path = "show ip source-and-port-range-check",
1383   .function = show_source_and_port_range_check_fn,
1384   .short_help =
1385   "show ip source-and-port-range-check vrf <table-id> <ip-addr> [port <n>]",
1386 };
1387 /* *INDENT-ON* */
1388
1389 /*
1390  * fd.io coding-style-patch-verification: ON
1391  *
1392  * Local Variables:
1393  * eval: (c-set-style "gnu")
1394  * End:
1395  */