-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
Copy pathesp_netif_lwip.c
2784 lines (2411 loc) · 92.9 KB
/
esp_netif_lwip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <inttypes.h>
#include <lwip/ip_addr.h>
#include <lwip/sockets.h>
#include "esp_compiler.h"
#include "esp_check.h"
#include "esp_netif_lwip_internal.h"
#include "lwip/esp_netif_net_stack.h"
#include "esp_netif.h"
#include "esp_netif_private.h"
#include "esp_random.h"
#include "esp_system.h"
#include "lwip/tcpip.h"
#include "lwip/dhcp.h"
#include "lwip/ip_addr.h"
#include "lwip/ip6_addr.h"
#include "lwip/mld6.h"
#include "lwip/prot/mld6.h"
#include "lwip/nd6.h"
#include "lwip/snmp.h"
#include "lwip/priv/tcpip_priv.h"
#include "lwip/netif.h"
#include "lwip/etharp.h"
#if CONFIG_ESP_NETIF_BRIDGE_EN
#include "netif/bridgeif.h"
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
#if LWIP_DNS /* don't build if not configured for use in lwipopts.h */
#include "lwip/dns.h"
#endif // LWIP_DNS
#if CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT
#include "lwip_default_hooks.h"
#endif // CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT
#include "esp_netif_lwip_ppp.h"
#if ESP_DHCPS
#include "dhcpserver/dhcpserver.h"
#include "dhcpserver/dhcpserver_options.h"
#endif
#include "netif/dhcp_state.h"
#include "esp_event.h"
#include "esp_log.h"
#if IP_NAPT
#include "lwip/lwip_napt.h"
#endif
//
// This is the main module implementing lwip interaction with esp-netif
//
#define ESP_NETIF_HOSTNAME_MAX_SIZE 32
#define DHCP_CB_CHANGE (LWIP_NSC_IPV4_SETTINGS_CHANGED | LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_GATEWAY_CHANGED | LWIP_NSC_IPV4_NETMASK_CHANGED)
/**
* @brief lwip thread safe tcpip function utility macros
*/
#define _RUN_IN_LWIP_TASK(function, netif, param) { return esp_netif_lwip_ipc_call(function, netif, (void *)(param)); }
/**
* @brief macros to check netif related data to evaluate interface type
*/
#if CONFIG_PPP_SUPPORT
#define _IS_NETIF_ANY_POINT2POINT_TYPE(netif) (netif && netif->related_data && netif->related_data->is_point2point)
#else
#define _IS_NETIF_ANY_POINT2POINT_TYPE(netif) false
#endif
#define _RUN_IN_LWIP_TASK_IF_SUPPORTED(function, netif, param) \
{ \
if (_IS_NETIF_ANY_POINT2POINT_TYPE(netif)) { \
return ESP_ERR_NOT_SUPPORTED; \
} \
return esp_netif_lwip_ipc_call(function, netif, (void *)(param)); \
}
/**
* @brief Utility macros to convert esp-ip addresses (both IPv6+IPv4 versions unconditionally)
* and lwip-ip addresses (contain only enabled portion of the address for each IP stack)
*/
#if LWIP_IPV4 && LWIP_IPV6
#define ESPIP_TO_IP(espip, ip) memcpy((ip), (espip), sizeof(ip_addr_t));
#define IP_TO_ESPIP(ip, espip) memcpy((espip), (ip), sizeof(ip_addr_t));
#elif LWIP_IPV4
#define ESPIP_TO_IP(espip, ip) memcpy((ip), &((espip)->u_addr.ip4), sizeof(ip_addr_t));
#define IP_TO_ESPIP(ip, espip) do { memcpy(&((espip)->u_addr.ip4), (ip), sizeof(ip4_addr_t)); \
(espip)->type = ESP_IPADDR_TYPE_V4; \
} while(0)
#elif LWIP_IPV6
#define ESPIP_TO_IP(espip, ip) memcpy((ip), &((espip)->u_addr.ip6), sizeof(ip_addr_t));
#define IP_TO_ESPIP(ip, espip) do { memcpy(&((espip)->u_addr.ip6), (ip), sizeof(ip6_addr_t)); \
(espip)->type = ESP_IPADDR_TYPE_V6; \
} while(0)
#endif
/**
* @brief If netif protocol not enabled in menuconfig, log the error and return appropriate code indicating failure
*/
#define LOG_NETIF_DISABLED_AND_DO(proto, action) \
do { \
ESP_LOGE(TAG, "%s not supported, please enable it in lwIP component configuration", proto); \
action; \
} while(0)
//
// Internal variables for this module
//
static const char *TAG = "esp_netif_lwip";
#if LWIP_ESP_NETIF_DATA
static u8_t lwip_netif_client_id = 0xff;
#endif
static esp_netif_t *s_last_default_esp_netif = NULL;
static bool s_is_last_default_esp_netif_overridden = false;
static netif_ext_callback_t netif_callback = { .callback_fn = NULL, .next = NULL };
#if LWIP_IPV4
static void esp_netif_internal_dhcpc_cb(struct netif *netif);
#endif
#if LWIP_IPV6
static void esp_netif_internal_nd6_cb(struct netif *p_netif, uint8_t ip_index);
static void netif_set_mldv6_flag(esp_netif_t *netif);
static void netif_unset_mldv6_flag(esp_netif_t *netif);
#endif /* LWIP_IPV6 */
static esp_err_t esp_netif_destroy_api(esp_netif_api_msg_t *msg);
static void netif_callback_fn(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
{
#if LWIP_IPV4
if (reason & DHCP_CB_CHANGE) {
esp_netif_internal_dhcpc_cb(netif);
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
if ((reason & LWIP_NSC_IPV6_ADDR_STATE_CHANGED) && (args != NULL)) {
s8_t addr_idx = args->ipv6_addr_state_changed.addr_index;
if (!(args->ipv6_addr_state_changed.old_state & IP6_ADDR_VALID) &&
netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
/* address is valid -> call the callback function */
esp_netif_internal_nd6_cb(netif, addr_idx);
}
}
#endif /* #if LWIP_IPV6 */
}
#ifdef CONFIG_LWIP_GARP_TMR_INTERVAL
static void netif_send_garp(void *arg)
{
struct netif *netif = arg;
if (!ip4_addr_cmp(netif_ip4_addr(netif), IP4_ADDR_ANY4)) { // Send GARP requests only if we have a valid IP
etharp_gratuitous(netif);
}
sys_timeout(CONFIG_LWIP_GARP_TMR_INTERVAL*1000, netif_send_garp, netif);
}
static void netif_set_garp_flag(struct netif *netif)
{
sys_timeout(CONFIG_LWIP_GARP_TMR_INTERVAL*1000, netif_send_garp, netif);
}
static void netif_unset_garp_flag(struct netif *netif)
{
sys_untimeout(netif_send_garp, netif);
}
#endif // CONFIG_LWIP_GARP_TMR_INTERVAL
#if !LWIP_TCPIP_CORE_LOCKING
static sys_sem_t api_sync_sem = NULL;
static sys_sem_t api_lock_sem = NULL;
#endif
/**
* @brief Api callback from tcpip thread used to call esp-netif
* function in lwip task context
*/
static void esp_netif_api_cb(void *api_msg)
{
esp_netif_api_msg_t *msg = (esp_netif_api_msg_t *)api_msg;
if (!msg || !msg->api_fn) {
ESP_LOGD(TAG, "null msg/api_fn");
return;
}
msg->ret = msg->api_fn(msg);
ESP_LOGD(TAG, "call api in lwip: ret=0x%x, give sem", msg->ret);
#if !LWIP_TCPIP_CORE_LOCKING
sys_sem_signal(&api_sync_sem);
#endif
}
/**
* @brief Initiates a tcpip remote call if called from another task
* or calls the function directly if executed from lwip task
*/
static inline esp_err_t esp_netif_lwip_ipc_call_msg(esp_netif_api_msg_t *msg)
{
if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
ESP_LOGD(TAG, "check: remote, if=%p fn=%p", msg->esp_netif, msg->api_fn);
#if LWIP_TCPIP_CORE_LOCKING
tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, msg, NULL);
#else
sys_arch_sem_wait(&api_lock_sem, 0);
tcpip_send_msg_wait_sem((tcpip_callback_fn)esp_netif_api_cb, msg, &api_sync_sem);
sys_sem_signal(&api_lock_sem);
#endif /* LWIP_TCPIP_CORE_LOCKING */
return msg->ret;
}
ESP_LOGD(TAG, "check: local, if=%p fn=%p", msg->esp_netif, msg->api_fn);
return msg->api_fn(msg);
}
static inline esp_err_t esp_netif_lwip_ipc_call(esp_netif_api_fn fn, esp_netif_t* netif, void *data)
{
esp_netif_api_msg_t msg = {
.esp_netif = netif,
.data = data,
.api_fn = fn
};
return esp_netif_lwip_ipc_call_msg(&msg);
}
static inline esp_err_t esp_netif_lwip_ipc_call_fn(esp_netif_api_fn fn, esp_netif_callback_fn user_fn, void *ctx)
{
esp_netif_api_msg_t msg = {
.user_fn = user_fn,
.data = ctx,
.api_fn = fn
};
return esp_netif_lwip_ipc_call_msg(&msg);
}
static inline esp_err_t esp_netif_lwip_ipc_call_get_netif(esp_netif_api_fn fn, esp_netif_t **netif, void *ctx)
{
esp_netif_api_msg_t msg = {
.p_esp_netif = netif,
.data = ctx,
.api_fn = fn
};
return esp_netif_lwip_ipc_call_msg(&msg);
}
static inline esp_err_t esp_netif_lwip_ipc_no_args(esp_netif_api_fn fn)
{
esp_netif_api_msg_t msg = {
.api_fn = fn
};
return esp_netif_lwip_ipc_call_msg(&msg);
}
/**
* @brief Check if supplied esp_netif handle is active, i.e. available within registered interfaces
* as it might have already been destroyed. Returns the supplied handle if active, nullptr otherwise
*
* @param esp_netif handle to check if available in the list of registered interfaces
* @return esp_netif handle if available, or NULL if it wasn't found
*/
static esp_netif_t* esp_netif_is_active(esp_netif_t *arg)
{
// looking for the netif in the list of registered interfaces
// as it might have already been destroyed
if (esp_netif_is_netif_listed(arg)) {
return arg;
}
return NULL;
}
/**
* @brief This function sets default netif no matter which implementation used
*
* @param esp_netif handle to network interface
*
* @note: This function must be called from lwip thread
*/
static void esp_netif_set_default_netif_internal(esp_netif_t *esp_netif)
{
if (ESP_NETIF_IS_POINT2POINT_TYPE(esp_netif, PPP_LWIP_NETIF)) {
#if CONFIG_PPP_SUPPORT
esp_netif_ppp_set_default_netif(esp_netif->netif_handle);
#endif
} else {
netif_set_default(esp_netif->lwip_netif);
}
#ifdef CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
for (int i = 0; i < DNS_MAX_SERVERS; ++i) {
dns_setserver(i, &esp_netif->dns[i]);
}
#endif
}
/**
* @brief tcpip thread version of esp_netif_update_default_netif
*
* @note This function and all functions called from this must be called from lwip task context
*/
static esp_err_t esp_netif_update_default_netif_lwip(esp_netif_api_msg_t *msg)
{
esp_netif_t *esp_netif = msg->esp_netif;
esp_netif_route_prio_action_t action = (esp_netif_route_prio_action_t)msg->data;
ESP_LOGD(TAG, "%s %p", __func__, esp_netif);
if (s_is_last_default_esp_netif_overridden && action != ESP_NETIF_SET_DEFAULT) {
// check if manually configured default interface hasn't been destroyed
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
if (s_last_default_esp_netif != NULL) {
return ESP_OK; // still valid -> don't update default netif
}
// invalid -> reset the manual override and perform auto update
s_is_last_default_esp_netif_overridden = false;
}
switch (action) {
case ESP_NETIF_SET_DEFAULT:
s_last_default_esp_netif = esp_netif;
s_is_last_default_esp_netif_overridden = true;
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
break;
case ESP_NETIF_STARTED:
case ESP_NETIF_GOT_IP:
{
// check if previously default interface hasn't been destroyed in the meantime
s_last_default_esp_netif = esp_netif_is_active(s_last_default_esp_netif);
if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)
&& (s_last_default_esp_netif->route_prio > esp_netif->route_prio)) {
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
} else if (esp_netif_is_netif_up(esp_netif)) {
s_last_default_esp_netif = esp_netif;
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
}
}
break;
default:
case ESP_NETIF_STOPPED:
case ESP_NETIF_LOST_IP:
{
s_last_default_esp_netif = NULL;
esp_netif_t *netif = esp_netif_next_unsafe(NULL);
while (netif) {
if (esp_netif_is_netif_up(netif)) {
if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) {
if (netif->route_prio > s_last_default_esp_netif->route_prio) {
s_last_default_esp_netif = netif;
} // else not needed, as the s_last_default_esp_netif is correct
} else {
// s_last_default is either not set or down, current netif is up
s_last_default_esp_netif = netif;
}
}
netif = esp_netif_next_unsafe(netif);
}
if (s_last_default_esp_netif && esp_netif_is_netif_up(s_last_default_esp_netif)) {
esp_netif_set_default_netif_internal(s_last_default_esp_netif);
}
}
break;
}
return ESP_OK;
}
/**
* @brief This function sets default routing netif based on priorities of all interfaces which are up
*
* @param esp_netif current interface which just updated state
* @param action updating action (on-off)
*/
esp_err_t esp_netif_update_default_netif(esp_netif_t *esp_netif, esp_netif_route_prio_action_t action)
{
return esp_netif_lwip_ipc_call(esp_netif_update_default_netif_lwip, esp_netif, (void*)action);
}
esp_err_t esp_netif_set_default_netif(esp_netif_t *esp_netif)
{
return esp_netif_update_default_netif(esp_netif, ESP_NETIF_SET_DEFAULT);
}
esp_netif_t *esp_netif_get_default_netif(void)
{
return s_last_default_esp_netif;
}
static inline esp_netif_t* lwip_get_esp_netif(struct netif *netif)
{
#if LWIP_ESP_NETIF_DATA
return (esp_netif_t*)netif_get_client_data(netif, lwip_netif_client_id);
#else
return (esp_netif_t*)netif->state;
#endif
}
static inline void lwip_set_esp_netif(struct netif *netif, esp_netif_t* esp_netif)
{
#if LWIP_ESP_NETIF_DATA
netif_set_client_data(netif, lwip_netif_client_id, esp_netif);
#else
netif->state = esp_netif;
#endif
}
#if CONFIG_ESP_NETIF_BRIDGE_EN
esp_err_t esp_netif_bridge_add_port(esp_netif_t *esp_netif_br, esp_netif_t *esp_netif_port)
{
if (ERR_OK != bridgeif_add_port(esp_netif_br->lwip_netif, esp_netif_port->lwip_netif)) {
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t esp_netif_bridge_fdb_add(esp_netif_t *esp_netif_br, uint8_t *addr, uint64_t ports_mask)
{
bridgeif_portmask_t ports = (bridgeif_portmask_t)ports_mask;
if (ports_mask & ESP_NETIF_BR_FDW_CPU) {
ports |= 1 << BRIDGEIF_MAX_PORTS;
}
if (ERR_OK != bridgeif_fdb_add(esp_netif_br->lwip_netif, (const struct eth_addr *)addr, ports)) {
return ESP_FAIL;
}
return ESP_OK;
}
esp_err_t esp_netif_bridge_fdb_remove(esp_netif_t *esp_netif_br, uint8_t *addr)
{
if (ERR_OK != bridgeif_fdb_remove(esp_netif_br->lwip_netif, (const struct eth_addr *)addr)) {
return ESP_FAIL;
}
return ESP_OK;
}
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
#if CONFIG_LWIP_IPV4
void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d)
{
ip4_addr_t *address = (ip4_addr_t*)addr;
IP4_ADDR(address, a, b, c, d);
}
char * esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen)
{
return ip4addr_ntoa_r((ip4_addr_t *)addr, buf, buflen);
}
uint32_t esp_ip4addr_aton(const char *addr)
{
return ipaddr_addr(addr);
}
#endif
esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst)
{
if (src == NULL || dst == NULL) {
return ESP_ERR_INVALID_ARG;
}
int err = inet_pton(AF_INET, src, dst);
return err == 1 ? ESP_OK : ESP_FAIL;
}
esp_err_t esp_netif_str_to_ip6(const char *src, esp_ip6_addr_t *dst)
{
if (src == NULL || dst == NULL) {
return ESP_ERR_INVALID_ARG;
}
int err = inet_pton(AF_INET6, src, dst);
return err == 1 ? ESP_OK : ESP_FAIL;
}
esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif)
{
return esp_netif->driver_handle;
}
esp_netif_t* esp_netif_get_handle_from_netif_impl(void *dev)
{
// ppp_pcb ptr would never get to app code, so this function only works with vanilla lwip impl
struct netif *lwip_netif = dev;
return lwip_get_esp_netif(lwip_netif);
}
void* esp_netif_get_netif_impl(esp_netif_t *esp_netif)
{
if (esp_netif) {
return esp_netif->lwip_netif;
}
return NULL;
}
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
static void store_dnsserver_info(struct netif* netif, u8_t numdns, const ip_addr_t *dnsserver)
{
if (netif == NULL) {
return;
}
esp_netif_t *esp_netif = lwip_get_esp_netif(netif);
if (esp_netif == NULL || !esp_netif_is_netif_listed(esp_netif)) {
return;
}
if (!ip_addr_isany(dnsserver)) {
ip_addr_copy(esp_netif->dns[numdns], *dnsserver);
} else {
ip_addr_copy(esp_netif->dns[numdns], *IP_ADDR_ANY);
}
}
#endif
static void tcpip_init_done(void *arg)
{
sys_sem_t *init_sem = arg;
#if LWIP_ESP_NETIF_DATA
if (lwip_netif_client_id == 0xFF) {
lwip_netif_client_id = netif_alloc_client_data_id();
}
#endif
sys_sem_signal(init_sem);
}
esp_err_t esp_netif_init(void)
{
if (!sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED)) {
#if CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT
uint8_t rand_buf[16];
/*
* This is early startup code where WiFi/BT is yet to be enabled and hence
* relevant entropy source is not available. However, bootloader enables
* SAR ADC based entropy source at its initialization, and our requirement
* of random bytes is pretty small (16), so we can assume that following
* API will provide sufficiently random data.
*/
esp_fill_random(rand_buf, sizeof(rand_buf));
lwip_init_tcp_isn(esp_log_timestamp(), rand_buf);
#endif
sys_sem_t init_sem;
if (sys_sem_new(&init_sem, 0) != ERR_OK) {
ESP_LOGE(TAG, "esp netif cannot create tcpip_init semaphore");
return ESP_FAIL;
}
#if LWIP_TCPIP_CORE_LOCKING
/* TCPIP thread is not initialized yet,
* pretend that the calling thread is holder
* to correctly set up the TCPIP task */
sys_thread_tcpip(LWIP_CORE_LOCK_MARK_HOLDER);
#endif
tcpip_init(tcpip_init_done, &init_sem);
sys_sem_wait(&init_sem);
sys_sem_free(&init_sem);
ESP_LOGD(TAG, "LwIP stack has been initialized");
#if CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF
if (dns_setserver_callback(store_dnsserver_info) != ERR_OK) {
ESP_LOGE(TAG, "Feiled to configure DNS set server callback");
return ESP_FAIL;
}
#endif
}
#if !LWIP_TCPIP_CORE_LOCKING
if (!api_sync_sem) {
if (ERR_OK != sys_sem_new(&api_sync_sem, 0)) {
ESP_LOGE(TAG, "esp netif api sync sem init fail");
return ESP_FAIL;
}
}
if (!api_lock_sem) {
if (ERR_OK != sys_sem_new(&api_lock_sem, 1)) {
ESP_LOGE(TAG, "esp netif api lock sem init fail");
return ESP_FAIL;
}
}
#endif
ESP_LOGD(TAG, "esp-netif has been successfully initialized");
return ESP_OK;
}
esp_err_t esp_netif_deinit(void)
{
if (sys_thread_tcpip(LWIP_CORE_IS_TCPIP_INITIALIZED)) {
/* deinit of LwIP not supported:
* do not deinit semaphores and states,
* so init could be called multiple times
*
sys_sem_free(&api_sync_sem);
sys_sem_free(&api_lock_sem);
*/
return ESP_ERR_NOT_SUPPORTED;
}
return ESP_ERR_INVALID_STATE;
}
static esp_err_t esp_netif_init_configuration(esp_netif_t *esp_netif, const esp_netif_config_t *cfg)
{
// Basic esp_netif and lwip is a mandatory configuration and cannot be updated after esp_netif_new()
if (cfg == NULL || cfg->base == NULL || cfg->stack == NULL) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
// Configure general esp-netif properties
memcpy(esp_netif->mac, cfg->base->mac, NETIF_MAX_HWADDR_LEN);
#if CONFIG_LWIP_IPV4
if (cfg->base->ip_info == NULL) {
ip4_addr_set_zero(&esp_netif->ip_info->ip);
ip4_addr_set_zero(&esp_netif->ip_info->gw);
ip4_addr_set_zero(&esp_netif->ip_info->netmask);
} else {
memcpy(esp_netif->ip_info, cfg->base->ip_info, sizeof(esp_netif_ip_info_t));
}
memcpy(esp_netif->ip_info_old, esp_netif->ip_info, sizeof(esp_netif_ip_info_t));
#endif
// Setup main config parameters
esp_netif->lost_ip_event = cfg->base->lost_ip_event;
esp_netif->get_ip_event = cfg->base->get_ip_event;
esp_netif->flags = cfg->base->flags;
if (cfg->base->if_key) {
esp_netif->if_key = strdup(cfg->base->if_key);
}
if (cfg->base->if_desc) {
esp_netif->if_desc = strdup(cfg->base->if_desc);
}
if (cfg->base->route_prio) {
esp_netif->route_prio = cfg->base->route_prio;
}
#if CONFIG_ESP_NETIF_BRIDGE_EN
// Setup bridge configuration if the interface is to be bridge
if (cfg->base->flags & ESP_NETIF_FLAG_IS_BRIDGE) {
if (cfg->base->bridge_info != NULL) {
esp_netif->max_fdb_dyn_entries = cfg->base->bridge_info->max_fdb_dyn_entries;
esp_netif->max_fdb_sta_entries = cfg->base->bridge_info->max_fdb_sta_entries;
esp_netif->max_ports = cfg->base->bridge_info->max_ports;
} else {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
}
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
// Install network stack functions -- connects netif and L3 stack
const esp_netif_netstack_config_t *esp_netif_stack_config = cfg->stack;
if (cfg->base->flags & ESP_NETIF_FLAG_IS_PPP) {
#if CONFIG_PPP_SUPPORT
esp_netif->related_data = esp_netif_new_ppp(esp_netif, esp_netif_stack_config);
if (esp_netif->related_data == NULL) {
return ESP_ERR_ESP_NETIF_INIT_FAILED;
}
esp_netif->lwip_input_fn = esp_netif_stack_config->lwip_ppp.input_fn;
// Make the netif handle (used for tcpip input function) the ppp_netif
esp_netif->netif_handle = esp_netif->related_data;
#else
LOG_NETIF_DISABLED_AND_DO("PPP", return ESP_ERR_NOT_SUPPORTED);
#endif
} else {
if (esp_netif_stack_config-> lwip.init_fn) {
esp_netif->lwip_init_fn = esp_netif_stack_config->lwip.init_fn;
}
if (esp_netif_stack_config->lwip.input_fn) {
esp_netif->lwip_input_fn = esp_netif_stack_config->lwip.input_fn;
}
// Make the netif handle (used for tcpip input function) the lwip_netif itself
esp_netif->netif_handle = esp_netif->lwip_netif;
}
// Install IO functions only if provided -- connects driver and netif
// this configuration could be updated after esp_netif_new(), typically in post_attach callback
if (cfg->driver) {
const esp_netif_driver_ifconfig_t *esp_netif_driver_config = cfg->driver;
if (esp_netif_driver_config->handle) {
esp_netif->driver_handle = esp_netif_driver_config->handle;
}
if (esp_netif_driver_config->transmit) {
esp_netif->driver_transmit = esp_netif_driver_config->transmit;
}
if (esp_netif_driver_config->transmit_wrap) {
esp_netif->driver_transmit_wrap = esp_netif_driver_config->transmit_wrap;
}
if (esp_netif_driver_config->driver_free_rx_buffer) {
esp_netif->driver_free_rx_buffer = esp_netif_driver_config->driver_free_rx_buffer;
}
}
return ESP_OK;
}
static esp_err_t tcpip_exec_api(esp_netif_api_msg_t *msg)
{
return msg->user_fn(msg->data);
}
esp_err_t esp_netif_tcpip_exec(esp_netif_callback_fn fn, void*ctx)
{
return esp_netif_lwip_ipc_call_fn(tcpip_exec_api, fn, ctx);
}
static esp_err_t esp_netif_new_api(esp_netif_api_msg_t *msg)
{
const esp_netif_config_t *esp_netif_config = msg->data;
// mandatory configuration must be provided when creating esp_netif object
if (esp_netif_config == NULL ||
esp_netif_config->base->if_key == NULL ||
NULL != esp_netif_get_handle_from_ifkey_unsafe(esp_netif_config->base->if_key)) {
ESP_LOGE(TAG, "%s: Failed to configure netif with config=%p (config or if_key is NULL or duplicate key)",
__func__, esp_netif_config);
return ESP_FAIL;
}
#if ESP_DHCPS
// DHCP server and client cannot be configured together
if((esp_netif_config->base->flags & ESP_NETIF_DHCP_SERVER) &&
(esp_netif_config->base->flags & ESP_NETIF_DHCP_CLIENT)) {
ESP_LOGE(TAG, "%s: Failed to configure netif with config=%p (DHCP server and client cannot be configured together)",
__func__, esp_netif_config);
return ESP_FAIL;
}
#endif
// Create parent esp-netif object
esp_netif_t *esp_netif = calloc(1, sizeof(struct esp_netif_obj));
if (!esp_netif) {
ESP_LOGE(TAG, "Failed to allocate %" PRIu32 " bytes (free heap size %" PRIu32 ")", (uint32_t)sizeof(struct esp_netif_obj),
esp_get_free_heap_size());
return ESP_FAIL;
}
// Create ip info
esp_netif_ip_info_t *ip_info = calloc(1, sizeof(esp_netif_ip_info_t));
if (!ip_info) {
ESP_LOGE(TAG, "Failed to allocate %" PRIu32 " bytes (free heap size %" PRIu32 ")", (uint32_t)sizeof(esp_netif_ip_info_t),
esp_get_free_heap_size());
free(esp_netif);
return ESP_FAIL;
}
esp_netif->ip_info = ip_info;
// creating another ip info (to store old ip)
ip_info = calloc(1, sizeof(esp_netif_ip_info_t));
if (!ip_info) {
ESP_LOGE(TAG, "Failed to allocate %" PRIu32 " bytes (free heap size %" PRIu32 ")", (uint32_t)sizeof(esp_netif_ip_info_t),
esp_get_free_heap_size());
free(esp_netif->ip_info);
free(esp_netif);
return ESP_FAIL;
}
esp_netif->ip_info_old = ip_info;
// Create underlying lwip netif
struct netif * lwip_netif = calloc(1, sizeof(struct netif));
if (!lwip_netif) {
ESP_LOGE(TAG, "Failed to allocate %" PRIu32 " bytes (free heap size %" PRIu32 ")", (uint32_t)sizeof(struct netif),
esp_get_free_heap_size());
free(esp_netif->ip_info_old);
free(esp_netif->ip_info);
free(esp_netif);
return ESP_FAIL;
}
esp_netif->lwip_netif = lwip_netif;
esp_netif_add_to_list_unsafe(esp_netif);
#if ESP_DHCPS
// Create DHCP server structure
if (esp_netif_config->base->flags & ESP_NETIF_DHCP_SERVER) {
esp_netif->dhcps = dhcps_new();
if (esp_netif->dhcps == NULL) {
ESP_LOGE(TAG, "Failed to create dhcp server handle");
esp_netif_api_msg_t to_destroy = { .esp_netif = esp_netif};
esp_netif_destroy_api(&to_destroy);
return ESP_FAIL;
}
}
#endif
// Configure the created object with provided configuration
esp_err_t ret = esp_netif_init_configuration(esp_netif, esp_netif_config);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Initial configuration of esp_netif failed with %d", ret);
esp_netif_api_msg_t to_destroy = { .esp_netif = esp_netif};
esp_netif_destroy_api(&to_destroy);
return ESP_FAIL;
}
lwip_set_esp_netif(lwip_netif, esp_netif);
if (netif_callback.callback_fn == NULL ) {
netif_add_ext_callback(&netif_callback, netif_callback_fn);
}
*msg->p_esp_netif = esp_netif;
return ESP_OK;
}
esp_netif_t *esp_netif_new(const esp_netif_config_t *config)
{
esp_netif_t *netif = NULL;
esp_netif_lwip_ipc_call_get_netif(esp_netif_new_api, &netif, (void *)config);
return netif;
}
static esp_err_t get_handle_from_ifkey_api(esp_netif_api_msg_t *msg)
{
*msg->p_esp_netif = esp_netif_get_handle_from_ifkey_unsafe(msg->data);
return ESP_OK;
}
esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key)
{
esp_netif_t *netif = NULL;
esp_netif_lwip_ipc_call_get_netif(get_handle_from_ifkey_api, &netif, (void*)if_key);
return netif;
}
typedef struct find_if_api {
esp_netif_find_predicate_t fn;
void *ctx;
} find_if_api_t;
static esp_err_t esp_netif_find_if_api(esp_netif_api_msg_t *msg)
{
find_if_api_t *find_if_api = msg->data;
esp_netif_t *esp_netif = NULL;
while ((esp_netif = esp_netif_next_unsafe(esp_netif)) != NULL) {
if (find_if_api->fn(esp_netif, find_if_api->ctx)) {
*msg->p_esp_netif = esp_netif;
return ESP_OK;
}
}
return ESP_FAIL;
}
esp_netif_t *esp_netif_find_if(esp_netif_find_predicate_t fn, void *ctx)
{
esp_netif_t *netif = NULL;
find_if_api_t find_if_api = { .fn = fn, .ctx = ctx };
if (esp_netif_lwip_ipc_call_get_netif(esp_netif_find_if_api, &netif, &find_if_api) == ESP_OK) {
return netif;
}
return NULL;
}
static void esp_netif_lwip_remove(esp_netif_t *esp_netif)
{
if (esp_netif->lwip_netif) {
if (netif_is_up(esp_netif->lwip_netif)) {
netif_set_down(esp_netif->lwip_netif);
}
netif_remove(esp_netif->lwip_netif);
#if ESP_GRATUITOUS_ARP
if (esp_netif->flags & ESP_NETIF_FLAG_GARP) {
netif_unset_garp_flag(esp_netif->lwip_netif);
}
#endif
#if ESP_MLDV6_REPORT && LWIP_IPV6
if (esp_netif->flags & ESP_NETIF_FLAG_MLDV6_REPORT) {
netif_unset_mldv6_flag(esp_netif);
}
#endif
if (esp_netif->flags & ESP_NETIF_DHCP_CLIENT) {
#if CONFIG_LWIP_IPV4
dhcp_cleanup(esp_netif->lwip_netif);
#endif
}
}
}
static esp_err_t esp_netif_lwip_add(esp_netif_t *esp_netif)
{
ESP_COMPILER_DIAGNOSTIC_PUSH_IGNORE("-Wanalyzer-malloc-leak"); // False-positive detection. TODO GCC-366
if (esp_netif->lwip_netif == NULL) {
esp_netif->lwip_netif = calloc(1, sizeof(struct netif));
if (esp_netif->lwip_netif == NULL) {
return ESP_ERR_NO_MEM;
}
}
ESP_COMPILER_DIAGNOSTIC_POP("-Wanalyzer-malloc-leak");
if (esp_netif->flags & ESP_NETIF_FLAG_IS_PPP) {
#if CONFIG_PPP_SUPPORT
err_t err = esp_netif->lwip_init_fn(NULL);
if (err != ERR_OK) {
ESP_LOGE(TAG, "Init netif failed with %" PRId8 "", err);
return ESP_ERR_ESP_NETIF_INIT_FAILED;
}
#else
LOG_NETIF_DISABLED_AND_DO("PPP", return ESP_ERR_NOT_SUPPORTED);
#endif
}
#if CONFIG_ESP_NETIF_BRIDGE_EN
if (esp_netif->flags & ESP_NETIF_FLAG_IS_BRIDGE) {
bridgeif_initdata_t bridge_initdata = {
.max_fdb_dynamic_entries = esp_netif->max_fdb_dyn_entries,
.max_fdb_static_entries = esp_netif->max_fdb_sta_entries,
.max_ports = esp_netif->max_ports
};
memcpy(&bridge_initdata.ethaddr, esp_netif->mac, ETH_HWADDR_LEN);
if (NULL == netif_add(esp_netif->lwip_netif, (struct ip4_addr*)&esp_netif->ip_info->ip,
(struct ip4_addr*)&esp_netif->ip_info->netmask, (struct ip4_addr*)&esp_netif->ip_info->gw,
&bridge_initdata, esp_netif->lwip_init_fn, tcpip_input)) {
esp_netif_lwip_remove(esp_netif);
return ESP_ERR_ESP_NETIF_IF_NOT_READY;
}
} else {
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
if (NULL == netif_add(esp_netif->lwip_netif,
#if CONFIG_LWIP_IPV4
(struct ip4_addr*)&esp_netif->ip_info->ip,
(struct ip4_addr*)&esp_netif->ip_info->netmask,
(struct ip4_addr*)&esp_netif->ip_info->gw,
#endif
esp_netif, esp_netif->lwip_init_fn, tcpip_input)) {
esp_netif_lwip_remove(esp_netif);
return ESP_ERR_ESP_NETIF_IF_NOT_READY;
}
#if CONFIG_ESP_NETIF_BRIDGE_EN
}
#endif // CONFIG_ESP_NETIF_BRIDGE_EN
lwip_set_esp_netif(esp_netif->lwip_netif, esp_netif);
return ESP_OK;
}
static void esp_netif_destroy_related(esp_netif_t *esp_netif)
{
if (ESP_NETIF_IS_POINT2POINT_TYPE(esp_netif, PPP_LWIP_NETIF)) {
#if CONFIG_PPP_SUPPORT
esp_netif_destroy_ppp(esp_netif->related_data);
#endif
}
}
static esp_err_t esp_netif_destroy_api(esp_netif_api_msg_t *msg)
{
esp_netif_t *esp_netif = msg->esp_netif;
esp_netif_remove_from_list_unsafe(esp_netif);
if (esp_netif_get_nr_of_ifs() == 0) {
netif_remove_ext_callback(&netif_callback);
netif_callback.callback_fn = NULL;
}
free(esp_netif->ip_info);
free(esp_netif->ip_info_old);
free(esp_netif->if_key);
free(esp_netif->if_desc);
esp_netif_lwip_remove(esp_netif);
esp_netif_destroy_related(esp_netif);
free(esp_netif->lwip_netif);
free(esp_netif->hostname);
esp_netif_update_default_netif(esp_netif, ESP_NETIF_STOPPED);
#if ESP_DHCPS
dhcps_delete(esp_netif->dhcps);
#endif
free(esp_netif);
return ESP_OK;
}
void esp_netif_destroy(esp_netif_t *esp_netif)
{
if (esp_netif == NULL) {
return;
}
esp_netif_lwip_ipc_call(esp_netif_destroy_api, esp_netif, NULL);
}
esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle)
{
esp_netif_driver_base_t *base_driver = driver_handle;
esp_netif->driver_handle = driver_handle;
if (base_driver->post_attach) {
esp_err_t ret = base_driver->post_attach(esp_netif, driver_handle);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Post-attach callback of driver(%p) failed with %d", driver_handle, ret);
return ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED;
}
}
return ESP_OK;
}
esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif,
const esp_netif_driver_ifconfig_t *driver_config)
{
if (esp_netif == NULL || driver_config == NULL) {
return ESP_ERR_ESP_NETIF_INVALID_PARAMS;
}
esp_netif->driver_handle = driver_config->handle;