1
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
54
55
56
57
58
59
60
61
62
63
64
65
68
69
70
71
72
76
77
78
79
80
81
82
83
84
85
86
94
95
96
97
98
103
104
105
106
107
108
112
113
114
115
116
117
118
119
120
121
127
128
129
130
131
132
133
134
136
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
232
233
234
235
236
238
239
240
241
242
243
244
262
266
267
268
269
270
271
277
278
279
280
281
282
283
284
285
286
287
300
301
302
303
304
305
306
307
308
309
310
311
312
316
317
318
319
320
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
361
362
363
364
366
367
368
369
370
371
372
373
377
378
380
431
432
433
434
435
437
438
439
440
441
444
452
453
454
455
456
457
458
459
460
461
464
465
466
467
476
478
485
486
487
490
491
492
493
494
495
496
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
524
525
526
527
528
532
533
534
535
536
537
538
540
541
542
543
544
545
546
547
548
549
550
551
552
554
565
566
583
584
586
587
588
589
596
597
598
599
602
603
604
607
608
609
610
611
612
613
614
615
616
617
618
619
621
622
623
624
625
626
627
628
629
630
631
632
633
634
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
686
687
688
689
690
691
692
693
694
695
696
697
698
699
703
704
705
706
711
712
714
715
716
717
718
719
720
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
747
748
749
750
751
752
753
754
755
756
757
758
759
760
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
783
784
785
786
787
791
792
793
794
795
796
797
798
799
800
801
804
805
806
807
808
809
810
811
812
813
814
815
816
821
822
826
827
828
829
834
835
836
837
838
841
842
843
847
848
849
850
851
852
853
854
855
856
860
861
862
863
864
868
869
872
873
874
875
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_IPV4
#include "lwip/ip4_frag.h"
#include "lwip/def.h"
#include "lwip/inet_chksum.h"
#include "lwip/netif.h"
#include "lwip/stats.h"
#include "lwip/icmp.h"
#include <string.h>
7 includes
#if IP_REASSEMBLY
/* ... */
/* ... */
#ifndef IP_REASS_CHECK_OVERLAP
#define IP_REASS_CHECK_OVERLAP 1
#endif
/* ... */
#ifndef IP_REASS_FREE_OLDEST
#define IP_REASS_FREE_OLDEST 1
#endif
#define IP_REASS_FLAG_LASTFRAG 0x01
#define IP_REASS_VALIDATE_TELEGRAM_FINISHED 1
#define IP_REASS_VALIDATE_PBUF_QUEUED 0
#define IP_REASS_VALIDATE_PBUF_DROPPED -1
/* ... */
#ifdef PACK_STRUCT_USE_INCLUDES
# include "arch/bpstruct.h"
#endif
PACK_STRUCT_BEGIN
struct ip_reass_helper {
PACK_STRUCT_FIELD(struct pbuf *next_pbuf);
PACK_STRUCT_FIELD(u16_t start);
PACK_STRUCT_FIELD(u16_t end);
...} PACK_STRUCT_STRUCT;
PACK_STRUCT_END
#ifdef PACK_STRUCT_USE_INCLUDES
# include "arch/epstruct.h"
#endif
#define IP_ADDRESSES_AND_ID_MATCH(iphdrA, iphdrB) \
(ip4_addr_cmp(&(iphdrA)->src, &(iphdrB)->src) && \
ip4_addr_cmp(&(iphdrA)->dest, &(iphdrB)->dest) && \
IPH_ID(iphdrA) == IPH_ID(iphdrB)) ? 1 : 0...
static struct ip_reassdata *reassdatagrams;
static u16_t ip_reass_pbufcount;
static void ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
static int ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev);
/* ... */
void
ip_reass_tmr(void)
{
struct ip_reassdata *r, *prev = NULL;
r = reassdatagrams;
while (r != NULL) {
/* ... */
if (r->timer > 0) {
r->timer--;
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer dec %"U16_F"\n", (u16_t)r->timer));
prev = r;
r = r->next;
}if (r->timer > 0) { ... } else {
struct ip_reassdata *tmp;
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_tmr: timer timed out\n"));
tmp = r;
r = r->next;
ip_reass_free_complete_datagram(tmp, prev);
}else { ... }
}while (r != NULL) { ... }
}{ ... }
/* ... */
static int
ip_reass_free_complete_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
{
u16_t pbufs_freed = 0;
u16_t clen;
struct pbuf *p;
struct ip_reass_helper *iprh;
LWIP_ASSERT("prev != ipr", prev != ipr);
if (prev != NULL) {
LWIP_ASSERT("prev->next == ipr", prev->next == ipr);
}if (prev != NULL) { ... }
MIB2_STATS_INC(mib2.ipreasmfails);
#if LWIP_ICMP
iprh = (struct ip_reass_helper *)ipr->p->payload;
if (iprh->start == 0) {
p = ipr->p;
ipr->p = iprh->next_pbuf;
SMEMCPY(p->payload, &ipr->iphdr, IP_HLEN);
icmp_time_exceeded(p, ICMP_TE_FRAG);
clen = pbuf_clen(p);
LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
pbufs_freed = (u16_t)(pbufs_freed + clen);
pbuf_free(p);
}if (iprh->start == 0) { ... }
/* ... */#endif
/* ... */
p = ipr->p;
while (p != NULL) {
struct pbuf *pcur;
iprh = (struct ip_reass_helper *)p->payload;
pcur = p;
p = iprh->next_pbuf;
clen = pbuf_clen(pcur);
LWIP_ASSERT("pbufs_freed + clen <= 0xffff", pbufs_freed + clen <= 0xffff);
pbufs_freed = (u16_t)(pbufs_freed + clen);
pbuf_free(pcur);
}while (p != NULL) { ... }
ip_reass_dequeue_datagram(ipr, prev);
LWIP_ASSERT("ip_reass_pbufcount >= pbufs_freed", ip_reass_pbufcount >= pbufs_freed);
ip_reass_pbufcount = (u16_t)(ip_reass_pbufcount - pbufs_freed);
return pbufs_freed;
}{ ... }
#if IP_REASS_FREE_OLDEST
/* ... */
static int
ip_reass_remove_oldest_datagram(struct ip_hdr *fraghdr, int pbufs_needed)
{
/* ... */
struct ip_reassdata *r, *oldest, *prev, *oldest_prev;
int pbufs_freed = 0, pbufs_freed_current;
int other_datagrams;
/* ... */
do {
oldest = NULL;
prev = NULL;
oldest_prev = NULL;
other_datagrams = 0;
r = reassdatagrams;
while (r != NULL) {
if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) {
other_datagrams++;
if (oldest == NULL) {
oldest = r;
oldest_prev = prev;
}if (oldest == NULL) { ... } else if (r->timer <= oldest->timer) {
oldest = r;
oldest_prev = prev;
}else if (r->timer <= oldest->timer) { ... }
}if (!IP_ADDRESSES_AND_ID_MATCH(&r->iphdr, fraghdr)) { ... }
if (r->next != NULL) {
prev = r;
}if (r->next != NULL) { ... }
r = r->next;
}while (r != NULL) { ... }
if (oldest != NULL) {
pbufs_freed_current = ip_reass_free_complete_datagram(oldest, oldest_prev);
pbufs_freed += pbufs_freed_current;
}if (oldest != NULL) { ... }
...} while ((pbufs_freed < pbufs_needed) && (other_datagrams > 1));
return pbufs_freed;
}{ ... }
#endif/* ... */
/* ... */
static struct ip_reassdata *
ip_reass_enqueue_new_datagram(struct ip_hdr *fraghdr, int clen)
{
struct ip_reassdata *ipr;
#if ! IP_REASS_FREE_OLDEST
LWIP_UNUSED_ARG(clen);
#endif
ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
if (ipr == NULL) {
#if IP_REASS_FREE_OLDEST
if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) {
ipr = (struct ip_reassdata *)memp_malloc(MEMP_REASSDATA);
}if (ip_reass_remove_oldest_datagram(fraghdr, clen) >= clen) { ... }
if (ipr == NULL)/* ... */
#endif
{
IPFRAG_STATS_INC(ip_frag.memerr);
LWIP_DEBUGF(IP_REASS_DEBUG, ("Failed to alloc reassdata struct\n"));
return NULL;
...}
}if (ipr == NULL) { ... }
memset(ipr, 0, sizeof(struct ip_reassdata));
ipr->timer = IP_REASS_MAXAGE;
ipr->next = reassdatagrams;
reassdatagrams = ipr;
SMEMCPY(&(ipr->iphdr), fraghdr, IP_HLEN);
return ipr;
}{ ... }
/* ... */
static void
ip_reass_dequeue_datagram(struct ip_reassdata *ipr, struct ip_reassdata *prev)
{
if (reassdatagrams == ipr) {
reassdatagrams = ipr->next;
}if (reassdatagrams == ipr) { ... } else {
LWIP_ASSERT("sanity check linked list", prev != NULL);
prev->next = ipr->next;
}else { ... }
memp_free(MEMP_REASSDATA, ipr);
}{ ... }
/* ... */
static int
ip_reass_chain_frag_into_datagram_and_validate(struct ip_reassdata *ipr, struct pbuf *new_p, int is_last)
{
struct ip_reass_helper *iprh, *iprh_tmp, *iprh_prev = NULL;
struct pbuf *q;
u16_t offset, len;
u8_t hlen;
struct ip_hdr *fraghdr;
int valid = 1;
fraghdr = (struct ip_hdr *)new_p->payload;
len = lwip_ntohs(IPH_LEN(fraghdr));
hlen = IPH_HL_BYTES(fraghdr);
if (hlen > len) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
}if (hlen > len) { ... }
len = (u16_t)(len - hlen);
offset = IPH_OFFSET_BYTES(fraghdr);
/* ... */
LWIP_ASSERT("sizeof(struct ip_reass_helper) <= IP_HLEN",
sizeof(struct ip_reass_helper) <= IP_HLEN);
iprh = (struct ip_reass_helper *)new_p->payload;
iprh->next_pbuf = NULL;
iprh->start = offset;
iprh->end = (u16_t)(offset + len);
if (iprh->end < offset) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
}if (iprh->end < offset) { ... }
/* ... */
for (q = ipr->p; q != NULL;) {
iprh_tmp = (struct ip_reass_helper *)q->payload;
if (iprh->start < iprh_tmp->start) {
iprh->next_pbuf = q;
if (iprh_prev != NULL) {
#if IP_REASS_CHECK_OVERLAP
if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
}if ((iprh->start < iprh_prev->end) || (iprh->end > iprh_tmp->start)) { ... }
/* ... */#endif
iprh_prev->next_pbuf = new_p;
if (iprh_prev->end != iprh->start) {
/* ... */
valid = 0;
}if (iprh_prev->end != iprh->start) { ... }
}if (iprh_prev != NULL) { ... } else {
#if IP_REASS_CHECK_OVERLAP
if (iprh->end > iprh_tmp->start) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
}if (iprh->end > iprh_tmp->start) { ... }
/* ... */#endif
ipr->p = new_p;
}else { ... }
break;
}if (iprh->start < iprh_tmp->start) { ... } else if (iprh->start == iprh_tmp->start) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
#if IP_REASS_CHECK_OVERLAP
}else if (iprh->start == iprh_tmp->start) { ... } else if (iprh->start < iprh_tmp->end) {
return IP_REASS_VALIDATE_PBUF_DROPPED;
#endif
}else if (iprh->start < iprh_tmp->end) { ... } else {
if (iprh_prev != NULL) {
if (iprh_prev->end != iprh_tmp->start) {
/* ... */
valid = 0;
}if (iprh_prev->end != iprh_tmp->start) { ... }
}if (iprh_prev != NULL) { ... }
}else { ... }
q = iprh_tmp->next_pbuf;
iprh_prev = iprh_tmp;
}for (q = ipr->p; q != NULL;) { ... }
if (q == NULL) {
if (iprh_prev != NULL) {
/* ... */
#if IP_REASS_CHECK_OVERLAP
LWIP_ASSERT("check fragments don't overlap", iprh_prev->end <= iprh->start);
#endif
iprh_prev->next_pbuf = new_p;
if (iprh_prev->end != iprh->start) {
valid = 0;
}if (iprh_prev->end != iprh->start) { ... }
}if (iprh_prev != NULL) { ... } else {
#if IP_REASS_CHECK_OVERLAP
LWIP_ASSERT("no previous fragment, this must be the first fragment!",
ipr->p == NULL);/* ... */
#endif
ipr->p = new_p;
}else { ... }
}if (q == NULL) { ... }
if (is_last || ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0)) {
if (valid) {
if ((ipr->p == NULL) || (((struct ip_reass_helper *)ipr->p->payload)->start != 0)) {
valid = 0;
}if ((ipr->p == NULL) || (((struct ip_reass_helper *)ipr->p->payload)->start != 0)) { ... } else {
iprh_prev = iprh;
q = iprh->next_pbuf;
while (q != NULL) {
iprh = (struct ip_reass_helper *)q->payload;
if (iprh_prev->end != iprh->start) {
valid = 0;
break;
}if (iprh_prev->end != iprh->start) { ... }
iprh_prev = iprh;
q = iprh->next_pbuf;
}while (q != NULL) { ... }
/* ... */
if (valid) {
LWIP_ASSERT("sanity check", ipr->p != NULL);
LWIP_ASSERT("sanity check",
((struct ip_reass_helper *)ipr->p->payload) != iprh);
LWIP_ASSERT("validate_datagram:next_pbuf!=NULL",
iprh->next_pbuf == NULL);
}if (valid) { ... }
}else { ... }
}if (valid) { ... }
/* ... */
return valid ? IP_REASS_VALIDATE_TELEGRAM_FINISHED : IP_REASS_VALIDATE_PBUF_QUEUED;
}if (is_last || ((ipr->flags & IP_REASS_FLAG_LASTFRAG) != 0)) { ... }
return IP_REASS_VALIDATE_PBUF_QUEUED;
}{ ... }
/* ... */
struct pbuf *
ip4_reass(struct pbuf *p)
{
struct pbuf *r;
struct ip_hdr *fraghdr;
struct ip_reassdata *ipr;
struct ip_reass_helper *iprh;
u16_t offset, len, clen;
u8_t hlen;
int valid;
int is_last;
IPFRAG_STATS_INC(ip_frag.recv);
MIB2_STATS_INC(mib2.ipreasmreqds);
fraghdr = (struct ip_hdr *)p->payload;
if (IPH_HL_BYTES(fraghdr) != IP_HLEN) {
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: IP options currently not supported!\n"));
IPFRAG_STATS_INC(ip_frag.err);
goto nullreturn;
}if (IPH_HL_BYTES(fraghdr) != IP_HLEN) { ... }
offset = IPH_OFFSET_BYTES(fraghdr);
len = lwip_ntohs(IPH_LEN(fraghdr));
hlen = IPH_HL_BYTES(fraghdr);
if (hlen > len) {
goto nullreturn;
}if (hlen > len) { ... }
len = (u16_t)(len - hlen);
clen = pbuf_clen(p);
if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) {
#if IP_REASS_FREE_OLDEST
if (!ip_reass_remove_oldest_datagram(fraghdr, clen) ||
((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS))/* ... */
#endif
{
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: Overflow condition: pbufct=%d, clen=%d, MAX=%d\n",
ip_reass_pbufcount, clen, IP_REASS_MAX_PBUFS));
IPFRAG_STATS_INC(ip_frag.memerr);
goto nullreturn;
...}
}if ((ip_reass_pbufcount + clen) > IP_REASS_MAX_PBUFS) { ... }
/* ... */
for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) {
/* ... */
if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) {
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: matching previous fragment ID=%"X16_F"\n",
lwip_ntohs(IPH_ID(fraghdr))));
IPFRAG_STATS_INC(ip_frag.cachehit);
break;
}if (IP_ADDRESSES_AND_ID_MATCH(&ipr->iphdr, fraghdr)) { ... }
}for (ipr = reassdatagrams; ipr != NULL; ipr = ipr->next) { ... }
if (ipr == NULL) {
ipr = ip_reass_enqueue_new_datagram(fraghdr, clen);
if (ipr == NULL) {
goto nullreturn;
}if (ipr == NULL) { ... }
}if (ipr == NULL) { ... } else {
if (((lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) &&
((lwip_ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) {
/* ... */
SMEMCPY(&ipr->iphdr, fraghdr, IP_HLEN);
}if (((lwip_ntohs(IPH_OFFSET(fraghdr)) & IP_OFFMASK) == 0) && ((lwip_ntohs(IPH_OFFSET(&ipr->iphdr)) & IP_OFFMASK) != 0)) { ... }
}else { ... }
/* ... */
is_last = (IPH_OFFSET(fraghdr) & PP_NTOHS(IP_MF)) == 0;
if (is_last) {
u16_t datagram_len = (u16_t)(offset + len);
if ((datagram_len < offset) || (datagram_len > (0xFFFF - IP_HLEN))) {
goto nullreturn_ipr;
}if ((datagram_len < offset) || (datagram_len > (0xFFFF - IP_HLEN))) { ... }
}if (is_last) { ... }
valid = ip_reass_chain_frag_into_datagram_and_validate(ipr, p, is_last);
if (valid == IP_REASS_VALIDATE_PBUF_DROPPED) {
goto nullreturn_ipr;
}if (valid == IP_REASS_VALIDATE_PBUF_DROPPED) { ... }
/* ... */
ip_reass_pbufcount = (u16_t)(ip_reass_pbufcount + clen);
if (is_last) {
u16_t datagram_len = (u16_t)(offset + len);
ipr->datagram_len = datagram_len;
ipr->flags |= IP_REASS_FLAG_LASTFRAG;
LWIP_DEBUGF(IP_REASS_DEBUG,
("ip4_reass: last fragment seen, total len %"S16_F"\n",
ipr->datagram_len));
}if (is_last) { ... }
if (valid == IP_REASS_VALIDATE_TELEGRAM_FINISHED) {
struct ip_reassdata *ipr_prev;
/* ... */
u16_t datagram_len = (u16_t)(ipr->datagram_len + IP_HLEN);
r = ((struct ip_reass_helper *)ipr->p->payload)->next_pbuf;
fraghdr = (struct ip_hdr *)(ipr->p->payload);
SMEMCPY(fraghdr, &ipr->iphdr, IP_HLEN);
IPH_LEN_SET(fraghdr, lwip_htons(datagram_len));
IPH_OFFSET_SET(fraghdr, 0);
IPH_CHKSUM_SET(fraghdr, 0);
#if CHECKSUM_GEN_IP
IF__NETIF_CHECKSUM_ENABLED(ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) {
IPH_CHKSUM_SET(fraghdr, inet_chksum(fraghdr, IP_HLEN));
}IF__NETIF_CHECKSUM_ENABLED (ip_current_input_netif(), NETIF_CHECKSUM_GEN_IP) { ... }
/* ... */#endif
p = ipr->p;
while (r != NULL) {
iprh = (struct ip_reass_helper *)r->payload;
pbuf_remove_header(r, IP_HLEN);
pbuf_cat(p, r);
r = iprh->next_pbuf;
}while (r != NULL) { ... }
if (ipr == reassdatagrams) {
ipr_prev = NULL;
}if (ipr == reassdatagrams) { ... } else {
for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) {
if (ipr_prev->next == ipr) {
break;
}if (ipr_prev->next == ipr) { ... }
}for (ipr_prev = reassdatagrams; ipr_prev != NULL; ipr_prev = ipr_prev->next) { ... }
}else { ... }
ip_reass_dequeue_datagram(ipr, ipr_prev);
clen = pbuf_clen(p);
LWIP_ASSERT("ip_reass_pbufcount >= clen", ip_reass_pbufcount >= clen);
ip_reass_pbufcount = (u16_t)(ip_reass_pbufcount - clen);
MIB2_STATS_INC(mib2.ipreasmoks);
return p;
}if (valid == IP_REASS_VALIDATE_TELEGRAM_FINISHED) { ... }
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip_reass_pbufcount: %d out\n", ip_reass_pbufcount));
return NULL;
nullreturn_ipr:
LWIP_ASSERT("ipr != NULL", ipr != NULL);
if (ipr->p == NULL) {
LWIP_ASSERT("not firstalthough just enqueued", ipr == reassdatagrams);
ip_reass_dequeue_datagram(ipr, NULL);
}if (ipr->p == NULL) { ... }
nullreturn:
LWIP_DEBUGF(IP_REASS_DEBUG, ("ip4_reass: nullreturn\n"));
IPFRAG_STATS_INC(ip_frag.drop);
pbuf_free(p);
return NULL;
}{ ... }
#endif/* ... */
#if IP_FRAG
#if !LWIP_NETIF_TX_SINGLE_PBUF
static struct pbuf_custom_ref *
ip_frag_alloc_pbuf_custom_ref(void)
{
return (struct pbuf_custom_ref *)memp_malloc(MEMP_FRAG_PBUF);
}{ ... }
static void
ip_frag_free_pbuf_custom_ref(struct pbuf_custom_ref *p)
{
LWIP_ASSERT("p != NULL", p != NULL);
memp_free(MEMP_FRAG_PBUF, p);
}{ ... }
/* ... */
static void
ipfrag_free_pbuf_custom(struct pbuf *p)
{
struct pbuf_custom_ref *pcr = (struct pbuf_custom_ref *)p;
LWIP_ASSERT("pcr != NULL", pcr != NULL);
LWIP_ASSERT("pcr == p", (void *)pcr == (void *)p);
if (pcr->original != NULL) {
pbuf_free(pcr->original);
}if (pcr->original != NULL) { ... }
ip_frag_free_pbuf_custom_ref(pcr);
}{ ... }
#endif/* ... */
/* ... */
err_t
ip4_frag(struct pbuf *p, struct netif *netif, const ip4_addr_t *dest)
{
struct pbuf *rambuf;
#if !LWIP_NETIF_TX_SINGLE_PBUF
struct pbuf *newpbuf;
u16_t newpbuflen = 0;
u16_t left_to_copy;/* ... */
#endif
struct ip_hdr *original_iphdr;
struct ip_hdr *iphdr;
const u16_t nfb = (u16_t)((netif->mtu - IP_HLEN) / 8);
u16_t left, fragsize;
u16_t ofo;
int last;
u16_t poff = IP_HLEN;
u16_t tmp;
int mf_set;
original_iphdr = (struct ip_hdr *)p->payload;
iphdr = original_iphdr;
if (IPH_HL_BYTES(iphdr) != IP_HLEN) {
return ERR_VAL;
}if (IPH_HL_BYTES(iphdr) != IP_HLEN) { ... }
LWIP_ERROR("ip4_frag(): pbuf too short", p->len >= IP_HLEN, return ERR_VAL);
tmp = lwip_ntohs(IPH_OFFSET(iphdr));
ofo = tmp & IP_OFFMASK;
mf_set = tmp & IP_MF;
left = (u16_t)(p->tot_len - IP_HLEN);
while (left) {
fragsize = LWIP_MIN(left, (u16_t)(nfb * 8));
#if LWIP_NETIF_TX_SINGLE_PBUF
rambuf = pbuf_alloc(PBUF_IP, fragsize, PBUF_RAM);
if (rambuf == NULL) {
goto memerr;
}if (rambuf == NULL) { ... }
LWIP_ASSERT("this needs a pbuf in one piece!",
(rambuf->len == rambuf->tot_len) && (rambuf->next == NULL));
poff += pbuf_copy_partial(p, rambuf->payload, fragsize, poff);
if (pbuf_add_header(rambuf, IP_HLEN)) {
pbuf_free(rambuf);
goto memerr;
}if (pbuf_add_header(rambuf, IP_HLEN)) { ... }
SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
iphdr = (struct ip_hdr *)rambuf->payload;/* ... */
#else
/* ... */
rambuf = pbuf_alloc(PBUF_LINK, IP_HLEN, PBUF_RAM);
if (rambuf == NULL) {
goto memerr;
}if (rambuf == NULL) { ... }
LWIP_ASSERT("this needs a pbuf in one piece!",
(rambuf->len >= (IP_HLEN)));
SMEMCPY(rambuf->payload, original_iphdr, IP_HLEN);
iphdr = (struct ip_hdr *)rambuf->payload;
left_to_copy = fragsize;
while (left_to_copy) {
struct pbuf_custom_ref *pcr;
u16_t plen = (u16_t)(p->len - poff);
LWIP_ASSERT("p->len >= poff", p->len >= poff);
newpbuflen = LWIP_MIN(left_to_copy, plen);
if (!newpbuflen) {
poff = 0;
p = p->next;
continue;
}if (!newpbuflen) { ... }
pcr = ip_frag_alloc_pbuf_custom_ref();
if (pcr == NULL) {
pbuf_free(rambuf);
goto memerr;
}if (pcr == NULL) { ... }
newpbuf = pbuf_alloced_custom(PBUF_RAW, newpbuflen, PBUF_REF, &pcr->pc,
(u8_t *)p->payload + poff, newpbuflen);
if (newpbuf == NULL) {
ip_frag_free_pbuf_custom_ref(pcr);
pbuf_free(rambuf);
goto memerr;
}if (newpbuf == NULL) { ... }
pbuf_ref(p);
pcr->original = p;
pcr->pc.custom_free_function = ipfrag_free_pbuf_custom;
/* ... */
pbuf_cat(rambuf, newpbuf);
left_to_copy = (u16_t)(left_to_copy - newpbuflen);
if (left_to_copy) {
poff = 0;
p = p->next;
}if (left_to_copy) { ... }
}while (left_to_copy) { ... }
poff = (u16_t)(poff + newpbuflen);/* ... */
#endif
last = (left <= netif->mtu - IP_HLEN);
tmp = (IP_OFFMASK & (ofo));
if (!last || mf_set) {
tmp = tmp | IP_MF;
}if (!last || mf_set) { ... }
IPH_OFFSET_SET(iphdr, lwip_htons(tmp));
IPH_LEN_SET(iphdr, lwip_htons((u16_t)(fragsize + IP_HLEN)));
IPH_CHKSUM_SET(iphdr, 0);
#if CHECKSUM_GEN_IP
IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IP_HLEN));
}IF__NETIF_CHECKSUM_ENABLED (netif, NETIF_CHECKSUM_GEN_IP) { ... }
/* ... */#endif
/* ... */
netif->output(netif, rambuf, dest);
IPFRAG_STATS_INC(ip_frag.xmit);
/* ... */
pbuf_free(rambuf);
left = (u16_t)(left - fragsize);
ofo = (u16_t)(ofo + nfb);
}while (left) { ... }
MIB2_STATS_INC(mib2.ipfragoks);
return ERR_OK;
memerr:
MIB2_STATS_INC(mib2.ipfragfails);
return ERR_MEM;
}{ ... }
#endif/* ... */
/* ... */
#endif