• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
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 #ifndef _PORTING_NET_AL_H
16 #define _PORTING_NET_AL_H
17 
18 #include "porting_net_def.h"
19 
20 // Forward declarations
21 struct fhost_vif_tag;
22 
23 /// Prototype for a function to free a network buffer */
24 typedef void (*net_buf_free_fn)(void *net_buf);
25 
26 /*
27  * FUNCTIONS
28  ****************************************************************************************
29  */
30 /**
31  ****************************************************************************************
32  * @brief Call the checksum computation function of the TCP/IP stack
33  *
34  * @param[in] dataptr Pointer to the data buffer on which the checksum is computed
35  * @param[in] len Length of the data buffer
36  *
37  * @return The computed checksum
38  ****************************************************************************************
39  */
40 uint16_t net_ip_chksum(const void *dataptr, int len);
41 
42 /**
43  ****************************************************************************************
44  * @brief Add a network interface
45  *
46  * @param[in] net_if Pointer to the net_if structure to add
47  * @param[in] ipaddr Pointer to the IP address of the interface (NULL if not available)
48  * @param[in] netmask Pointer to the net mask of the interface (NULL if not available)
49  * @param[in] gw Pointer to the gateway address of the interface (NULL if not available)
50  * @param[in] vif Pointer to the VIF information structure associated to this interface
51  *
52  * @return 0 on success and != 0 if error occurred
53  ****************************************************************************************
54  */
55 int net_if_add(net_if_t *net_if,
56                const uint32_t *ipaddr,
57                const uint32_t *netmask,
58                const uint32_t *gw,
59                struct fhost_vif_tag *vif);
60 
61 /**
62  ****************************************************************************************
63  * @brief Get network interface MAC address
64  *
65  * @param[in] net_if Pointer to the net_if structure
66  *
67  * @return Pointer to interface MAC address
68  ****************************************************************************************
69  */
70 const uint8_t *net_if_get_mac_addr(net_if_t *net_if);
71 
72 /**
73  ****************************************************************************************
74  * @brief Get pointer to network interface from its name
75  *
76  * @param[in] name Name of the interface
77  *
78  * @return pointer to the net_if structure and NULL if such interface doesn't exist.
79  ****************************************************************************************
80  */
81 net_if_t *net_if_find_from_name(const char *name);
82 
83 /**
84  ****************************************************************************************
85  * @brief Get pointer to network interface from its FHOST wifi index
86  *
87  * @param[in] idx Index of the FHOST wifi interface
88  *
89  * @return pointer to the net_if structure and NULL if such interface doesn't exist.
90  ****************************************************************************************
91  */
92 net_if_t *net_if_find_from_wifi_idx(unsigned int idx);
93 
94 /**
95  ****************************************************************************************
96  * @brief Get name of network interface
97  *
98  * Passing a buffer of at least @ref NET_AL_MAX_IFNAME bytes, will ensure that it is big
99  * enough to contain the interface name.
100  *
101  * @param[in]     net_if Pointer to the net_if structure
102  * @param[in]     buf    Buffer to write the interface name
103  * @param[in,out] len    Length of the buffer, updated with the actual length of the
104  *                       interface name (not including terminating null byte)
105  *
106  * @return 0 on success and != 0 if error occurred
107  ****************************************************************************************
108  */
109 int net_if_get_name(net_if_t *net_if, char *buf, int len);
110 
111 /**
112  ****************************************************************************************
113  * @brief Get index of the FHOST wifi interface for a network interface
114  *
115  * @param[in] net_if Pointer to the net_if structure
116  *
117  * @return <0 if cannot find the interface and the FHOST wifi interface index otherwise
118  ****************************************************************************************
119  */
120 int net_if_get_wifi_idx(net_if_t *net_if);
121 
122 /**
123  ****************************************************************************************
124  * @brief Indicate that the network interface is now up (i.e. able to do traffic)
125  *
126  * @param[in] net_if Pointer to the net_if structure
127  ****************************************************************************************
128  */
129 void net_if_up(net_if_t *net_if);
130 
131 /**
132  ****************************************************************************************
133  * @brief Indicate that the network interface is now down
134  *
135  * @param[in] net_if Pointer to the net_if structure
136  ****************************************************************************************
137  */
138 void net_if_down(net_if_t *net_if);
139 
140 /**
141  ****************************************************************************************
142  * @brief Set a network interface as the default output interface
143  *
144  * If IP routing failed to select an output interface solely based on interface and
145  * destination addresses then this interface will be selected.
146  * Use a NULL parameter to reset the default interface.
147  *
148  * @param[in] net_if Pointer to the net_if structure
149  ****************************************************************************************
150  */
151 void net_if_set_default(net_if_t *net_if);
152 
153 /**
154  ****************************************************************************************
155  * @brief Set IPv4 address of an interface
156  *
157  * It is assumed that only one address can be configured on the interface and then
158  * setting a new address can be used to replace/delete the current address.
159  *
160  * @param[in] net_if Pointer to the net_if structure
161  * @param[in] ip     IPv4 address
162  * @param[in] mask   IPv4 network mask
163  * @param[in] gw     IPv4 gateway address
164  ****************************************************************************************
165  */
166 void net_if_set_ip(net_if_t *net_if, uint32_t ip, uint32_t mask, uint32_t gw);
167 
168 /**
169  ****************************************************************************************
170  * @brief Get IPv4 address of an interface
171  *
172  * Set to NULL parameter you're not interested in.
173  *
174  * @param[in]  net_if Pointer to the net_if structure
175  * @param[out] ip     IPv4 address
176  * @param[out] mask   IPv4 network mask
177  * @param[out] gw     IPv4 gateway address
178  * @return 0 if requested parameters have been updated successfully and !=0 otherwise.
179  ****************************************************************************************
180  */
181 int net_if_get_ip(net_if_t *net_if, uint32_t *ip, uint32_t *mask, uint32_t *gw);
182 
183 /**
184  ****************************************************************************************
185  * @brief Call the networking stack input function.
186  * This function is supposed to link the payload data and length to the RX buffer
187  * structure passed as parameter. The free_fn function shall be called when the networking
188  * stack is not using the buffer anymore.
189  *
190  * @param[in] buf Pointer to the RX buffer structure
191  * @param[in] net_if Pointer to the net_if structure that receives the packet
192  * @param[in] addr Pointer to the payload data
193  * @param[in] len Length of the data available at payload address
194  * @param[in] free_fn Pointer to buffer freeing function to be called after use
195  *
196  * @return 0 on success and != 0 if packet is not accepted
197  ****************************************************************************************
198  */
199 int net_if_input(net_buf_rx_t *buf, net_if_t *net_if, void *addr, uint16_t len, net_buf_free_fn free_fn);
200 
201 /**
202  ****************************************************************************************
203  * @brief Get the pointer to the VIF structure attached to a net interface.
204  *
205  * @param[in] net_if Pointer to the net_if structure
206  *
207  * @return The pointer to the VIF structure attached to a net interface
208  ****************************************************************************************
209  */
210 struct fhost_vif_tag *net_if_vif_info(net_if_t *net_if);
211 
212  /**
213   ****************************************************************************************
214   * @brief Allocate a buffer for TX.
215   *
216   * This function is used to transmit buffer that do not originate from the Network Stack.
217   * (e.g. a management frame sent by wpa_supplicant)
218   * This function allocates a buffer for transmission and initializes its content with
219   * the payload passed as parameter. The buffer must still reserve @ref NET_AL_TX_HEADROOM
220   * headroom space like for regular TX buffers.
221   *
222   * @param[in] payload  Pointer to the buffer data
223   * @param[in] length   Size, in bytes, of the payload
224   *
225   * @return The pointer to the allocated TX buffer and NULL is allocation failed
226   ****************************************************************************************
227   */
228 net_buf_tx_t *net_buf_tx_alloc(const uint8_t *payload, uint32_t length);
229 
230 /**
231  ****************************************************************************************
232  * @brief Provides information on a TX buffer.
233  *
234  * This function is used by the FHOST module before queuing the buffer for transmission.
235  * This function must returns information (pointer and length) on all data segments that
236  * compose the buffer. Each buffer must at least have one data segment.
237  * It must also return a pointer to the headroom (of size @ref NET_AL_TX_HEADROOM)
238  * which must be reserved for each TX buffer on their first data segment.
239  *
240  * @param[in]     buf       Pointer to the TX buffer structure
241  * @param[in]     tot_len   Total size in bytes on the buffer (includes size of all data
242  *                          segment)
243  * @param[in,out] seg_cnt   Contains the maximum number of data segment supported (i.e.
244  *                          the size of @p seg_addr and @p seg_len parameter) and must be
245  *                          updated with the actual number of segment in this buffer.
246  * @param[out]    seg_addr  Table to retrieve the address of each segment.
247  * @param[out]    seg_len   Table to retrieve the length, in bytes, of each segment.
248  ****************************************************************************************
249  */
250 void net_buf_tx_info(net_buf_tx_t *buf, uint16_t *tot_len, uint8_t *seg_cnt);
251 
252 /**
253  ****************************************************************************************
254  * @brief Free a TX buffer that was involved in a transmission.
255  *
256  * @param[in] buf Pointer to the TX buffer structure
257  ****************************************************************************************
258  */
259 void net_buf_tx_free(net_buf_tx_t *buf);
260 
261 /**
262  ****************************************************************************************
263  * @brief Initialize the networking stack
264  *
265  * @return 0 on success and != 0 if packet is not accepted
266  ****************************************************************************************
267  */
268 int net_init(void);
269 
270 /**
271  ****************************************************************************************
272  * @brief Send a L2 (aka ethernet) packet
273  *
274  * Send data on the link layer (L2). If destination address is not NULL, Ethernet header
275  * will be added (using ethertype parameter) and MAC address of the sending interface is
276  * used as source address. If destination address is NULL, it means that ethernet header
277  * is already present and frame should be send as is.
278  * The data buffer will be copied by this function, and must then be freed by the caller.
279  *
280  * The primary purpose of this function is to allow the supplicant sending EAPOL frames.
281  * As these frames are often followed by addition/deletion of crypto keys, that
282  * can cause encryption to be enabled/disabled in the MAC, it is required to ensure that
283  * the packet transmission is completed before proceeding to the key setting.
284  * This function shall therefore be blocking until the frame has been transmitted by the
285  * MAC.
286  *
287  * @param[in] net_if    Pointer to the net_if structure.
288  * @param[in] data      Data buffer to send.
289  * @param[in] data_len  Buffer size, in bytes.
290  * @param[in] ethertype Ethernet type to set in the ethernet header. (in host endianess)
291  * @param[in] dst_addr  Ethernet address of the destination. If NULL then it means that
292  *                      ethernet header is already present in the frame (and in this case
293  *                      ethertype should be ignored)
294  * @param[out] ack      Optional to get transmission status. If not NULL, the value
295  *                      pointed is set to true if peer acknowledged the transmission and
296  *                      false in all other cases.
297  *
298  * @return 0 on success and != 0 if packet hasn't been sent
299  ****************************************************************************************
300  */
301 int net_l2_send(net_if_t *net_if, const uint8_t *data, int data_len, uint16_t ethertype,
302                 const uint8_t *dst_addr, bool *ack);
303 
304 /**
305  ****************************************************************************************
306  * @brief Create a L2 (aka ethernet) socket for specific packet
307  *
308  * Create a L2 socket that will receive specified frames: a given ethertype on a given
309  * interface.
310  * It is expected to fail if a L2 socket for the same ethertype/interface couple already
311  * exists.
312  *
313  * @note As L2 sockets are not specified in POSIX standard, the implementation of such
314  * function may be impossible in some network stack.
315  *
316  * @param[in] net_if    Pointer to the net_if structure.
317  * @param[in] ethertype Ethernet type to filter. (in host endianess)
318  *
319  * @return <0 if error occurred and the socket descriptor otherwise.
320  ****************************************************************************************
321  */
322 int net_l2_socket_create(net_if_t *net_if, uint16_t ethertype);
323 
324 /**
325  ****************************************************************************************
326  * @brief Delete a L2 (aka ethernet) socket
327  *
328  * @param[in] sock Socket descriptor returned by @ref net_l2_socket_create
329  *
330  * @return 0 on success and != 0 if error occurred.
331  ****************************************************************************************
332  **/
333 int net_l2_socket_delete(int sock);
334 
335 /**
336  ****************************************************************************************
337  * @brief Start DHCP procedure on a given interface
338  *
339  * @param[in] net_if Pointer to the interface on which DHCP must be started
340  *
341  * @return 0 if DHCP procedure successfully started and != 0 if an error occured
342  ****************************************************************************************
343  */
344 int net_dhcp_start(net_if_t *net_if);
345 
346 /**
347  ****************************************************************************************
348  * @brief Stop DHCP procedure on a given interface
349  *
350  * @param[in] net_if Pointer to the interface on which DHCP must be stoped
351  ****************************************************************************************
352  */
353 void net_dhcp_stop(net_if_t *net_if);
354 
355 /**
356  ****************************************************************************************
357  * @brief Release DHCP lease on a given interface
358  *
359  * @note Not needed if NET_AL_NO_IP is defined
360  *
361  * @param[in] net_if Pointer to the interface on which DHCP must be released
362  * @return 0 if DHCP lease has been released and != 0 if an error occurred
363  ****************************************************************************************
364  */
365 int net_dhcp_release(net_if_t *net_if);
366 
367 /**
368  ****************************************************************************************
369  * @brief Check if an IP has been assigned with DHCP
370  *
371  * @param[in] net_if Pointer to the interface to test
372  *
373  * @return 0 if ip address assigned to the interface has been obtained via DHCP and != 0
374  * otherwise
375  ****************************************************************************************
376  */
377 int net_dhcp_address_obtained(net_if_t *net_if);
378 
379 /**
380  ****************************************************************************************
381  * @brief Configure DNS server IP address
382  *
383  * @note Not needed if NET_AL_NO_IP is defined
384  *
385  * @param[in] dns_server  DNS server IPv4 address
386  * @return 0 on success and != 0 if error occurred.
387  ****************************************************************************************
388  */
389 int net_set_dns(uint32_t dns_server);
390 
391 /**
392  ****************************************************************************************
393  * @brief Get DNS server IP address
394  *
395  * @note Not needed if NET_AL_NO_IP is defined
396  *
397  * @param[out] dns_server  DNS server IPv4 address
398  * @return 0 on success and != 0 if error occurred.
399  ****************************************************************************************
400  */
401 int net_get_dns(uint32_t *dns_server);
402 
403 // void lwip_data_save(void);
404 // void lwip_data_restore(void);
405 int net_dhcp_start_status(void);
406 
407 #endif // _PORTING_NET_AL_H
408