2 * Copyright (c) 2015 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.
16 #include <vlib/vlib.h>
17 #include <vnet/vnet.h>
18 #include <vppinfra/error.h>
20 #include <vnet/lawful-intercept/lawful_intercept.h>
22 #include <vppinfra/error.h>
23 #include <vppinfra/elog.h>
25 vlib_node_registration_t li_hit_node;
31 /* packet trace format function */
32 static u8 * format_li_hit_trace (u8 * s, va_list * args)
34 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36 li_hit_trace_t * t = va_arg (*args, li_hit_trace_t *);
38 s = format (s, "LI_HIT: next index %d", t->next_index);
43 vlib_node_registration_t li_hit_node;
45 #define foreach_li_hit_error \
46 _(HITS, "LI packets processed") \
47 _(NO_COLLECTOR, "No collector configured") \
48 _(BUFFER_ALLOCATION_FAILURE, "Buffer allocation failure")
51 #define _(sym,str) LI_HIT_ERROR_##sym,
57 static char * li_hit_error_strings[] = {
58 #define _(sym,string) string,
69 li_hit_node_fn (vlib_main_t * vm,
70 vlib_node_runtime_t * node,
73 u32 n_left_from, * from, * to_next;
74 li_hit_next_t next_index;
75 vlib_frame_t * int_frame = 0;
76 u32 * to_int_next = 0;
77 li_main_t * lm = &li_main;
79 from = vlib_frame_vector_args (frame);
80 n_left_from = frame->n_vectors;
81 next_index = node->cached_next_index;
83 if (PREDICT_FALSE (vec_len (lm->collectors) == 0))
85 vlib_node_increment_counter (vm, li_hit_node.index,
86 LI_HIT_ERROR_NO_COLLECTOR,
91 /* The intercept frame... */
92 int_frame = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
93 to_int_next = vlib_frame_vector_args (int_frame);
96 while (n_left_from > 0)
100 vlib_get_next_frame (vm, node, next_index,
101 to_next, n_left_to_next);
104 while (n_left_from >= 4 && n_left_to_next >= 2)
106 u32 next0 = LI_HIT_NEXT_INTERFACE_OUTPUT;
107 u32 next1 = LI_HIT_NEXT_INTERFACE_OUTPUT;
108 u32 sw_if_index0, sw_if_index1;
110 ethernet_header_t *en0, *en1;
112 vlib_buffer_t * b0, * b1;
114 /* Prefetch next iteration. */
116 vlib_buffer_t * p2, * p3;
118 p2 = vlib_get_buffer (vm, from[2]);
119 p3 = vlib_get_buffer (vm, from[3]);
121 vlib_prefetch_buffer_header (p2, LOAD);
122 vlib_prefetch_buffer_header (p3, LOAD);
124 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
125 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
128 /* speculatively enqueue b0 and b1 to the current next frame */
129 to_next[0] = bi0 = from[0];
130 to_next[1] = bi1 = from[1];
136 b0 = vlib_get_buffer (vm, bi0);
137 b1 = vlib_get_buffer (vm, bi1);
139 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
140 ASSERT (b0->current_data == 0);
141 ASSERT (b1->current_data == 0);
143 en0 = vlib_buffer_get_current (b0);
144 en1 = vlib_buffer_get_current (b1);
146 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
147 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
149 /* Send pkt back out the RX interface */
150 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
151 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
153 /* $$$$$ End of processing 2 x packets $$$$$ */
155 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
157 if (b0->flags & VLIB_BUFFER_IS_TRACED)
160 vlib_add_trace (vm, node, b0, sizeof (*t));
161 t->sw_if_index = sw_if_index0;
162 t->next_index = next0;
164 if (b1->flags & VLIB_BUFFER_IS_TRACED)
167 vlib_add_trace (vm, node, b1, sizeof (*t));
168 t->sw_if_index = sw_if_index1;
169 t->next_index = next1;
173 /* verify speculative enqueues, maybe switch current next frame */
174 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
175 to_next, n_left_to_next,
176 bi0, bi1, next0, next1);
178 #endif /* $$$ dual-loop off */
180 while (n_left_from > 0 && n_left_to_next > 0)
185 ip4_udp_header_t * iu0;
188 u32 next0 = LI_HIT_NEXT_ETHERNET;
190 /* speculatively enqueue b0 to the current next frame */
198 b0 = vlib_get_buffer (vm, bi0);
199 if (PREDICT_TRUE(to_int_next != 0))
201 /* Make an intercept copy. This can fail. */
202 c0 = vlib_buffer_copy (vm, b0);
204 if (PREDICT_FALSE (c0 == 0))
206 vlib_node_increment_counter
207 (vm, node->node_index,
208 LI_HIT_ERROR_BUFFER_ALLOCATION_FAILURE, 1);
212 vlib_buffer_advance(c0, -sizeof(*iu0));
214 iu0 = vlib_buffer_get_current(c0);
217 ip0->ip_version_and_header_length = 0x45;
219 ip0->protocol = IP_PROTOCOL_UDP;
221 ip0->src_address.as_u32 = lm->src_addrs[0].as_u32;
222 ip0->dst_address.as_u32 = lm->collectors[0].as_u32;
223 ip0->length = vlib_buffer_length_in_chain (vm, c0);
224 ip0->checksum = ip4_header_checksum (ip0);
227 udp0->src_port = udp0->dst_port =
228 clib_host_to_net_u16(lm->ports[0]);
231 clib_net_to_host_u16 (vlib_buffer_length_in_chain (vm , b0));
233 to_int_next [0] = vlib_get_buffer_index (vm, c0);
238 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
239 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
242 vlib_add_trace (vm, node, b0, sizeof (*t));
243 t->next_index = next0;
246 /* verify speculative enqueue, maybe switch current next frame */
247 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
248 to_next, n_left_to_next,
252 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
257 int_frame->n_vectors = frame->n_vectors;
258 vlib_put_frame_to_node (vm, ip4_lookup_node.index, int_frame);
261 vlib_node_increment_counter (vm, li_hit_node.index,
262 LI_HIT_ERROR_HITS, frame->n_vectors);
263 return frame->n_vectors;
266 VLIB_REGISTER_NODE (li_hit_node) = {
267 .function = li_hit_node_fn,
269 .vector_size = sizeof (u32),
270 .format_trace = format_li_hit_trace,
271 .type = VLIB_NODE_TYPE_INTERNAL,
273 .n_errors = ARRAY_LEN(li_hit_error_strings),
274 .error_strings = li_hit_error_strings,
276 .n_next_nodes = LI_HIT_N_NEXT,
278 /* edit / add dispositions here */
280 [LI_HIT_NEXT_ETHERNET] = "ethernet-input-not-l2",
284 VLIB_NODE_FUNCTION_MULTIARCH (li_hit_node, li_hit_node_fn)