pppoe: fix uninitialized memory bug
[vpp.git] / src / plugins / pppoe / pppoe_decap.c
1 /*
2  * decap.c: pppoe session decap packet processing
3  *
4  * Copyright (c) 2017 Intel and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/ppp/packet.h>
21 #include <pppoe/pppoe.h>
22
23 typedef struct {
24   u32 next_index;
25   u32 session_index;
26   u32 session_id;
27   u32 error;
28 } pppoe_rx_trace_t;
29
30 static u8 * format_pppoe_rx_trace (u8 * s, va_list * args)
31 {
32   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34   pppoe_rx_trace_t * t = va_arg (*args, pppoe_rx_trace_t *);
35
36   if (t->session_index != ~0)
37     {
38       s = format (s, "PPPoE decap from pppoe_session%d session_id %d next %d error %d",
39                   t->session_index, t->session_id, t->next_index, t->error);
40     }
41   else
42     {
43       s = format (s, "PPPoE decap error - session for session_id %d does not exist",
44                   t->session_id);
45     }
46   return s;
47 }
48
49 VLIB_NODE_FN (pppoe_input_node) (vlib_main_t * vm,
50              vlib_node_runtime_t * node,
51              vlib_frame_t * from_frame)
52 {
53   u32 n_left_from, next_index, * from, * to_next;
54   pppoe_main_t * pem = &pppoe_main;
55   vnet_main_t * vnm = pem->vnet_main;
56   vnet_interface_main_t * im = &vnm->interface_main;
57   u32 pkts_decapsulated = 0;
58   u32 thread_index = vlib_get_thread_index();
59   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
60   pppoe_entry_key_t cached_key;
61   pppoe_entry_result_t cached_result;
62
63   from = vlib_frame_vector_args (from_frame);
64   n_left_from = from_frame->n_vectors;
65
66   /* Clear the one-entry cache in case session table was updated */
67   cached_key.raw = ~0;
68   cached_result.raw = ~0;       /* warning be gone */
69
70   next_index = node->cached_next_index;
71   stats_sw_if_index = node->runtime_data[0];
72   stats_n_packets = stats_n_bytes = 0;
73
74   while (n_left_from > 0)
75     {
76       u32 n_left_to_next;
77
78       vlib_get_next_frame (vm, node, next_index,
79                            to_next, n_left_to_next);
80       while (n_left_from >= 4 && n_left_to_next >= 2)
81         {
82           u32 bi0, bi1;
83           vlib_buffer_t * b0, * b1;
84           u32 next0, next1;
85           ethernet_header_t *h0, *h1;
86           pppoe_header_t * pppoe0, * pppoe1;
87           u16 ppp_proto0 = 0, ppp_proto1 = 0;
88           pppoe_session_t * t0, * t1;
89           u32 error0, error1;
90           u32 sw_if_index0, sw_if_index1, len0, len1;
91           pppoe_entry_key_t key0, key1;
92           pppoe_entry_result_t result0, result1;
93           u32 bucket0, bucket1;
94
95           /* Prefetch next iteration. */
96           {
97             vlib_buffer_t * p2, * p3;
98
99             p2 = vlib_get_buffer (vm, from[2]);
100             p3 = vlib_get_buffer (vm, from[3]);
101
102             vlib_prefetch_buffer_header (p2, LOAD);
103             vlib_prefetch_buffer_header (p3, LOAD);
104
105             CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
106             CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
107           }
108
109           bi0 = from[0];
110           bi1 = from[1];
111           to_next[0] = bi0;
112           to_next[1] = bi1;
113           from += 2;
114           to_next += 2;
115           n_left_to_next -= 2;
116           n_left_from -= 2;
117
118           b0 = vlib_get_buffer (vm, bi0);
119           b1 = vlib_get_buffer (vm, bi1);
120           error0 = 0;
121           error1 = 0;
122
123           /* leaves current_data pointing at the pppoe header */
124           pppoe0 = vlib_buffer_get_current (b0);
125           pppoe1 = vlib_buffer_get_current (b1);
126           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
127           ppp_proto1 = clib_net_to_host_u16(pppoe1->ppp_proto);
128
129           /* Manipulate packet 0 */
130           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
131              && (ppp_proto0 != PPP_PROTOCOL_ip6))
132             {
133               error0 = PPPOE_ERROR_CONTROL_PLANE;
134               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
135               goto trace0;
136             }
137
138           /* get client mac */
139           vlib_buffer_reset(b0);
140           h0 = vlib_buffer_get_current (b0);
141
142           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
143                           h0->src_address, pppoe0->session_id,
144                           &key0, &bucket0, &result0);
145           if (PREDICT_FALSE (result0.fields.session_index == ~0))
146             {
147               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
148               next0 = PPPOE_INPUT_NEXT_DROP;
149               goto trace0;
150             }
151
152           t0 = pool_elt_at_index (pem->sessions,
153                                   result0.fields.session_index);
154
155           /* Pop Eth and PPPoE header */
156           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
157
158           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
159                   PPPOE_INPUT_NEXT_IP4_INPUT
160                   : PPPOE_INPUT_NEXT_IP6_INPUT;
161
162           sw_if_index0 = t0->sw_if_index;
163           len0 = vlib_buffer_length_in_chain (vm, b0);
164
165           pkts_decapsulated ++;
166           stats_n_packets += 1;
167           stats_n_bytes += len0;
168
169           /* Batch stats increment on the same pppoe session so counter
170              is not incremented per packet */
171           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
172             {
173               stats_n_packets -= 1;
174               stats_n_bytes -= len0;
175               if (stats_n_packets)
176                 vlib_increment_combined_counter
177                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
178                    thread_index, stats_sw_if_index,
179                    stats_n_packets, stats_n_bytes);
180               stats_n_packets = 1;
181               stats_n_bytes = len0;
182               stats_sw_if_index = sw_if_index0;
183             }
184
185         trace0:
186           b0->error = error0 ? node->errors[error0] : 0;
187
188           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
189             {
190               pppoe_rx_trace_t *tr
191                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
192               tr->next_index = next0;
193               tr->error = error0;
194               tr->session_index = result0.fields.session_index;
195               tr->session_id = clib_net_to_host_u32(pppoe0->session_id);
196             }
197
198
199           /* Manipulate packet 1 */
200           if ((ppp_proto1 != PPP_PROTOCOL_ip4)
201              && (ppp_proto1 != PPP_PROTOCOL_ip6))
202             {
203               error1 = PPPOE_ERROR_CONTROL_PLANE;
204               next1 = PPPOE_INPUT_NEXT_CP_INPUT;
205               goto trace1;
206             }
207
208           /* get client mac */
209           vlib_buffer_reset(b1);
210           h1 = vlib_buffer_get_current (b1);
211
212           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
213                           h1->src_address, pppoe1->session_id,
214                           &key1, &bucket1, &result1);
215           if (PREDICT_FALSE (result1.fields.session_index == ~0))
216             {
217               error1 = PPPOE_ERROR_NO_SUCH_SESSION;
218               next1 = PPPOE_INPUT_NEXT_DROP;
219               goto trace1;
220             }
221
222           t1 = pool_elt_at_index (pem->sessions,
223                                   result1.fields.session_index);
224
225           /* Pop Eth and PPPoE header */
226           vlib_buffer_advance(b1, sizeof(*h1)+sizeof(*pppoe1));
227
228           next1 = (ppp_proto1==PPP_PROTOCOL_ip4)?
229                   PPPOE_INPUT_NEXT_IP4_INPUT
230                   : PPPOE_INPUT_NEXT_IP6_INPUT;
231
232           sw_if_index1 = t1->sw_if_index;
233           len1 = vlib_buffer_length_in_chain (vm, b1);
234
235           pkts_decapsulated ++;
236           stats_n_packets += 1;
237           stats_n_bytes += len1;
238
239           /* Batch stats increment on the same pppoe session so counter
240              is not incremented per packet */
241           if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index))
242             {
243               stats_n_packets -= 1;
244               stats_n_bytes -= len1;
245               if (stats_n_packets)
246                 vlib_increment_combined_counter
247                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
248                    thread_index, stats_sw_if_index,
249                    stats_n_packets, stats_n_bytes);
250               stats_n_packets = 1;
251               stats_n_bytes = len1;
252               stats_sw_if_index = sw_if_index1;
253             }
254
255         trace1:
256           b1->error = error1 ? node->errors[error1] : 0;
257
258           if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
259             {
260               pppoe_rx_trace_t *tr
261                 = vlib_add_trace (vm, node, b1, sizeof (*tr));
262               tr->next_index = next1;
263               tr->error = error1;
264               tr->session_index = result1.fields.session_index;
265               tr->session_id = clib_net_to_host_u32(pppoe1->session_id);
266             }
267
268           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
269                                            to_next, n_left_to_next,
270                                            bi0, bi1, next0, next1);
271         }
272
273       while (n_left_from > 0 && n_left_to_next > 0)
274         {
275           u32 bi0;
276           vlib_buffer_t * b0;
277           u32 next0;
278           ethernet_header_t *h0;
279           pppoe_header_t * pppoe0;
280           u16 ppp_proto0 = 0;
281           pppoe_session_t * t0;
282           u32 error0;
283           u32 sw_if_index0, len0;
284           pppoe_entry_key_t key0;
285           pppoe_entry_result_t result0;
286           u32 bucket0;
287
288           bi0 = from[0];
289           to_next[0] = bi0;
290           from += 1;
291           to_next += 1;
292           n_left_from -= 1;
293           n_left_to_next -= 1;
294
295           b0 = vlib_get_buffer (vm, bi0);
296           error0 = 0;
297
298           /* leaves current_data pointing at the pppoe header */
299           pppoe0 = vlib_buffer_get_current (b0);
300           ppp_proto0 = clib_net_to_host_u16(pppoe0->ppp_proto);
301
302           if ((ppp_proto0 != PPP_PROTOCOL_ip4)
303              && (ppp_proto0 != PPP_PROTOCOL_ip6))
304             {
305               error0 = PPPOE_ERROR_CONTROL_PLANE;
306               next0 = PPPOE_INPUT_NEXT_CP_INPUT;
307               goto trace00;
308             }
309
310           /* get client mac */
311           vlib_buffer_reset(b0);
312           h0 = vlib_buffer_get_current (b0);
313
314           pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
315                           h0->src_address, pppoe0->session_id,
316                           &key0, &bucket0, &result0);
317           if (PREDICT_FALSE (result0.fields.session_index == ~0))
318             {
319               error0 = PPPOE_ERROR_NO_SUCH_SESSION;
320               next0 = PPPOE_INPUT_NEXT_DROP;
321               goto trace00;
322             }
323
324           t0 = pool_elt_at_index (pem->sessions,
325                                   result0.fields.session_index);
326
327           /* Pop Eth and PPPoE header */
328           vlib_buffer_advance(b0, sizeof(*h0)+sizeof(*pppoe0));
329
330           next0 = (ppp_proto0==PPP_PROTOCOL_ip4)?
331                   PPPOE_INPUT_NEXT_IP4_INPUT
332                   : PPPOE_INPUT_NEXT_IP6_INPUT;
333
334           sw_if_index0 = t0->sw_if_index;
335           len0 = vlib_buffer_length_in_chain (vm, b0);
336
337           pkts_decapsulated ++;
338           stats_n_packets += 1;
339           stats_n_bytes += len0;
340
341           /* Batch stats increment on the same pppoe session so counter
342              is not incremented per packet */
343           if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index))
344             {
345               stats_n_packets -= 1;
346               stats_n_bytes -= len0;
347               if (stats_n_packets)
348                 vlib_increment_combined_counter
349                   (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
350                    thread_index, stats_sw_if_index,
351                    stats_n_packets, stats_n_bytes);
352               stats_n_packets = 1;
353               stats_n_bytes = len0;
354               stats_sw_if_index = sw_if_index0;
355             }
356
357         trace00:
358           b0->error = error0 ? node->errors[error0] : 0;
359
360           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
361             {
362               pppoe_rx_trace_t *tr
363                 = vlib_add_trace (vm, node, b0, sizeof (*tr));
364               tr->next_index = next0;
365               tr->error = error0;
366               tr->session_index = result0.fields.session_index;
367               tr->session_id = clib_net_to_host_u16(pppoe0->session_id);
368             }
369           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
370                                            to_next, n_left_to_next,
371                                            bi0, next0);
372         }
373
374       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
375     }
376   /* Do we still need this now that session tx stats is kept? */
377   vlib_node_increment_counter (vm, pppoe_input_node.index,
378                                PPPOE_ERROR_DECAPSULATED,
379                                pkts_decapsulated);
380
381   /* Increment any remaining batch stats */
382   if (stats_n_packets)
383     {
384       vlib_increment_combined_counter
385         (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX,
386          thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
387       node->runtime_data[0] = stats_sw_if_index;
388     }
389
390   return from_frame->n_vectors;
391 }
392
393 #ifndef CLIB_MARCH_VARIANT
394 char * pppoe_error_strings[] = {
395 #define pppoe_error(n,s) s,
396 #include <pppoe/pppoe_error.def>
397 #undef pppoe_error
398 #undef _
399 };
400 #endif /* CLIB_MARCH_VARIANT */
401
402 VLIB_REGISTER_NODE (pppoe_input_node) = {
403   .name = "pppoe-input",
404   /* Takes a vector of packets. */
405   .vector_size = sizeof (u32),
406
407   .n_errors = PPPOE_N_ERROR,
408   .error_strings = pppoe_error_strings,
409
410   .n_next_nodes = PPPOE_INPUT_N_NEXT,
411   .next_nodes = {
412 #define _(s,n) [PPPOE_INPUT_NEXT_##s] = n,
413     foreach_pppoe_input_next
414 #undef _
415   },
416
417   .format_trace = format_pppoe_rx_trace,
418 };
419
420