1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "net_device.h"
10 #include "securec.h"
11
12 #include "osal_mem.h"
13 #include "net_device_impl.h"
14 #include "net_device_adapter.h"
15
16 #define HDF_LOG_TAG "NetDevice"
17
18 static struct NetDeviceImpl *g_netDeviceImplTable[MAX_NETDEVICE_COUNT] = {NULL};
19
FindAvailableTable(uint32_t * index)20 static bool FindAvailableTable(uint32_t *index)
21 {
22 uint32_t i;
23
24 if (index == NULL) {
25 HDF_LOGE("%s Find Available table index error!", __func__);
26 return false;
27 }
28 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
29 if (g_netDeviceImplTable[i] == NULL) {
30 *index = i;
31 return true;
32 }
33 }
34 return false;
35 }
36
AddNetDeviceImplToTable(uint32_t index,struct NetDeviceImpl * netDeviceImpl)37 static bool AddNetDeviceImplToTable(uint32_t index, struct NetDeviceImpl *netDeviceImpl)
38 {
39 if (index >= MAX_NETDEVICE_COUNT) {
40 HDF_LOGE("%s error because of not enough space!", __func__);
41 return false;
42 }
43 g_netDeviceImplTable[index] = netDeviceImpl;
44 return true;
45 }
46
DeleteNetDeviceImplFromTable(const struct NetDeviceImpl * netDeviceImpl)47 static void DeleteNetDeviceImplFromTable(const struct NetDeviceImpl *netDeviceImpl)
48 {
49 int32_t i;
50
51 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
52 if (g_netDeviceImplTable[i] == netDeviceImpl) {
53 g_netDeviceImplTable[i] = NULL;
54 return;
55 }
56 }
57 return;
58 }
59
InitNetDeviceImpl(NetDevice * nd,NetIfCategory ifCategory)60 static struct NetDeviceImpl *InitNetDeviceImpl(NetDevice *nd, NetIfCategory ifCategory)
61 {
62 struct NetDeviceImpl *ndImpl = NULL;
63 if (nd == NULL) {
64 return NULL;
65 }
66 ndImpl = (struct NetDeviceImpl *)OsalMemCalloc(sizeof(struct NetDeviceImpl));
67 if (ndImpl == NULL) {
68 HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__);
69 return NULL;
70 }
71
72 ndImpl->netDevice = nd;
73 if (RegisterNetDeviceImpl(ndImpl) != HDF_SUCCESS) {
74 HDF_LOGE("%s fail: resiter lite impl fail!", __func__);
75 OsalMemFree(ndImpl);
76 ndImpl = NULL;
77 }
78 return ndImpl;
79 }
80
DeInitNetDeviceImpl(struct NetDeviceImpl * netDeviceImpl)81 static void DeInitNetDeviceImpl(struct NetDeviceImpl *netDeviceImpl)
82 {
83 if (netDeviceImpl == NULL) {
84 HDF_LOGE("%s success : already free!", __func__);
85 return;
86 }
87
88 /* release osPrivate */
89 if (netDeviceImpl->interFace != NULL && netDeviceImpl->interFace->deInit != NULL) {
90 netDeviceImpl->interFace->deInit(netDeviceImpl);
91 }
92
93 /* step 1 : release interFace. step 2 : release netdevice */
94 if (netDeviceImpl->netDevice != NULL) {
95 UnRegisterNetDeviceImpl(netDeviceImpl);
96 OsalMemFree(netDeviceImpl->netDevice);
97 netDeviceImpl->netDevice = NULL;
98 }
99
100 /* last release netDeviceImpl */
101 OsalMemFree(netDeviceImpl);
102 netDeviceImpl = NULL;
103 HDF_LOGI("%s success!", __func__);
104 return;
105 }
106
GetImplByNetDevice(const struct NetDevice * netDevice)107 static struct NetDeviceImpl *GetImplByNetDevice(const struct NetDevice *netDevice)
108 {
109 struct NetDeviceImpl *ndImpl = NULL;
110 int32_t i;
111
112 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
113 if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
114 if (netDevice == g_netDeviceImplTable[i]->netDevice) {
115 ndImpl = g_netDeviceImplTable[i];
116 return ndImpl;
117 }
118 }
119 }
120 HDF_LOGE("%s Get Impl by netdevice failed", __func__);
121 return ndImpl;
122 }
123
NetDeviceInit(const char * ifName,uint32_t len,NetLinkType type,NetIfCategory ifCategory)124 struct NetDevice *NetDeviceInit(const char *ifName, uint32_t len, NetLinkType type, NetIfCategory ifCategory)
125 {
126 NetDevice *netDevice = NULL;
127 struct NetDeviceImpl *ndImpl = NULL;
128 uint32_t index = 0;
129 int32_t ret;
130
131 if ((ifName == NULL) || (strlen(ifName) != len) || (strlen(ifName) > IFNAMSIZ - 1)) {
132 HDF_LOGE("%s fail: ifName = null or len not right!", __func__);
133 return NULL;
134 }
135 netDevice = (NetDevice *)OsalMemCalloc(sizeof(NetDevice));
136 if (netDevice == NULL) {
137 HDF_LOGE("%s fail: OsalMemCalloc fail!", __func__);
138 return NULL;
139 }
140 if (strcpy_s(netDevice->name, IFNAMSIZ, ifName) != EOK) {
141 HDF_LOGE("%s fail: strcpy_s fail!", __func__);
142 OsalMemFree(netDevice);
143 return NULL;
144 }
145 netDevice->netifCateg = ifCategory;
146 ndImpl = InitNetDeviceImpl(netDevice, ifCategory);
147 if (ndImpl == NULL) {
148 HDF_LOGE("%s fail: InitNetDeviceImpl fail!", __func__);
149 OsalMemFree(netDevice);
150 return NULL;
151 }
152 if (FindAvailableTable(&index)) {
153 AddNetDeviceImplToTable(index, ndImpl);
154 } else {
155 DeInitNetDeviceImpl(ndImpl);
156 HDF_LOGE("%s fail: Not extra table.", __func__);
157 return NULL;
158 }
159 /* INIT OSPrivate */
160 ret = HDF_FAILURE;
161 if (ndImpl->interFace != NULL && ndImpl->interFace->init != NULL) {
162 ret = ndImpl->interFace->init(ndImpl);
163 }
164 if (ret != HDF_SUCCESS) {
165 HDF_LOGE("%s fail : interface->init fail!", __func__);
166 DeleteNetDeviceImplFromTable(ndImpl);
167 DeInitNetDeviceImpl(ndImpl);
168 return NULL;
169 }
170 netDevice->LinkLayerType = type;
171 HDF_LOGI("Init Net Device success!");
172 return netDevice;
173 }
174
NetDeviceDeInit(struct NetDevice * netDevice)175 int32_t NetDeviceDeInit(struct NetDevice *netDevice)
176 {
177 struct NetDeviceImpl *ndImpl = NULL;
178
179 if (netDevice == NULL) {
180 HDF_LOGI("%s success: already deinit!", __func__);
181 return HDF_SUCCESS;
182 }
183 ndImpl = GetImplByNetDevice(netDevice);
184 if (ndImpl == NULL) {
185 HDF_LOGI("%s success: already free.", __func__);
186 OsalMemFree(netDevice);
187 return HDF_SUCCESS;
188 }
189 DeleteNetDeviceImplFromTable(ndImpl);
190 DeInitNetDeviceImpl(ndImpl);
191 return HDF_SUCCESS;
192 }
193
NetDeviceAdd(struct NetDevice * netDevice)194 int32_t NetDeviceAdd(struct NetDevice *netDevice)
195 {
196 struct NetDeviceImplOp *op = NULL;
197 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
198
199 if (ndImpl == NULL) {
200 HDF_LOGE("%s fail: netDevice not exist!", __func__);
201 return HDF_ERR_INVALID_PARAM;
202 }
203 op = ndImpl->interFace;
204 if (op == NULL || op->add == NULL) {
205 HDF_LOGE("%s fail: Impl Add not exist.", __func__);
206 return HDF_ERR_INVALID_PARAM;
207 }
208 return op->add(ndImpl);
209 }
210
NetDeviceDelete(struct NetDevice * netDevice)211 int32_t NetDeviceDelete(struct NetDevice *netDevice)
212 {
213 struct NetDeviceImplOp *op = NULL;
214 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
215
216 if (ndImpl == NULL) {
217 HDF_LOGE("%s fail: netDevice not exist!", __func__);
218 return HDF_ERR_INVALID_PARAM;
219 }
220 op = ndImpl->interFace;
221 if (op == NULL || op->delete == NULL) {
222 HDF_LOGE("%s fail: Impl op Delete exist.", __func__);
223 return HDF_ERR_INVALID_PARAM;
224 }
225 return op->delete(ndImpl);
226 }
227
NetDeviceGetInstByName(const char * name)228 struct NetDevice *NetDeviceGetInstByName(const char *name)
229 {
230 int32_t i;
231
232 if (name == NULL) {
233 HDF_LOGE("%s fail: name = NULL.", __func__);
234 return NULL;
235 }
236 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
237 if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
238 if (strcmp(g_netDeviceImplTable[i]->netDevice->name, name) == 0) {
239 return g_netDeviceImplTable[i]->netDevice;
240 }
241 }
242 }
243 HDF_LOGE("%s fail: %s: name not exist.", __func__, name);
244 return NULL;
245 }
246
NetDeviceIsAnyInstRunning(void)247 bool NetDeviceIsAnyInstRunning(void)
248 {
249 struct NetDevice *netDev = NULL;
250 int32_t i;
251
252 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
253 if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
254 netDev = g_netDeviceImplTable[i]->netDevice;
255 if ((GET_NET_DEV_FLAGS(netDev) & NET_DEVICE_IFF_RUNNING) != 0) {
256 return true;
257 }
258 }
259 }
260 return false;
261 }
262
NetDeviceIsInstRunning(const struct NetDevice * netDevice)263 bool NetDeviceIsInstRunning(const struct NetDevice *netDevice)
264 {
265 if (netDevice == NULL) {
266 return false;
267 }
268 if ((GET_NET_DEV_FLAGS(netDevice) & NET_DEVICE_IFF_RUNNING) != 0) {
269 return true;
270 }
271 return false;
272 }
273
NetDevGetRegisterCount(void)274 uint32_t NetDevGetRegisterCount(void)
275 {
276 uint32_t count = 0;
277 int32_t i;
278
279 for (i = 0; i < MAX_NETDEVICE_COUNT; i++) {
280 if (g_netDeviceImplTable[i] != NULL && g_netDeviceImplTable[i]->netDevice != NULL) {
281 count++;
282 }
283 }
284 return count;
285 }
286
NetDeviceGetCap(void)287 uint32_t NetDeviceGetCap(void)
288 {
289 return MAX_NETDEVICE_COUNT;
290 }
291
NetDeviceGetInstByIndex(uint32_t index)292 struct NetDevice *NetDeviceGetInstByIndex(uint32_t index)
293 {
294 if (index < MAX_NETDEVICE_COUNT) {
295 if (g_netDeviceImplTable[index] != NULL) {
296 return g_netDeviceImplTable[index]->netDevice;
297 } else {
298 return NULL;
299 }
300 }
301 return NULL;
302 }
303
NetIfSetAddr(const struct NetDevice * netDevice,const IpV4Addr * ipAddr,const IpV4Addr * netMask,const IpV4Addr * gw)304 int32_t NetIfSetAddr(const struct NetDevice *netDevice, const IpV4Addr *ipAddr, const IpV4Addr *netMask,
305 const IpV4Addr *gw)
306 {
307 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
308 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setIpAddr != NULL) {
309 return ndImpl->interFace->setIpAddr(ndImpl, ipAddr, netMask, gw);
310 }
311 HDF_LOGE("%s: netDevice not init or already free.", __func__);
312 return HDF_ERR_INVALID_PARAM;
313 }
314
NetIfRxImpl(const struct NetDevice * netDevice,NetBuf * buff,ReceiveFlag flag)315 static int32_t NetIfRxImpl(const struct NetDevice *netDevice, NetBuf *buff, ReceiveFlag flag)
316 {
317 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
318 ProcessingResult ret = PROCESSING_CONTINUE;
319
320 if (ndImpl == NULL || ndImpl->interFace == NULL || ndImpl->interFace->receive == NULL) {
321 HDF_LOGE("%s: NetIfRxImpl fail : netdevice not exist!", __func__);
322 return HDF_ERR_INVALID_PARAM;
323 }
324
325 /* to do driver special process */
326 if (netDevice->netDeviceIf != NULL && netDevice->netDeviceIf->specialEtherTypeProcess != NULL) {
327 ret = netDevice->netDeviceIf->specialEtherTypeProcess(netDevice, buff);
328 }
329 /* Sent to TCP/IP Stack. */
330 if (ret == PROCESSING_CONTINUE) {
331 return ndImpl->interFace->receive(ndImpl, buff, flag);
332 } else if (ret == PROCESSING_COMPLETE) {
333 HDF_LOGI("NetIfRxImpl specialEtherType Process not need TCP/IP stack!");
334 return HDF_SUCCESS;
335 } else {
336 HDF_LOGE("%s: NetIfRxImpl specialEtherType Process error", __func__);
337 return HDF_FAILURE;
338 }
339 }
340
NetIfSetMacAddr(struct NetDevice * netDevice,const unsigned char * macAddr,unsigned char length)341 int32_t NetIfSetMacAddr(struct NetDevice *netDevice, const unsigned char *macAddr, unsigned char length)
342 {
343 HDF_STATUS ret;
344 struct NetDeviceImpl *ndImpl = NULL;
345
346 if (macAddr == NULL || length != MAC_ADDR_SIZE) {
347 HDF_LOGE("%s fail: input param error!", __func__);
348 return HDF_ERR_INVALID_PARAM;
349 }
350 if (memcpy_s(netDevice->macAddr, MAC_ADDR_SIZE, macAddr, MAC_ADDR_SIZE) != EOK) {
351 HDF_LOGE("%s fail : memcpy_s fail!", __func__);
352 return HDF_FAILURE;
353 }
354 if (netDevice->netDeviceIf != NULL && netDevice->netDeviceIf->setMacAddr != NULL) {
355 ret = netDevice->netDeviceIf->setMacAddr(netDevice, (void*)macAddr);
356 if (ret != HDF_SUCCESS) {
357 HDF_LOGE("%s fail : setMacAddr fail!", __func__);
358 return ret;
359 }
360 }
361 ndImpl = GetImplByNetDevice(netDevice);
362 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->changeMacAddr != NULL) {
363 return ndImpl->interFace->changeMacAddr(ndImpl);
364 }
365 return HDF_FAILURE;
366 }
367
NetIfRx(const struct NetDevice * netDevice,NetBuf * buff)368 int32_t NetIfRx(const struct NetDevice *netDevice, NetBuf *buff)
369 {
370 return NetIfRxImpl(netDevice, buff, IN_INTERRUPT);
371 }
372
NetIfRxNi(const struct NetDevice * netDevice,NetBuf * buff)373 int32_t NetIfRxNi(const struct NetDevice *netDevice, NetBuf *buff)
374 {
375 return NetIfRxImpl(netDevice, buff, NO_IN_INTERRUPT);
376 }
377
NetIfSetStatus(const struct NetDevice * netDevice,NetIfStatus status)378 int32_t NetIfSetStatus(const struct NetDevice *netDevice, NetIfStatus status)
379 {
380 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
381 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setStatus != NULL) {
382 return ndImpl->interFace->setStatus(ndImpl, status);
383 }
384 HDF_LOGE("%s: netDevice not init or already free.", __func__);
385 return HDF_ERR_INVALID_PARAM;
386 }
387
NetIfSetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus status)388 int32_t NetIfSetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus status)
389 {
390 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
391 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->setLinkStatus != NULL) {
392 return ndImpl->interFace->setLinkStatus(ndImpl, status);
393 }
394 HDF_LOGE("%s: netDevice not init or already free.", __func__);
395 return HDF_ERR_INVALID_PARAM;
396 }
397
NetIfGetLinkStatus(const struct NetDevice * netDevice,NetIfLinkStatus * status)398 int32_t NetIfGetLinkStatus(const struct NetDevice *netDevice, NetIfLinkStatus *status)
399 {
400 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
401 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->getLinkStatus != NULL) {
402 return ndImpl->interFace->getLinkStatus(ndImpl, status);
403 }
404 HDF_LOGE("%s: netDevice not init or already free.", __func__);
405 return HDF_ERR_INVALID_PARAM;
406 }
407
NetIfDhcpsStart(const struct NetDevice * netDevice,char * ip,uint16_t ipNum)408 int32_t NetIfDhcpsStart(const struct NetDevice *netDevice, char *ip, uint16_t ipNum)
409 {
410 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
411 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStart != NULL) {
412 return ndImpl->interFace->dhcpsStart(ndImpl, ip, ipNum);
413 }
414 HDF_LOGE("%s: netDevice not init or already free.", __func__);
415 return HDF_ERR_INVALID_PARAM;
416 }
417
NetIfDhcpsStop(const struct NetDevice * netDevice)418 int32_t NetIfDhcpsStop(const struct NetDevice *netDevice)
419 {
420 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
421 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpsStop != NULL) {
422 return ndImpl->interFace->dhcpsStop(ndImpl);
423 }
424 HDF_LOGE("%s: netDevice not init or already free.", __func__);
425 return HDF_ERR_INVALID_PARAM;
426 }
427
NetIfDhcpStart(const struct NetDevice * netDevice)428 int32_t NetIfDhcpStart(const struct NetDevice *netDevice)
429 {
430 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
431 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStart != NULL) {
432 return ndImpl->interFace->dhcpStart(ndImpl);
433 }
434 HDF_LOGE("%s: netDevice not init or already free.", __func__);
435 return HDF_ERR_INVALID_PARAM;
436 }
437
NetIfDhcpStop(const struct NetDevice * netDevice)438 int32_t NetIfDhcpStop(const struct NetDevice *netDevice)
439 {
440 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
441 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpStop != NULL) {
442 return ndImpl->interFace->dhcpStop(ndImpl);
443 }
444 HDF_LOGE("%s: netDevice not init or already free.", __func__);
445 return HDF_ERR_INVALID_PARAM;
446 }
447
NetIfDhcpIsBound(const struct NetDevice * netDevice)448 int32_t NetIfDhcpIsBound(const struct NetDevice *netDevice)
449 {
450 struct NetDeviceImpl *ndImpl = GetImplByNetDevice(netDevice);
451 if (ndImpl != NULL && ndImpl->interFace != NULL && ndImpl->interFace->dhcpIsBound != NULL) {
452 return ndImpl->interFace->dhcpIsBound(ndImpl);
453 }
454 HDF_LOGE("%s: netDevice not init or already free.", __func__);
455 return HDF_ERR_INVALID_PARAM;
456 }
457
458 /*
459 * Alloc a net buffer for the net device and reserve headroom depended on net device setting
460 *
461 * @param : dev The net device
462 * size The net buffer size
463 * @return : A new net buffer on success or NULL on fail
464 */
NetBufDevAlloc(const struct NetDevice * dev,uint32_t size)465 NetBuf *NetBufDevAlloc(const struct NetDevice *dev, uint32_t size)
466 {
467 uint32_t reserve = 0;
468 NetBuf *nb = NULL;
469
470 if (dev != NULL) {
471 reserve = dev->neededHeadRoom + dev->neededTailRoom;
472 }
473
474 size += reserve;
475
476 nb = NetBufAlloc(size);
477 if (nb == NULL) {
478 return NULL;
479 }
480
481 if (dev != NULL) {
482 nb->dev = (void *)dev;
483 NetBufPop(nb, E_TAIL_BUF, dev->neededHeadRoom);
484 NetBufPop(nb, E_DATA_BUF, dev->neededHeadRoom);
485 }
486
487 return nb;
488 }
489