pppoe: fix uninitialized memory bug
[vpp.git] / src / plugins / pppoe / pppoe.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _PPPOE_H
19 #define _PPPOE_H
20
21 #include <vnet/plugin/plugin.h>
22 #include <vppinfra/lock.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/hash.h>
25 #include <vnet/vnet.h>
26 #include <vnet/ip/ip.h>
27 #include <vnet/ethernet/ethernet.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/dpo/dpo.h>
31 #include <vnet/adj/adj_types.h>
32 #include <vnet/fib/fib_table.h>
33 #include <vlib/vlib.h>
34 #include <vppinfra/bihash_8_8.h>
35
36
37 typedef struct
38 {
39   u8 ver_type;
40   u8 code;
41   u16 session_id;
42   u16 length;
43   u16 ppp_proto;
44 } pppoe_header_t;
45
46 #define PPPOE_VER_TYPE 0x11
47 #define PPPOE_PADS 0x65
48
49 typedef struct
50 {
51   /* Required for pool_get_aligned  */
52   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
53
54   /* pppoe session_id in HOST byte order */
55   u16 session_id;
56
57   /* session client addresses */
58   ip46_address_t client_ip;
59
60   /* the index of tx interface for pppoe encaped packet */
61   u32 encap_if_index;
62
63   /** FIB indices - inner IP packet lookup here */
64   u32 decap_fib_index;
65
66   u8 local_mac[6];
67   u8 client_mac[6];
68
69   /* vnet intfc index */
70   u32 sw_if_index;
71   u32 hw_if_index;
72
73 } pppoe_session_t;
74
75 #define foreach_pppoe_input_next        \
76 _(DROP, "error-drop")                  \
77 _(IP4_INPUT, "ip4-input")              \
78 _(IP6_INPUT, "ip6-input" )             \
79 _(CP_INPUT, "pppoe-cp-dispatch" )      \
80
81 typedef enum
82 {
83 #define _(s,n) PPPOE_INPUT_NEXT_##s,
84   foreach_pppoe_input_next
85 #undef _
86     PPPOE_INPUT_N_NEXT,
87 } pppoe_input_next_t;
88
89 typedef enum
90 {
91 #define pppoe_error(n,s) PPPOE_ERROR_##n,
92 #include <pppoe/pppoe_error.def>
93 #undef pppoe_error
94   PPPOE_N_ERROR,
95 } pppoe_input_error_t;
96
97 extern char *pppoe_error_strings[];
98
99 #define MTU 1500
100 #define MTU_BUFFERS ((MTU + vlib_buffer_get_default_data_size(vm) - 1) / vlib_buffer_get_default_data_size(vm))
101 #define NUM_BUFFERS_TO_ALLOC 32
102
103 /*
104  * The size of pppoe session table
105  */
106 #define PPPOE_NUM_BUCKETS (64 * 1024)
107 #define PPPOE_MEMORY_SIZE (8<<20)
108
109 /* *INDENT-OFF* */
110 /*
111  * The PPPoE key is the mac address and session ID
112  */
113 typedef struct
114 {
115   union
116   {
117     struct
118     {
119       u16 session_id;
120       u8 mac[6];
121     } fields;
122     struct
123     {
124       u32 w0;
125       u32 w1;
126     } words;
127     u64 raw;
128   };
129 } pppoe_entry_key_t;
130 /* *INDENT-ON* */
131
132 /* *INDENT-OFF* */
133 /*
134  * The PPPoE entry results
135  */
136 typedef struct
137 {
138   union
139   {
140     struct
141     {
142       u32 sw_if_index;
143
144       u32 session_index;
145
146     } fields;
147     u64 raw;
148   };
149 }  pppoe_entry_result_t;
150 /* *INDENT-ON* */
151
152 typedef struct
153 {
154   /* Vector of encap session instances, */
155   pppoe_session_t *sessions;
156
157   /* For CP:  vector of CP path */
158     BVT (clib_bihash) link_table;
159
160   /* For DP:  vector of DP path */
161     BVT (clib_bihash) session_table;
162
163   /* Free vlib hw_if_indices */
164   u32 *free_pppoe_session_hw_if_indices;
165
166   /* Mapping from sw_if_index to session index */
167   u32 *session_index_by_sw_if_index;
168
169   /* used for pppoe cp path */
170   u32 cp_if_index;
171
172   /* API message ID base */
173   u16 msg_id_base;
174
175   /* convenience */
176   vlib_main_t *vlib_main;
177   vnet_main_t *vnet_main;
178
179 } pppoe_main_t;
180
181 extern pppoe_main_t pppoe_main;
182
183 extern vlib_node_registration_t pppoe_input_node;
184 extern vlib_node_registration_t pppoe_cp_dispatch_node;
185
186 typedef struct
187 {
188   u8 is_add;
189   u8 is_ip6;
190   u16 session_id;
191   ip46_address_t client_ip;
192   u32 encap_if_index;
193   u32 decap_fib_index;
194   u8 local_mac[6];
195   u8 client_mac[6];
196 } vnet_pppoe_add_del_session_args_t;
197
198 int vnet_pppoe_add_del_session
199   (vnet_pppoe_add_del_session_args_t * a, u32 * sw_if_indexp);
200
201 typedef struct
202 {
203   u8 is_add;
204   u32 client_if_index;
205   u32 cp_if_index;
206 } vnet_pppoe_add_del_tap_args_t;
207
208 always_inline u64
209 pppoe_make_key (u8 * mac_address, u16 session_id)
210 {
211   u64 temp;
212
213   /*
214    * The mac address in memory is A:B:C:D:E:F
215    * The session_id in register is H:L
216    */
217 #if CLIB_ARCH_IS_LITTLE_ENDIAN
218   /*
219    * Create the in-register key as F:E:D:C:B:A:H:L
220    * In memory the key is L:H:A:B:C:D:E:F
221    */
222   temp = *((u64 *) (mac_address)) << 16;
223   temp = (temp & ~0xffff) | (u64) (session_id);
224 #else
225   /*
226    * Create the in-register key as H:L:A:B:C:D:E:F
227    * In memory the key is H:L:A:B:C:D:E:F
228    */
229   temp = *((u64 *) (mac_address)) >> 16;
230   temp = temp | (((u64) session_id) << 48);
231 #endif
232
233   return temp;
234 }
235
236 /**
237  * Perform learning on one packet based on the mac table lookup result.
238  * */
239 static_always_inline void
240 pppoe_learn_process (BVT (clib_bihash) * table,
241                      u32 sw_if_index0,
242                      pppoe_entry_key_t * key0,
243                      pppoe_entry_key_t * cached_key,
244                      u32 * bucket0, pppoe_entry_result_t * result0)
245 {
246   /* Check mac table lookup result */
247   if (PREDICT_TRUE (result0->fields.sw_if_index == sw_if_index0))
248     {
249       /*
250        * The entry was in the table, and the sw_if_index matched, the normal case
251        */
252       return;
253     }
254   else if (result0->fields.sw_if_index == ~0)
255     {
256       /* The entry was not in table, so add it  */
257       result0->fields.sw_if_index = sw_if_index0;
258       result0->fields.session_index = ~0;
259       cached_key->raw = ~0;     /* invalidate the cache */
260     }
261   else
262     {
263       /* The entry was in the table, but with the wrong sw_if_index mapping (mac move) */
264       result0->fields.sw_if_index = sw_if_index0;
265     }
266
267   /* Update the entry */
268   BVT (clib_bihash_kv) kv;
269   kv.key = key0->raw;
270   kv.value = result0->raw;
271   BV (clib_bihash_add_del) (table, &kv, 1 /* is_add */ );
272 }
273
274 static_always_inline void
275 pppoe_lookup_1 (BVT (clib_bihash) * table,
276                 pppoe_entry_key_t * cached_key,
277                 pppoe_entry_result_t * cached_result,
278                 u8 * mac0,
279                 u16 session_id0,
280                 pppoe_entry_key_t * key0,
281                 u32 * bucket0, pppoe_entry_result_t * result0)
282 {
283   /* set up key */
284   key0->raw = pppoe_make_key (mac0, session_id0);
285   *bucket0 = ~0;
286
287   if (key0->raw == cached_key->raw)
288     {
289       /* Hit in the one-entry cache */
290       result0->raw = cached_result->raw;
291     }
292   else
293     {
294       /* Do a regular session table lookup */
295       BVT (clib_bihash_kv) kv;
296
297       kv.key = key0->raw;
298       kv.value = ~0ULL;
299       BV (clib_bihash_search_inline) (table, &kv);
300       result0->raw = kv.value;
301
302       /* Update one-entry cache */
303       cached_key->raw = key0->raw;
304       cached_result->raw = result0->raw;
305     }
306 }
307
308 static_always_inline void
309 pppoe_update_1 (BVT (clib_bihash) * table,
310                 u8 * mac0,
311                 u16 session_id0,
312                 pppoe_entry_key_t * key0,
313                 u32 * bucket0, pppoe_entry_result_t * result0)
314 {
315   /* set up key */
316   key0->raw = pppoe_make_key (mac0, session_id0);
317   *bucket0 = ~0;
318
319   /* Update the entry */
320   BVT (clib_bihash_kv) kv;
321   kv.key = key0->raw;
322   kv.value = result0->raw;
323   BV (clib_bihash_add_del) (table, &kv, 1 /* is_add */ );
324
325 }
326 #endif /* _PPPOE_H */
327
328 /*
329  * fd.io coding-style-patch-verification: ON
330  *
331  * Local Variables:
332  * eval: (c-set-style "gnu")
333  * End:
334  */