1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   PPP Over Ethernet (PPPoE)                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
27 /*                                                                        */
28 /*    nx_pppoe_client.h                                   PORTABLE C      */
29 /*                                                           6.1.9        */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Yuxin Zhou, Microsoft Corporation                                   */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file defines the NetX PPP Over Ethernet (PPPoE) Client         */
37 /*    componet, including all data types and external references. It is   */
38 /*    assumed that nx_api.h and nx_port.h have already been included.     */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
45 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
46 /*                                            resulting in version 6.1    */
47 /*  12-31-2020     Yuxin Zhou               Modified comment(s), improved */
48 /*                                            string length verification, */
49 /*                                            resulting in version 6.1.3  */
50 /*  10-15-2021     Yuxin Zhou               Modified comment(s), included */
51 /*                                            necessary header file,      */
52 /*                                            resulting in version 6.1.9  */
53 /*                                                                        */
54 /**************************************************************************/
55 
56 #ifndef NX_PPPOE_CLIENT_H
57 #define NX_PPPOE_CLIENT_H
58 
59 /* Determine if a C++ compiler is being used.  If so, ensure that standard
60    C is used to process the API information.  */
61 
62 #ifdef   __cplusplus
63 
64 /* Yes, C++ compiler is present.  Use standard C.  */
65 extern   "C" {
66 
67 #endif
68 
69 #include "nx_api.h"
70 
71 /* Note: Prerequisite for using PPPoE.
72          Redefine NX_PHYSICAL_HEADER to 24 to ensure enough space for filling in physical header.
73          Physical header:14(Ethernet header) + 6(PPPoE header) + 2(PPP header) + 2(four-byte aligment).  */
74 
75 /* Define the Driver command for PPPoE packet.  */
76 #ifndef NX_LINK_PPPOE_DISCOVERY_SEND
77 #define NX_LINK_PPPOE_DISCOVERY_SEND 51
78 #endif
79 #ifndef NX_LINK_PPPOE_SESSION_SEND
80 #define NX_LINK_PPPOE_SESSION_SEND   52
81 #endif
82 
83 /* Define the PPPoE Client ID.  */
84 #define NX_PPPOE_CLIENT_ID                                  0x504F4543UL
85 
86 /* If the driver is not initialized in other module, enable the feature to initialize the driver in PPPoE module .  */
87 /*
88 #define NX_PPPOE_CLIENT_INITIALIZE_DRIVER_ENABLE
89 */
90 
91 /* Define the PPPoE Thread time slice.  */
92 #ifndef NX_PPPOE_CLIENT_THREAD_TIME_SLICE
93 #define NX_PPPOE_CLIENT_THREAD_TIME_SLICE                   TX_NO_TIME_SLICE
94 #endif
95 
96 /* Define the init timeout for PADI.  */
97 #ifndef NX_PPPOE_CLIENT_PADI_INIT_TIMEOUT
98 #define NX_PPPOE_CLIENT_PADI_INIT_TIMEOUT                   1
99 #endif
100 
101 /* Define the maximum number for PADI.  */
102 #ifndef NX_PPPOE_CLIENT_PADI_COUNT
103 #define NX_PPPOE_CLIENT_PADI_COUNT                          4
104 #endif
105 
106 /* Define the init timeout for PADR.  */
107 #ifndef NX_PPPOE_CLIENT_PADR_INIT_TIMEOUT
108 #define NX_PPPOE_CLIENT_PADR_INIT_TIMEOUT                   1
109 #endif
110 
111 /* Define the maximum number for PADR.  */
112 #ifndef NX_PPPOE_CLIENT_PADR_COUNT
113 #define NX_PPPOE_CLIENT_PADR_COUNT                          4
114 #endif
115 
116 /* Define the size of pppoe AC-Name.  */
117 #ifndef NX_PPPOE_CLIENT_MAX_AC_NAME_SIZE
118 #define NX_PPPOE_CLIENT_MAX_AC_NAME_SIZE                    32
119 #endif
120 
121 /* Define the size of pppoe AC-Cookie.  */
122 #ifndef NX_PPPOE_CLIENT_MAX_AC_COOKIE_SIZE
123 #define NX_PPPOE_CLIENT_MAX_AC_COOKIE_SIZE                  32
124 #endif
125 
126 /* Define the size of pppoe Relay-Session-Id.  */
127 #ifndef NX_PPPOE_CLIENT_MAX_RELAY_SESSION_ID_SIZE
128 #define NX_PPPOE_CLIENT_MAX_RELAY_SESSION_ID_SIZE           12
129 #endif
130 
131 /* Define the minimum size of packet payload to avoid packet chained. Maximum Payload Size of Ethernet(1500) + Ethernet Header + CRC + Four-byte alignment.  */
132 
133 #ifndef NX_PPPOE_CLIENT_MIN_PACKET_PAYLOAD_SIZE
134 #define NX_PPPOE_CLIENT_MIN_PACKET_PAYLOAD_SIZE             1520
135 #endif
136 
137 /* Define PPPoE ethernet header size.  */
138 #define NX_PPPOE_CLIENT_ETHER_HEADER_SIZE                   14
139 
140 /* Define PPPoE ethernet types.  */
141 #define NX_PPPOE_CLIENT_ETHER_TYPE_DISCOVERY                0x8863
142 #define NX_PPPOE_CLIENT_ETHER_TYPE_SESSION                  0x8864
143 
144 /* Define PPPoE version and type.   */
145 #define NX_PPPOE_CLIENT_VERSION_TYPE                        0x11     /* Version 1, Type 1.  */
146 
147 /* Define PPPoE codes.   */
148 #define NX_PPPOE_CLIENT_CODE_ZERO                           0x00
149 #define NX_PPPOE_CLIENT_CODE_PADI                           0x09
150 #define NX_PPPOE_CLIENT_CODE_PADO                           0x07
151 #define NX_PPPOE_CLIENT_CODE_PADR                           0x19
152 #define NX_PPPOE_CLIENT_CODE_PADS                           0x65
153 #define NX_PPPOE_CLIENT_CODE_PADT                           0xa7
154 
155 /* Define the PPPoE Area Offsets.  */
156 #define NX_PPPOE_CLIENT_OFFSET_VER_TYPE                     0       /* 1 byte, version + type: 0x11                     */
157 #define NX_PPPOE_CLIENT_OFFSET_CODE                         1       /* 1 byte, code: Discovery or Session               */
158 #define NX_PPPOE_CLIENT_OFFSET_SESSION_ID                   2       /* 2 bytes, session id: unique session identifieer  */
159 #define NX_PPPOE_CLIENT_OFFSET_LENGTH                       4       /* 2 bytes, length: the length of PPPoE payload     */
160 #define NX_PPPOE_CLIENT_OFFSET_PAYLOAD                      6       /* variable, payload                                */
161 
162 /* Define the PPPoE Tag Types.  */
163 #define NX_PPPOE_CLIENT_TAG_TYPE_END_OF_LIST                0x0000
164 #define NX_PPPOE_CLIENT_TAG_TYPE_SERVICE_NAME               0x0101
165 #define NX_PPPOE_CLIENT_TAG_TYPE_AC_NAME                    0x0102
166 #define NX_PPPOE_CLIENT_TAG_TYPE_HOST_UNIQ                  0x0103
167 #define NX_PPPOE_CLIENT_TAG_TYPE_AC_COOKIE                  0x0104
168 #define NX_PPPOE_CLIENT_TAG_TYPE_VENDOR_SPECIFIC            0x0105
169 #define NX_PPPOE_CLIENT_TAG_TYPE_RELAY_SESSION_ID           0x0110
170 #define NX_PPPOE_CLIENT_TAG_TYPE_SERVICE_NAME_ERROR         0x0201
171 #define NX_PPPOE_CLIENT_TAG_TYPE_AC_SYSTEM_ERROR            0x0202
172 #define NX_PPPOE_CLIENT_TAG_TYPE_GENERIC_ERROR              0x0203
173 
174 /* Define the PPPoE Error flags.  */
175 #define NX_PPPOE_CLIENT_ERROR_SERVICE_NAME                  ((ULONG) 0x00000001)    /* Service Name Error.              */
176 #define NX_PPPOE_CLIENT_ERROR_AC_SYSTEM                     ((ULONG) 0x00000002)    /* AC-System Error                  */
177 #define NX_PPPOE_CLIENT_ERROR_GENERIC                       ((ULONG) 0x00000004)    /* Generic Error                    */
178 
179 /* Define event flags for PPPoE thread control.  */
180 #define NX_PPPOE_CLIENT_ALL_EVENTS                          ((ULONG) 0xFFFFFFFF)    /* All event flags                      */
181 #define NX_PPPOE_CLIENT_PACKET_RECEIVE_EVENT                ((ULONG) 0x00000001)    /* PPPoE Client receive packet event    */
182 #define NX_PPPOE_CLIENT_TIMER_PERIODIC_EVENT                ((ULONG) 0x00000002)    /* PPPoE CLient timer Periodic event    */
183 #define NX_PPPOE_CLIENT_SESSION_CONNECT_CLEANUP_EVENT       ((ULONG) 0x00000004)    /* PPPoE Client Session cleanup event   */
184 
185 /* Define PPPoE Client state.  */
186 #define NX_PPPOE_CLIENT_STATE_INITIAL                       0
187 #define NX_PPPOE_CLIENT_STATE_PADI_SENT                     1
188 #define NX_PPPOE_CLIENT_STATE_PADR_SENT                     2
189 #define NX_PPPOE_CLIENT_STATE_ESTABLISHED                   3
190 
191 /* Define error codes from PPPoE Server operation.  */
192 #define NX_PPPOE_CLIENT_SUCCESS                             0x00    /* Success                                           */
193 #define NX_PPPOE_CLIENT_PTR_ERROR                           0xD1    /* Invalid input pointers                            */
194 #define NX_PPPOE_CLIENT_INVALID_INTERFACE                   0xD2    /* Invalid interface                                 */
195 #define NX_PPPOE_CLIENT_PACKET_PAYLOAD_ERROR                0xD3    /* Invalid payload size of packet                    */
196 #define NX_PPPOE_CLIENT_MEMORY_SIZE_ERROR                   0xD4    /* Invalid memory size                               */
197 #define NX_PPPOE_CLIENT_PRIORITY_ERROR                      0xD5    /* Invalid priority                                  */
198 #define NX_PPPOE_CLIENT_NOT_ENABLED                         0xD6    /* PPPoE is not enabled                              */
199 #define NX_PPPOE_CLIENT_INVALID_SESSION                     0xD7    /* Invalid Session                                   */
200 #define NX_PPPOE_CLIENT_SESSION_NOT_ESTABLISHED             0xD8    /* PPPoE Session is not established                  */
201 #define NX_PPPOE_CLIENT_SERVICE_NAME_ERROR                  0xD9    /* Service name error                                */
202 #define NX_PPPOE_CLIENT_AC_NAME_ERROR                       0xDA    /* AC Name error                                     */
203 #define NX_PPPOE_CLIENT_CLIENT_SESSION_FULL                 0xDB    /* Client Session full                               */
204 #define NX_PPPOE_CLIENT_CLIENT_SESSION_NOT_FOUND            0xDC    /* Not found the client session                      */
205 #define NX_PPPOE_CLIENT_HOST_UNIQ_CACHE_ERROR               0xDD    /* The cache is not enough to record the Host Uniq   */
206 #define NX_PPPOE_CLIENT_RELAY_SESSION_ID_CACHE_ERROR        0xDF    /* The cache is not enough to record Relay Session ID*/
207 
208 
209 /* Define the PPPoE Server Session structure containing the session id and physical address.  */
210 
211 typedef struct NX_PPPOE_SERVER_SESSION_STRUCT
212 {
213 
214     USHORT          nx_pppoe_session_id;
215     USHORT          reserved;
216     ULONG           nx_pppoe_physical_address_msw;
217     ULONG           nx_pppoe_physical_address_lsw;
218 } NX_PPPOE_SERVER_SESSION;
219 
220 
221 /* Define the main PPPoE Server data structure.  */
222 
223 typedef struct NX_PPPOE_CLIENT_STRUCT
224 {
225 
226     ULONG                       nx_pppoe_id;
227     UCHAR                      *nx_pppoe_name;
228     NX_IP                      *nx_pppoe_ip_ptr;
229     NX_INTERFACE               *nx_pppoe_interface_ptr;
230     NX_PACKET_POOL             *nx_pppoe_packet_pool_ptr;
231     TX_EVENT_FLAGS_GROUP        nx_pppoe_events;
232     TX_THREAD                   nx_pppoe_thread;
233     TX_TIMER                    nx_pppoe_timer;
234     NX_PACKET                  *nx_pppoe_deferred_received_packet_head,
235                                *nx_pppoe_deferred_received_packet_tail;
236     UINT                        nx_pppoe_state;
237     NX_PPPOE_SERVER_SESSION     nx_pppoe_server_session;
238     UCHAR                      *nx_pppoe_service_name;
239     UINT                        nx_pppoe_service_name_length;
240     UCHAR                      *nx_pppoe_host_uniq;
241     UINT                        nx_pppoe_host_uniq_length;
242     UCHAR                       nx_pppoe_ac_name[NX_PPPOE_CLIENT_MAX_AC_NAME_SIZE];
243     UINT                        nx_pppoe_ac_name_size;
244     UCHAR                       nx_pppoe_ac_cookie[NX_PPPOE_CLIENT_MAX_AC_COOKIE_SIZE];
245     UINT                        nx_pppoe_ac_cookie_size;
246     UCHAR                       nx_pppoe_relay_session_id[NX_PPPOE_CLIENT_MAX_RELAY_SESSION_ID_SIZE];
247     UINT                        nx_pppoe_relay_session_id_size;
248     UINT                        nx_pppoe_error_flag;
249 
250     /* Define the retransmit timeout and count for PADI and PADR.  */
251     UINT                        nx_pppoe_rtr_timeout;
252     UINT                        nx_pppoe_rtr_count;
253 
254     /* Define the Link Driver entry point.  */
255     VOID                      (*nx_pppoe_link_driver_entry)(struct NX_IP_DRIVER_STRUCT *);
256 
257     /* Define the function to receive PPPoE packet.  */
258     VOID                      (*nx_pppoe_packet_receive)(NX_PACKET *packet_ptr);
259 
260     /* Define the PPPoE session connect suspension pointer that contains the pointer to the
261        thread suspended attempting to establish a PPPoE session connection.  */
262     TX_THREAD                  *nx_pppoe_session_connect_suspended_thread;
263 
264 } NX_PPPOE_CLIENT;
265 
266 
267 #ifndef NX_PPPOE_CLIENT_SOURCE_CODE
268 
269 /* Application caller is present, perform API mapping.  */
270 
271 /* Determine if error checking is desired.  If so, map API functions
272    to the appropriate error checking front-ends.  Otherwise, map API
273    functions to the core functions that actually perform the work.
274    Note: error checking is enabled by default.  */
275 
276 #ifdef NX_DISABLE_ERROR_CHECKING
277 
278 /* Services without error checking.  */
279 
280 #define nx_pppoe_client_create              _nx_pppoe_client_create
281 #define nx_pppoe_client_delete              _nx_pppoe_client_delete
282 #define nx_pppoe_client_host_uniq_set       _nx_pppoe_client_host_uniq_set
283 #define nx_pppoe_client_host_uniq_set_extended _nx_pppoe_client_host_uniq_set_extended
284 #define nx_pppoe_client_service_name_set    _nx_pppoe_client_service_name_set
285 #define nx_pppoe_client_service_name_set_extended _nx_pppoe_client_service_name_set_extended
286 #define nx_pppoe_client_session_connect     _nx_pppoe_client_session_connect
287 #define nx_pppoe_client_session_packet_send _nx_pppoe_client_session_packet_send
288 #define nx_pppoe_client_session_terminate   _nx_pppoe_client_session_terminate
289 #define nx_pppoe_client_session_get         _nx_pppoe_client_session_get
290 
291 #else
292 
293 /* Services with error checking.  */
294 
295 #define nx_pppoe_client_create              _nxe_pppoe_client_create
296 #define nx_pppoe_client_delete              _nxe_pppoe_client_delete
297 #define nx_pppoe_client_host_uniq_set       _nxe_pppoe_client_host_uniq_set
298 #define nx_pppoe_client_host_uniq_set_extended _nxe_pppoe_client_host_uniq_set_extended
299 #define nx_pppoe_client_service_name_set    _nxe_pppoe_client_service_name_set
300 #define nx_pppoe_client_service_name_set_extended _nxe_pppoe_client_service_name_set_extended
301 #define nx_pppoe_client_session_connect     _nxe_pppoe_client_session_connect
302 #define nx_pppoe_client_session_packet_send _nxe_pppoe_client_session_packet_send
303 #define nx_pppoe_client_session_terminate   _nxe_pppoe_client_session_terminate
304 #define nx_pppoe_client_session_get         _nxe_pppoe_client_session_get
305 
306 #endif /* NX_DISABLE_ERROR_CHECKING */
307 
308 /* Define the prototypes accessible to the application software.  */
309 
310 UINT    nx_pppoe_client_create(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *name, NX_IP *ip_ptr, UINT interface_index,
311                                NX_PACKET_POOL *pool_ptr, VOID *stack_ptr, ULONG stack_size, UINT priority,
312                                VOID (*pppoe_link_driver)(struct NX_IP_DRIVER_STRUCT *),
313                                VOID (*pppoe_packet_receive)(NX_PACKET *packet_ptr));
314 UINT    nx_pppoe_client_delete(NX_PPPOE_CLIENT *pppoe_client_ptr);
315 UINT    nx_pppoe_client_host_uniq_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq);
316 UINT    nx_pppoe_client_host_uniq_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq, UINT host_uniq_length);
317 UINT    nx_pppoe_client_service_name_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name);
318 UINT    nx_pppoe_client_service_name_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name, UINT service_name_length);
319 UINT    nx_pppoe_client_session_connect(NX_PPPOE_CLIENT *pppoe_client_ptr, ULONG wait_option);
320 UINT    nx_pppoe_client_session_packet_send(NX_PPPOE_CLIENT *pppoe_client_ptr, NX_PACKET *packet_ptr);
321 UINT    nx_pppoe_client_session_terminate(NX_PPPOE_CLIENT *pppoe_client_ptr);
322 UINT    nx_pppoe_client_session_get(NX_PPPOE_CLIENT *pppoe_client_ptr, ULONG *server_mac_msw, ULONG *server_mac_lsw, ULONG *session_id);
323 VOID    _nx_pppoe_client_packet_deferred_receive(NX_PACKET *packet_ptr);
324 
325 #else
326 
327 UINT    _nxe_pppoe_client_create(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *name, NX_IP *ip_ptr, UINT interface_index,
328                                NX_PACKET_POOL *pool_ptr, VOID *stack_ptr, ULONG stack_size, UINT priority,
329                                VOID (*pppoe_link_driver)(struct NX_IP_DRIVER_STRUCT *),
330                                VOID (*pppoe_packet_receive)(NX_PACKET *packet_ptr));
331 UINT    _nx_pppoe_client_create(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *name, NX_IP *ip_ptr, UINT interface_index,
332                                NX_PACKET_POOL *pool_ptr, VOID *stack_ptr, ULONG stack_size, UINT priority,
333                                VOID (*pppoe_link_driver)(struct NX_IP_DRIVER_STRUCT *),
334                                VOID (*pppoe_packet_receive)(NX_PACKET *packet_ptr));
335 UINT    _nxe_pppoe_client_delete(NX_PPPOE_CLIENT *pppoe_client_ptr);
336 UINT    _nx_pppoe_client_delete(NX_PPPOE_CLIENT *pppoe_client_ptr);
337 UINT    _nxe_pppoe_client_host_uniq_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq);
338 UINT    _nx_pppoe_client_host_uniq_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq);
339 UINT    _nxe_pppoe_client_host_uniq_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq, UINT host_uniq_length);
340 UINT    _nx_pppoe_client_host_uniq_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *host_uniq, UINT host_uniq_length);
341 UINT    _nxe_pppoe_client_service_name_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name);
342 UINT    _nx_pppoe_client_service_name_set(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name);
343 UINT    _nxe_pppoe_client_service_name_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name, UINT service_name_length);
344 UINT    _nx_pppoe_client_service_name_set_extended(NX_PPPOE_CLIENT *pppoe_client_ptr, UCHAR *service_name, UINT service_name_length);
345 UINT    _nxe_pppoe_client_session_connect(NX_PPPOE_CLIENT *pppoe_client_ptr, ULONG wait_option);
346 UINT    _nx_pppoe_client_session_connect(NX_PPPOE_CLIENT *pppoe_client_ptr, ULONG wait_option);
347 UINT    _nxe_pppoe_client_session_packet_send(NX_PPPOE_CLIENT *pppoe_client_ptr, NX_PACKET *packet_ptr);
348 UINT    _nx_pppoe_client_session_packet_send(NX_PPPOE_CLIENT *pppoe_client_ptr, NX_PACKET *packet_ptr);
349 UINT    _nxe_pppoe_client_session_terminate(NX_PPPOE_CLIENT *pppoe_client_ptr);
350 UINT    _nx_pppoe_client_session_terminate(NX_PPPOE_CLIENT *pppoe_client_ptr);
351 UINT    _nxe_pppoe_client_session_get(NX_PPPOE_CLIENT *pppoe_client_ptr, ULONG *server_mac_msw, ULONG *server_mac_lsw, ULONG *session_id);
352 UINT    _nx_pppoe_client_session_get(NX_PPPOE_CLIENT *pppoe_server_ptr, ULONG *server_mac_msw, ULONG *server_mac_lsw, ULONG *session_id);
353 VOID    _nx_pppoe_client_packet_deferred_receive(NX_PACKET *packet_ptr);
354 
355 #endif /* NX_PPPOE_CLIENT_SOURCE_CODE */
356 
357 
358 /* Determine if a C++ compiler is being used.  If so, complete the standard
359    C conditional started above.  */
360 #ifdef   __cplusplus
361         }
362 #endif
363 
364 #endif /* NX_PPPOE_CLIENT_H */
365