1
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
66
67
68
69
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
94
95
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
113
114
115
116
117
118
119
120
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
159
160
164
165
166
167
168
169
170
171
172
173
179
180
181
182
187
188
189
190
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
291
292
293
294
295
296
297
298
299
300
301
302
303
304
333
334
335
336
337
338
344
345
346
347
348
349
350
351
352
353
354
355
359
360
361
362
363
364
365
366
367
381
382
383
384
385
386
387
388
389
390
391
392
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
437
438
439
440
443
444
445
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
542
543
547
548
549
550
551
552
553
558
559
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
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
674
675
676
678
679
680
681
682
688
689
694
695
696
697
698
699
700
701
704
705
706
707
708
709
710
711
712
713
714
715
723
724
735
739
740
741
742
743
744
747
748
749
750
751
752
755
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
804
805
806
811
812
813
814
815
816
817
818
819
820
821
822
823
825
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
877
878
879
880
881
882
883
884
885
886
887
888
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
948
949
950
951
952
953
961
962
963
964
965
971
972
973
974
975
976
977
978
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1007
1008
1016
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1044
1045
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1066
1067
1075
1076
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1151
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1203
1204
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_IPV4 && LWIP_ARP
#include "lwip/etharp.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/prot/iana.h"
#include "netif/ethernet.h"
#include <string.h>
8 includes
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
/* ... */
#define ARP_AGE_REREQUEST_USED_UNICAST (ARP_MAXAGE - 30)
#define ARP_AGE_REREQUEST_USED_BROADCAST (ARP_MAXAGE - 15)
/* ... */
#define ARP_MAXPENDING 5
enum etharp_state {
ETHARP_STATE_EMPTY = 0,
ETHARP_STATE_PENDING,
ETHARP_STATE_STABLE,
ETHARP_STATE_STABLE_REREQUESTING_1,
ETHARP_STATE_STABLE_REREQUESTING_2
#if ETHARP_SUPPORT_STATIC_ENTRIES
, ETHARP_STATE_STATIC
#endif
...};
struct etharp_entry {
#if ARP_QUEUEING
struct etharp_q_entry *q;/* ... */
#else
struct pbuf *q;/* ... */
#endif
ip4_addr_t ipaddr;
struct netif *netif;
struct eth_addr ethaddr;
u16_t ctime;
u8_t state;
...};
static struct etharp_entry arp_table[ARP_TABLE_SIZE];
#if !LWIP_NETIF_HWADDRHINT
static netif_addr_idx_t etharp_cached_entry;
#endif
/* ... */
#define ETHARP_FLAG_TRY_HARD 1
#define ETHARP_FLAG_FIND_ONLY 2
#if ETHARP_SUPPORT_STATIC_ENTRIES
#define ETHARP_FLAG_STATIC_ENTRY 4
#endif
#if LWIP_NETIF_HWADDRHINT
#define ETHARP_SET_ADDRHINT(netif, addrhint) do { if (((netif) != NULL) && ((netif)->hints != NULL)) { \
(netif)->hints->addr_hint = (addrhint); }if (((netif) != NULL) && ((netif)->hints != NULL)) { ... }}... while(0)...
/* ... */#else
#define ETHARP_SET_ADDRHINT(netif, addrhint) (etharp_cached_entry = (addrhint))
#endif
#if (ARP_TABLE_SIZE > NETIF_ADDR_IDX_MAX)
#error "ARP_TABLE_SIZE must fit in an s16_t, you have to reduce it in your lwipopts.h"
#endif
static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr);
static err_t etharp_raw(struct netif *netif,
const struct eth_addr *ethsrc_addr, const struct eth_addr *ethdst_addr,
const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
const u16_t opcode);
#if ARP_QUEUEING
/* ... */
static void
free_etharp_q(struct etharp_q_entry *q)
{
struct etharp_q_entry *r;
LWIP_ASSERT("q != NULL", q != NULL);
while (q) {
r = q;
q = q->next;
LWIP_ASSERT("r->p != NULL", (r->p != NULL));
pbuf_free(r->p);
memp_free(MEMP_ARP_QUEUE, r);
}while (q) { ... }
}free_etharp_q (struct etharp_q_entry *q) { ... }
/* ... */#else
#define free_etharp_q(q) pbuf_free(q)
/* ... */
#endif
static void
etharp_free_entry(int i)
{
mib2_remove_arp_entry(arp_table[i].netif, &arp_table[i].ipaddr);
if (arp_table[i].q != NULL) {
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_free_entry: freeing entry %"U16_F", packet queue %p.\n", (u16_t)i, (void *)(arp_table[i].q)));
free_etharp_q(arp_table[i].q);
arp_table[i].q = NULL;
}if (arp_table[i].q != NULL) { ... }
arp_table[i].state = ETHARP_STATE_EMPTY;
#ifdef LWIP_DEBUG
arp_table[i].ctime = 0;
arp_table[i].netif = NULL;
ip4_addr_set_zero(&arp_table[i].ipaddr);
arp_table[i].ethaddr = ethzero;/* ... */
#endif
}{ ... }
/* ... */
void
etharp_tmr(void)
{
int i;
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer\n"));
for (i = 0; i < ARP_TABLE_SIZE; ++i) {
u8_t state = arp_table[i].state;
if (state != ETHARP_STATE_EMPTY
#if ETHARP_SUPPORT_STATIC_ENTRIES
&& (state != ETHARP_STATE_STATIC)
#endif
) {
arp_table[i].ctime++;
if ((arp_table[i].ctime >= ARP_MAXAGE) ||
((arp_table[i].state == ETHARP_STATE_PENDING) &&
(arp_table[i].ctime >= ARP_MAXPENDING))) {
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_timer: expired %s entry %d.\n",
arp_table[i].state >= ETHARP_STATE_STABLE ? "stable" : "pending", i));
etharp_free_entry(i);
}if ((arp_table[i].ctime >= ARP_MAXAGE) || ((arp_table[i].state == ETHARP_STATE_PENDING) && (arp_table[i].ctime >= ARP_MAXPENDING))) { ... } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) {
arp_table[i].state = ETHARP_STATE_STABLE_REREQUESTING_2;
}else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_1) { ... } else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) {
/* ... */
arp_table[i].state = ETHARP_STATE_STABLE;
}else if (arp_table[i].state == ETHARP_STATE_STABLE_REREQUESTING_2) { ... } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
etharp_request(arp_table[i].netif, &arp_table[i].ipaddr);
}else if (arp_table[i].state == ETHARP_STATE_PENDING) { ... }
}if (state != ETHARP_STATE_EMPTY #if ETHARP_SUPPORT_STATIC_ENTRIES && (state != ETHARP_STATE_STATIC) #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */) { ... }
}for (i = 0; i < ARP_TABLE_SIZE; ++i) { ... }
}{ ... }
/* ... */
static s16_t
etharp_find_entry(const ip4_addr_t *ipaddr, u8_t flags, struct netif *netif)
{
s16_t old_pending = ARP_TABLE_SIZE, old_stable = ARP_TABLE_SIZE;
s16_t empty = ARP_TABLE_SIZE;
s16_t i = 0;
s16_t old_queue = ARP_TABLE_SIZE;
u16_t age_queue = 0, age_pending = 0, age_stable = 0;
LWIP_UNUSED_ARG(netif);
/* ... */
/* ... */
for (i = 0; i < ARP_TABLE_SIZE; ++i) {
u8_t state = arp_table[i].state;
if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) {
LWIP_DEBUGF(ETHARP_DEBUG, ("etharp_find_entry: found empty entry %d\n", (int)i));
empty = i;
}if ((empty == ARP_TABLE_SIZE) && (state == ETHARP_STATE_EMPTY)) { ... } else if (state != ETHARP_STATE_EMPTY) {
LWIP_ASSERT("state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE",
state == ETHARP_STATE_PENDING || state >= ETHARP_STATE_STABLE);
if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr)
#if ETHARP_TABLE_MATCH_NETIF
&& ((netif == NULL) || (netif == arp_table[i].netif))
#endif
) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: found matching entry %d\n", (int)i));
return i;
}if (ipaddr && ip4_addr_cmp(ipaddr, &arp_table[i].ipaddr) #if ETHARP_TABLE_MATCH_NETIF && ((netif == NULL) || (netif == arp_table[i].netif)) #endif /* ETHARP_TABLE_MATCH_NETIF */) { ... }
if (state == ETHARP_STATE_PENDING) {
if (arp_table[i].q != NULL) {
if (arp_table[i].ctime >= age_queue) {
old_queue = i;
age_queue = arp_table[i].ctime;
}if (arp_table[i].ctime >= age_queue) { ... }
}if (arp_table[i].q != NULL) { ... } else
{
if (arp_table[i].ctime >= age_pending) {
old_pending = i;
age_pending = arp_table[i].ctime;
}if (arp_table[i].ctime >= age_pending) { ... }
}else { ... }
}if (state == ETHARP_STATE_PENDING) { ... } else if (state >= ETHARP_STATE_STABLE) {
#if ETHARP_SUPPORT_STATIC_ENTRIES
if (state < ETHARP_STATE_STATIC)/* ... */
#endif
{
if (arp_table[i].ctime >= age_stable) {
old_stable = i;
age_stable = arp_table[i].ctime;
}if (arp_table[i].ctime >= age_stable) { ... }
...}
}else if (state >= ETHARP_STATE_STABLE) { ... }
}else if (state != ETHARP_STATE_EMPTY) { ... }
}for (i = 0; i < ARP_TABLE_SIZE; ++i) { ... }
if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) ||
((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty entry found and not allowed to recycle\n"));
return (s16_t)ERR_MEM;
}if (((flags & ETHARP_FLAG_FIND_ONLY) != 0) || /* or no empty entry found and not allowed to recycle? */ ((empty == ARP_TABLE_SIZE) && ((flags & ETHARP_FLAG_TRY_HARD) == 0))) { ... }
/* ... */
if (empty < ARP_TABLE_SIZE) {
i = empty;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting empty entry %d\n", (int)i));
}if (empty < ARP_TABLE_SIZE) { ... } else {
if (old_stable < ARP_TABLE_SIZE) {
i = old_stable;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest stable entry %d\n", (int)i));
LWIP_ASSERT("arp_table[i].q == NULL", arp_table[i].q == NULL);
}if (old_stable < ARP_TABLE_SIZE) { ... } else if (old_pending < ARP_TABLE_SIZE) {
i = old_pending;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d (without queue)\n", (int)i));
}else if (old_pending < ARP_TABLE_SIZE) { ... } else if (old_queue < ARP_TABLE_SIZE) {
i = old_queue;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: selecting oldest pending entry %d, freeing packet queue %p\n", (int)i, (void *)(arp_table[i].q)));
}else if (old_queue < ARP_TABLE_SIZE) { ... } else {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_find_entry: no empty or recyclable entries found\n"));
return (s16_t)ERR_MEM;
}else { ... }
LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
etharp_free_entry(i);
}else { ... }
LWIP_ASSERT("i < ARP_TABLE_SIZE", i < ARP_TABLE_SIZE);
LWIP_ASSERT("arp_table[i].state == ETHARP_STATE_EMPTY",
arp_table[i].state == ETHARP_STATE_EMPTY);
if (ipaddr != NULL) {
ip4_addr_copy(arp_table[i].ipaddr, *ipaddr);
}if (ipaddr != NULL) { ... }
arp_table[i].ctime = 0;
#if ETHARP_TABLE_MATCH_NETIF
arp_table[i].netif = netif;
#endif
return (s16_t)i;
}{ ... }
/* ... */
static err_t
etharp_update_arp_entry(struct netif *netif, const ip4_addr_t *ipaddr, struct eth_addr *ethaddr, u8_t flags)
{
s16_t i;
LWIP_ASSERT("netif->hwaddr_len == ETH_HWADDR_LEN", netif->hwaddr_len == ETH_HWADDR_LEN);
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
(u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
(u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
if (ip4_addr_isany(ipaddr) ||
ip4_addr_isbroadcast(ipaddr, netif) ||
ip4_addr_ismulticast(ipaddr)) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: will not add non-unicast IP address to ARP cache\n"));
return ERR_ARG;
}if (ip4_addr_isany(ipaddr) || ip4_addr_isbroadcast(ipaddr, netif) || ip4_addr_ismulticast(ipaddr)) { ... }
i = etharp_find_entry(ipaddr, flags, netif);
if (i < 0) {
return (err_t)i;
}if (i < 0) { ... }
#if ETHARP_SUPPORT_STATIC_ENTRIES
if (flags & ETHARP_FLAG_STATIC_ENTRY) {
arp_table[i].state = ETHARP_STATE_STATIC;
}if (flags & ETHARP_FLAG_STATIC_ENTRY) { ... } else if (arp_table[i].state == ETHARP_STATE_STATIC) {
return ERR_VAL;
}else if (arp_table[i].state == ETHARP_STATE_STATIC) { ... } else
#endif
{
arp_table[i].state = ETHARP_STATE_STABLE;
}else { ... }
arp_table[i].netif = netif;
mib2_add_arp_entry(netif, &arp_table[i].ipaddr);
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_update_arp_entry: updating stable entry %"S16_F"\n", i));
SMEMCPY(&arp_table[i].ethaddr, ethaddr, ETH_HWADDR_LEN);
arp_table[i].ctime = 0;
#if ARP_QUEUEING
while (arp_table[i].q != NULL) {
struct pbuf *p;
struct etharp_q_entry *q = arp_table[i].q;
arp_table[i].q = q->next;
p = q->p;
memp_free(MEMP_ARP_QUEUE, q);
#else
if (arp_table[i].q != NULL) {
struct pbuf *p = arp_table[i].q;
arp_table[i].q = NULL;
#endif
ethernet_output(netif, p, (struct eth_addr *)(netif->hwaddr), ethaddr, ETHTYPE_IP);
pbuf_free(p);
}if (arp_table[i].q != NULL) { ... }
return ERR_OK;
}{ ... }
#if ETHARP_SUPPORT_STATIC_ENTRIES
/* ... */
err_t
etharp_add_static_entry(const ip4_addr_t *ipaddr, struct eth_addr *ethaddr)
{
struct netif *netif;
LWIP_ASSERT_CORE_LOCKED();
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_add_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F" - %02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F":%02"X16_F"\n",
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr),
(u16_t)ethaddr->addr[0], (u16_t)ethaddr->addr[1], (u16_t)ethaddr->addr[2],
(u16_t)ethaddr->addr[3], (u16_t)ethaddr->addr[4], (u16_t)ethaddr->addr[5]));
netif = ip4_route(ipaddr);
if (netif == NULL) {
return ERR_RTE;
}if (netif == NULL) { ... }
return etharp_update_arp_entry(netif, ipaddr, ethaddr, ETHARP_FLAG_TRY_HARD | ETHARP_FLAG_STATIC_ENTRY);
}etharp_add_static_entry (const ip4_addr_t *ipaddr, struct eth_addr *ethaddr) { ... }
/* ... */
err_t
etharp_remove_static_entry(const ip4_addr_t *ipaddr)
{
s16_t i;
LWIP_ASSERT_CORE_LOCKED();
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_remove_static_entry: %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, NULL);
if (i < 0) {
return (err_t)i;
}if (i < 0) { ... }
if (arp_table[i].state != ETHARP_STATE_STATIC) {
return ERR_ARG;
}if (arp_table[i].state != ETHARP_STATE_STATIC) { ... }
etharp_free_entry(i);
return ERR_OK;
}etharp_remove_static_entry (const ip4_addr_t *ipaddr) { ... }
/* ... */#endif
/* ... */
void
etharp_cleanup_netif(struct netif *netif)
{
int i;
for (i = 0; i < ARP_TABLE_SIZE; ++i) {
u8_t state = arp_table[i].state;
if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) {
etharp_free_entry(i);
}if ((state != ETHARP_STATE_EMPTY) && (arp_table[i].netif == netif)) { ... }
}for (i = 0; i < ARP_TABLE_SIZE; ++i) { ... }
}{ ... }
/* ... */
ssize_t
etharp_find_addr(struct netif *netif, const ip4_addr_t *ipaddr,
struct eth_addr **eth_ret, const ip4_addr_t **ip_ret)
{
s16_t i;
LWIP_ASSERT("eth_ret != NULL && ip_ret != NULL",
eth_ret != NULL && ip_ret != NULL);
LWIP_UNUSED_ARG(netif);
i = etharp_find_entry(ipaddr, ETHARP_FLAG_FIND_ONLY, netif);
if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
*eth_ret = &arp_table[i].ethaddr;
*ip_ret = &arp_table[i].ipaddr;
return i;
}if ((i >= 0) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { ... }
return -1;
}{ ... }
/* ... */
int
etharp_get_entry(size_t i, ip4_addr_t **ipaddr, struct netif **netif, struct eth_addr **eth_ret)
{
LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
LWIP_ASSERT("netif != NULL", netif != NULL);
LWIP_ASSERT("eth_ret != NULL", eth_ret != NULL);
if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) {
*ipaddr = &arp_table[i].ipaddr;
*netif = arp_table[i].netif;
*eth_ret = &arp_table[i].ethaddr;
return 1;
}if ((i < ARP_TABLE_SIZE) && (arp_table[i].state >= ETHARP_STATE_STABLE)) { ... } else {
return 0;
}else { ... }
}{ ... }
/* ... */
void
etharp_input(struct pbuf *p, struct netif *netif)
{
struct etharp_hdr *hdr;
ip4_addr_t sipaddr, dipaddr;
u8_t for_us;
LWIP_ASSERT_CORE_LOCKED();
LWIP_ERROR("netif != NULL", (netif != NULL), return;);
hdr = (struct etharp_hdr *)p->payload;
if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) ||
(hdr->hwlen != ETH_HWADDR_LEN) ||
(hdr->protolen != sizeof(ip4_addr_t)) ||
(hdr->proto != PP_HTONS(ETHTYPE_IP))) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING,
("etharp_input: packet dropped, wrong hw type, hwlen, proto, protolen or ethernet type (%"U16_F"/%"U16_F"/%"U16_F"/%"U16_F")\n",
hdr->hwtype, (u16_t)hdr->hwlen, hdr->proto, (u16_t)hdr->protolen));
ETHARP_STATS_INC(etharp.proterr);
ETHARP_STATS_INC(etharp.drop);
pbuf_free(p);
return;
}if ((hdr->hwtype != PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET)) || (hdr->hwlen != ETH_HWADDR_LEN) || (hdr->protolen != sizeof(ip4_addr_t)) || (hdr->proto != PP_HTONS(ETHTYPE_IP))) { ... }
ETHARP_STATS_INC(etharp.recv);
#if LWIP_AUTOIP
/* ... */
autoip_arp_reply(netif, hdr);/* ... */
#endif
/* ... */
IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&sipaddr, &hdr->sipaddr);
IPADDR_WORDALIGNED_COPY_TO_IP4_ADDR_T(&dipaddr, &hdr->dipaddr);
if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
for_us = 0;
}if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { ... } else {
for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif));
}else { ... }
/* ... */
etharp_update_arp_entry(netif, &sipaddr, &(hdr->shwaddr),
for_us ? ETHARP_FLAG_TRY_HARD : ETHARP_FLAG_FIND_ONLY);
switch (hdr->opcode) {
case PP_HTONS(ARP_REQUEST):
/* ... */
LWIP_DEBUGF (ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP request\n"));
if (for_us) {
etharp_raw(netif,
(struct eth_addr *)netif->hwaddr, &hdr->shwaddr,
(struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif),
&hdr->shwaddr, &sipaddr,
ARP_REPLY);
}if (for_us) { ... } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: we are unconfigured, ARP request ignored.\n"));
}else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { ... } else {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP request was not for us.\n"));
}else { ... }
break;case PP_HTONS(ARP_REQUEST):
case PP_HTONS(ARP_REPLY):
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: incoming ARP reply\n"));
#if (LWIP_DHCP && DHCP_DOES_ARP_CHECK)
/* ... */
dhcp_arp_reply(netif, &sipaddr);/* ... */
#endif
break;case PP_HTONS(ARP_REPLY):
default:
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_input: ARP unknown opcode type %"S16_F"\n", lwip_htons(hdr->opcode)));
ETHARP_STATS_INC(etharp.err);
break;default
}switch (hdr->opcode) { ... }
pbuf_free(p);
}{ ... }
/* ... */
static err_t
etharp_output_to_arp_index(struct netif *netif, struct pbuf *q, netif_addr_idx_t arp_idx)
{
LWIP_ASSERT("arp_table[arp_idx].state >= ETHARP_STATE_STABLE",
arp_table[arp_idx].state >= ETHARP_STATE_STABLE);
/* ... */
if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) {
if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) {
if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) {
arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
}if (etharp_request(netif, &arp_table[arp_idx].ipaddr) == ERR_OK) { ... }
}if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_BROADCAST) { ... } else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) {
if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) {
arp_table[arp_idx].state = ETHARP_STATE_STABLE_REREQUESTING_1;
}if (etharp_request_dst(netif, &arp_table[arp_idx].ipaddr, &arp_table[arp_idx].ethaddr) == ERR_OK) { ... }
}else if (arp_table[arp_idx].ctime >= ARP_AGE_REREQUEST_USED_UNICAST) { ... }
}if (arp_table[arp_idx].state == ETHARP_STATE_STABLE) { ... }
return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), &arp_table[arp_idx].ethaddr, ETHTYPE_IP);
}{ ... }
/* ... */
err_t
etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
{
const struct eth_addr *dest;
struct eth_addr mcastaddr;
const ip4_addr_t *dst_addr = ipaddr;
LWIP_ASSERT_CORE_LOCKED();
LWIP_ASSERT("netif != NULL", netif != NULL);
LWIP_ASSERT("q != NULL", q != NULL);
LWIP_ASSERT("ipaddr != NULL", ipaddr != NULL);
/* ... */
if (ip4_addr_isbroadcast(ipaddr, netif)) {
dest = (const struct eth_addr *)ðbroadcast;
}if (ip4_addr_isbroadcast(ipaddr, netif)) { ... } else if (ip4_addr_ismulticast(ipaddr)) {
mcastaddr.addr[0] = LL_IP4_MULTICAST_ADDR_0;
mcastaddr.addr[1] = LL_IP4_MULTICAST_ADDR_1;
mcastaddr.addr[2] = LL_IP4_MULTICAST_ADDR_2;
mcastaddr.addr[3] = ip4_addr2(ipaddr) & 0x7f;
mcastaddr.addr[4] = ip4_addr3(ipaddr);
mcastaddr.addr[5] = ip4_addr4(ipaddr);
dest = &mcastaddr;
}else if (ip4_addr_ismulticast(ipaddr)) { ... } else {
netif_addr_idx_t i;
/* ... */
if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) &&
!ip4_addr_islinklocal(ipaddr)) {
#if LWIP_AUTOIP
struct ip_hdr *iphdr = LWIP_ALIGNMENT_CAST(struct ip_hdr *, q->payload);
/* ... */
if (!ip4_addr_islinklocal(&iphdr->src))/* ... */
#endif
{
#ifdef LWIP_HOOK_ETHARP_GET_GW
/* ... */
dst_addr = LWIP_HOOK_ETHARP_GET_GW(netif, ipaddr);
if (dst_addr == NULL)/* ... */
#endif
{
if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) {
dst_addr = netif_ip4_gw(netif);
}if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) { ... } else {
return ERR_RTE;
}else { ... }
...}
...}
}if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && !ip4_addr_islinklocal(ipaddr)) { ... }
#if LWIP_NETIF_HWADDRHINT
if (netif->hints != NULL) {
netif_addr_idx_t etharp_cached_entry = netif->hints->addr_hint;
if (etharp_cached_entry < ARP_TABLE_SIZE) {
#endif
if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) &&
#if ETHARP_TABLE_MATCH_NETIF
(arp_table[etharp_cached_entry].netif == netif) &&
#endif
(ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) {
ETHARP_STATS_INC(etharp.cachehit);
return etharp_output_to_arp_index(netif, q, etharp_cached_entry);
}if ((arp_table[etharp_cached_entry].state >= ETHARP_STATE_STABLE) && #if ETHARP_TABLE_MATCH_NETIF (arp_table[etharp_cached_entry].netif == netif) && #endif (ip4_addr_cmp(dst_addr, &arp_table[etharp_cached_entry].ipaddr))) { ... }
#if LWIP_NETIF_HWADDRHINT
}if (etharp_cached_entry < ARP_TABLE_SIZE) { ... }
}if (netif->hints != NULL) { ... }
#endif
/* ... */
for (i = 0; i < ARP_TABLE_SIZE; i++) {
if ((arp_table[i].state >= ETHARP_STATE_STABLE) &&
#if ETHARP_TABLE_MATCH_NETIF
(arp_table[i].netif == netif) &&
#endif
(ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) {
ETHARP_SET_ADDRHINT(netif, i);
return etharp_output_to_arp_index(netif, q, i);
}if ((arp_table[i].state >= ETHARP_STATE_STABLE) && #if ETHARP_TABLE_MATCH_NETIF (arp_table[i].netif == netif) && #endif (ip4_addr_cmp(dst_addr, &arp_table[i].ipaddr))) { ... }
}for (i = 0; i < ARP_TABLE_SIZE; i++) { ... }
/* ... */
return etharp_query(netif, dst_addr, q);
}else { ... }
return ethernet_output(netif, q, (struct eth_addr *)(netif->hwaddr), dest, ETHTYPE_IP);
}{ ... }
/* ... */
err_t
etharp_query(struct netif *netif, const ip4_addr_t *ipaddr, struct pbuf *q)
{
struct eth_addr *srcaddr = (struct eth_addr *)netif->hwaddr;
err_t result = ERR_MEM;
int is_new_entry = 0;
s16_t i_err;
netif_addr_idx_t i;
if (ip4_addr_isbroadcast(ipaddr, netif) ||
ip4_addr_ismulticast(ipaddr) ||
ip4_addr_isany(ipaddr)) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: will not add non-unicast IP address to ARP cache\n"));
return ERR_ARG;
}if (ip4_addr_isbroadcast(ipaddr, netif) || ip4_addr_ismulticast(ipaddr) || ip4_addr_isany(ipaddr)) { ... }
i_err = etharp_find_entry(ipaddr, ETHARP_FLAG_TRY_HARD, netif);
if (i_err < 0) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not create ARP entry\n"));
if (q) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: packet dropped\n"));
ETHARP_STATS_INC(etharp.memerr);
}if (q) { ... }
return (err_t)i_err;
}if (i_err < 0) { ... }
LWIP_ASSERT("type overflow", (size_t)i_err < NETIF_ADDR_IDX_MAX);
i = (netif_addr_idx_t)i_err;
if (arp_table[i].state == ETHARP_STATE_EMPTY) {
is_new_entry = 1;
arp_table[i].state = ETHARP_STATE_PENDING;
arp_table[i].netif = netif;
}if (arp_table[i].state == ETHARP_STATE_EMPTY) { ... }
LWIP_ASSERT("arp_table[i].state == PENDING or STABLE",
((arp_table[i].state == ETHARP_STATE_PENDING) ||
(arp_table[i].state >= ETHARP_STATE_STABLE)));
if (is_new_entry || (q == NULL)) {
result = etharp_request(netif, ipaddr);
if (result != ERR_OK) {
/* ... */
}if (result != ERR_OK) { ... }
if (q == NULL) {
return result;
}if (q == NULL) { ... }
}if (is_new_entry || (q == NULL)) { ... }
LWIP_ASSERT("q != NULL", q != NULL);
if (arp_table[i].state >= ETHARP_STATE_STABLE) {
ETHARP_SET_ADDRHINT(netif, i);
result = ethernet_output(netif, q, srcaddr, &(arp_table[i].ethaddr), ETHTYPE_IP);
}if (arp_table[i].state >= ETHARP_STATE_STABLE) { ... } else if (arp_table[i].state == ETHARP_STATE_PENDING) {
struct pbuf *p;
int copy_needed = 0;
/* ... */
p = q;
while (p) {
LWIP_ASSERT("no packet queues allowed!", (p->len != p->tot_len) || (p->next == 0));
if (PBUF_NEEDS_COPY(p)) {
copy_needed = 1;
break;
}if (PBUF_NEEDS_COPY(p)) { ... }
p = p->next;
}while (p) { ... }
if (copy_needed) {
p = pbuf_clone(PBUF_LINK, PBUF_RAM, q);
}if (copy_needed) { ... } else {
p = q;
pbuf_ref(p);
}else { ... }
if (p != NULL) {
#if ARP_QUEUEING
struct etharp_q_entry *new_entry;
new_entry = (struct etharp_q_entry *)memp_malloc(MEMP_ARP_QUEUE);
if (new_entry != NULL) {
unsigned int qlen = 0;
new_entry->next = 0;
new_entry->p = p;
if (arp_table[i].q != NULL) {
struct etharp_q_entry *r;
r = arp_table[i].q;
qlen++;
while (r->next != NULL) {
r = r->next;
qlen++;
}while (r->next != NULL) { ... }
r->next = new_entry;
}if (arp_table[i].q != NULL) { ... } else {
arp_table[i].q = new_entry;
}else { ... }
#if ARP_QUEUE_LEN
if (qlen >= ARP_QUEUE_LEN) {
struct etharp_q_entry *old;
old = arp_table[i].q;
arp_table[i].q = arp_table[i].q->next;
pbuf_free(old->p);
memp_free(MEMP_ARP_QUEUE, old);
}if (qlen >= ARP_QUEUE_LEN) { ... }
/* ... */#endif
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, i));
result = ERR_OK;
}if (new_entry != NULL) { ... } else {
pbuf_free(p);
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
result = ERR_MEM;
}else { ... }
/* ... */#else
if (arp_table[i].q != NULL) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: dropped previously queued packet %p for ARP entry %"U16_F"\n", (void *)q, (u16_t)i));
pbuf_free(arp_table[i].q);
}if (arp_table[i].q != NULL) { ... }
arp_table[i].q = p;
result = ERR_OK;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: queued packet %p on ARP entry %"U16_F"\n", (void *)q, (u16_t)i));/* ... */
#endif
}if (p != NULL) { ... } else {
ETHARP_STATS_INC(etharp.memerr);
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_query: could not queue a copy of PBUF_REF packet %p (out of memory)\n", (void *)q));
result = ERR_MEM;
}else { ... }
}else if (arp_table[i].state == ETHARP_STATE_PENDING) { ... }
return result;
}{ ... }
/* ... */
static err_t
etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
const struct eth_addr *ethdst_addr,
const struct eth_addr *hwsrc_addr, const ip4_addr_t *ipsrc_addr,
const struct eth_addr *hwdst_addr, const ip4_addr_t *ipdst_addr,
const u16_t opcode)
{
struct pbuf *p;
err_t result = ERR_OK;
struct etharp_hdr *hdr;
LWIP_ASSERT("netif != NULL", netif != NULL);
p = pbuf_alloc(PBUF_LINK, SIZEOF_ETHARP_HDR, PBUF_RAM);
if (p == NULL) {
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
("etharp_raw: could not allocate pbuf for ARP request.\n"));
ETHARP_STATS_INC(etharp.memerr);
return ERR_MEM;
}if (p == NULL) { ... }
LWIP_ASSERT("check that first pbuf can hold struct etharp_hdr",
(p->len >= SIZEOF_ETHARP_HDR));
hdr = (struct etharp_hdr *)p->payload;
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_raw: sending raw ARP packet.\n"));
hdr->opcode = lwip_htons(opcode);
LWIP_ASSERT("netif->hwaddr_len must be the same as ETH_HWADDR_LEN for etharp!",
(netif->hwaddr_len == ETH_HWADDR_LEN));
SMEMCPY(&hdr->shwaddr, hwsrc_addr, ETH_HWADDR_LEN);
SMEMCPY(&hdr->dhwaddr, hwdst_addr, ETH_HWADDR_LEN);
/* ... */
IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->sipaddr, ipsrc_addr);
IPADDR_WORDALIGNED_COPY_FROM_IP4_ADDR_T(&hdr->dipaddr, ipdst_addr);
hdr->hwtype = PP_HTONS(LWIP_IANA_HWTYPE_ETHERNET);
hdr->proto = PP_HTONS(ETHTYPE_IP);
hdr->hwlen = ETH_HWADDR_LEN;
hdr->protolen = sizeof(ip4_addr_t);
#if LWIP_AUTOIP
/* ... */
if (ip4_addr_islinklocal(ipsrc_addr)) {
ethernet_output(netif, p, ethsrc_addr, ðbroadcast, ETHTYPE_ARP);
}if (ip4_addr_islinklocal(ipsrc_addr)) { ... } else
#endif
{
ethernet_output(netif, p, ethsrc_addr, ethdst_addr, ETHTYPE_ARP);
}else { ... }
ETHARP_STATS_INC(etharp.xmit);
pbuf_free(p);
p = NULL;
return result;
}{ ... }
/* ... */
static err_t
etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr *hw_dst_addr)
{
return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr,
(struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero,
ipaddr, ARP_REQUEST);
}{ ... }
/* ... */
err_t
etharp_request(struct netif *netif, const ip4_addr_t *ipaddr)
{
LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_request: sending ARP request.\n"));
return etharp_request_dst(netif, ipaddr, ðbroadcast);
}{ ... }
/* ... */#endif