forked from pfalcon/esp-open-lwip
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathip.c
1737 lines (1568 loc) · 56.5 KB
/
ip.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
/**
* @file
* This is the IPv4 layer implementation for incoming and outgoing IP traffic.
*
* @see ip_frag.c
*
*/
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels <[email protected]>
*
*/
#include "lwip/opt.h"
#include "lwip/ip.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/ip_frag.h"
#include "lwip/inet_chksum.h"
#include "lwip/netif.h"
#include "lwip/icmp.h"
#include "lwip/igmp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcp_impl.h"
#include "lwip/snmp.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/stats.h"
#include "lwip/lwip_napt.h"
#ifdef IP_ROUTING_TAB
#include "lwip/ip_route.h"
#endif
#include "arch/perf.h"
#include <string.h>
/** Set this to 0 in the rare case of wanting to call an extra function to
* generate the IP checksum (in contrast to calculating it on-the-fly). */
#ifndef LWIP_INLINE_IP_CHKSUM
#define LWIP_INLINE_IP_CHKSUM 1
#endif
#if LWIP_INLINE_IP_CHKSUM && CHECKSUM_GEN_IP
#define CHECKSUM_GEN_IP_INLINE 1
#else
#define CHECKSUM_GEN_IP_INLINE 0
#endif
#if LWIP_DHCP || defined(LWIP_IP_ACCEPT_UDP_PORT)
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 1
/** Some defines for DHCP to let link-layer-addressed packets through while the
* netif is down.
* To use this in your own application/protocol, define LWIP_IP_ACCEPT_UDP_PORT
* to return 1 if the port is accepted and 0 if the port is not accepted.
*/
#if LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT)
/* accept DHCP client port and custom port */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (((port) == PP_NTOHS(DHCP_CLIENT_PORT)) \
|| (LWIP_IP_ACCEPT_UDP_PORT(port)))
#elif defined(LWIP_IP_ACCEPT_UDP_PORT) /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
/* accept custom port only */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) (LWIP_IP_ACCEPT_UDP_PORT(dst_port))
#else /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
/* accept DHCP client port only */
#define IP_ACCEPT_LINK_LAYER_ADDRESSED_PORT(port) ((port) == PP_NTOHS(DHCP_CLIENT_PORT))
#endif /* LWIP_DHCP && defined(LWIP_IP_ACCEPT_UDP_PORT) */
#else /* LWIP_DHCP */
#define IP_ACCEPT_LINK_LAYER_ADDRESSING 0
#endif /* LWIP_DHCP */
/**
* The interface that provided the packet for the current callback
* invocation.
*/
struct netif *current_netif;
/**
* Header of the input packet currently being processed.
*/
const struct ip_hdr *current_header;
/** Source IP address of current_header */
ip_addr_t current_iphdr_src;
/** Destination IP address of current_header */
ip_addr_t current_iphdr_dest;
/** The IP header ID of the next outgoing IP packet */
static u16_t ip_id;
#ifdef IP_ROUTING_TAB
/** Destination IP address of current_header after routing */
ip_addr_t current_ip_new_dest;
#endif /* IP_ROUTING_TAB */
/*
void
ip_print(struct pbuf *p)
{
struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
u8_t *payload;
payload = (u8_t *)iphdr + IP_HLEN;
os_printf("IP header:\n");
os_printf("+-------------------------------+\n");
os_printf("|%2"S16_F" |%2"S16_F" | 0x%02"X16_F" | %5"U16_F" | (v, hl, tos, len)\n",
IPH_V(iphdr),
IPH_HL(iphdr),
IPH_TOS(iphdr),
ntohs(IPH_LEN(iphdr)));
os_printf("+-------------------------------+\n");
os_printf("| %5"U16_F" |%"U16_F"%"U16_F"%"U16_F"| %4"U16_F" | (id, flags, offset)\n",
ntohs(IPH_ID(iphdr)),
ntohs(IPH_OFFSET(iphdr)) >> 15 & 1,
ntohs(IPH_OFFSET(iphdr)) >> 14 & 1,
ntohs(IPH_OFFSET(iphdr)) >> 13 & 1,
ntohs(IPH_OFFSET(iphdr)) & IP_OFFMASK);
os_printf("+-------------------------------+\n");
os_printf("| %3"U16_F" | %3"U16_F" | 0x%04"X16_F" | (ttl, proto, chksum)\n",
IPH_TTL(iphdr),
IPH_PROTO(iphdr),
ntohs(IPH_CHKSUM(iphdr)));
os_printf("+-------------------------------+\n");
os_printf("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
ip4_addr1_16(&iphdr->src),
ip4_addr2_16(&iphdr->src),
ip4_addr3_16(&iphdr->src),
ip4_addr4_16(&iphdr->src));
os_printf("+-------------------------------+\n");
os_printf("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
ip4_addr1_16(&iphdr->dest),
ip4_addr2_16(&iphdr->dest),
ip4_addr3_16(&iphdr->dest),
ip4_addr4_16(&iphdr->dest));
os_printf("+-------------------------------+\n");
}
*/
/**
* Finds the appropriate network interface for a given IP address. It
* searches the list of network interfaces linearly. A match is found
* if the masked IP address of the network interface equals the masked
* IP address given to the function.
*
* @param dest the destination IP address for which to find the route
* @return the netif on which to send to reach dest
*/
struct netif *ICACHE_FLASH_ATTR
ip_route(ip_addr_t *dest)
{
struct netif *netif;
#ifdef IP_ROUTING_TAB
ip_addr_copy(current_ip_new_dest, *dest);
#endif
#ifdef IP_ROUTING_TAB
int i;
//os_printf_plus("ip_route route to %d.%d.%d.%d\r\n",
// ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest));
/* search route */
struct route_entry *found_route = ip_find_route(*dest);
if (found_route) {
ip_addr_copy(current_ip_new_dest, found_route->gw);
/* now go on and find the netif on which to forward the packet */
dest = ¤t_ip_new_dest;
//os_printf_plus("redirected to %d.%d.%d.%d\r\n",
// ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest));
}
#endif /* IP_ROUTING_TAB */
/* iterate through netifs */
for(netif = netif_list; netif != NULL; netif = netif->next) {
/* network mask matches? */
if (netif_is_up(netif)) {
if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
/* return netif on which to forward IP packet */
//os_printf_plus("send through netif %c%c%d\r\n", netif->name[0], netif->name[1], netif->num);
return netif;
}
}
}
/* iterate through netifs */
for(netif = netif_list; netif != NULL; netif = netif->next) {
/* This is a hack! Always sends is through the STA interface as default */
if (netif_is_up(netif)) {
if (!ip_addr_isbroadcast(dest, netif) && netif == (struct netif *)eagle_lwip_getif(0)) {
//os_printf_plus("HACK send through netif %c%c%d\r\n", netif->name[0], netif->name[1], netif->num);
return netif;
}
}
}
if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
IP_STATS_INC(ip.rterr);
snmp_inc_ipoutnoroutes();
//os_printf_plus("no netif found\r\n");
return NULL;
}
/* no matching netif found, use default netif */
//os_printf_plus("send through netif %c%c%d\r\n", netif_default->name[0], netif_default->name[1], netif_default->num);
return netif_default;
}
/**
* Finds the appropriate network interface for a source IP address. It
* searches the list of network interfaces linearly. A match is found
* if the masked IP address of the network interface equals the masked
* IP address given to the function.
*
* @param source the sourcination IP address for which to find the route
* @return the netif on which to send to reach source
*/
struct netif *ICACHE_FLASH_ATTR
ip_router(ip_addr_t *dest, ip_addr_t *source){
struct netif *netif;
/* iterate through netifs */
for(netif = netif_list; netif != NULL; netif = netif->next) {
/* network mask matches? */
if (netif_is_up(netif)) {
if (ip_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) {
/* return netif on which to forward IP packet */
return netif;
}
}
if (netif_is_up(netif)) {
if (ip_addr_netcmp(source, &(netif->ip_addr), &(netif->netmask))) {
/* return netif on which to forward IP packet */
return netif;
}
}
}
if ((netif_default == NULL) || (!netif_is_up(netif_default))) {
LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
ip4_addr1_16(dest), ip4_addr2_16(dest), ip4_addr3_16(dest), ip4_addr4_16(dest)));
IP_STATS_INC(ip.rterr);
snmp_inc_ipoutnoroutes();
return NULL;
}
/* no matching netif found, use default netif */
os_printf("ip_router %d %p\n", __LINE__, netif_default);
return netif_default;
}
#if IP_FORWARD
#if IP_NAPT
#define NO_IDX ((u16_t)-1)
#define NT(x) ((x) == NO_IDX ? NULL : &ip_napt_table[x])
u16_t napt_list = NO_IDX, napt_list_last = NO_IDX, napt_free = 0;
static struct napt_table *ip_napt_table;
struct portmap_table *ip_portmap_table;
int nr_active_napt_tcp = 0, nr_active_napt_udp = 0, nr_active_napt_icmp = 0;
uint16_t ip_napt_max = 0;
uint8_t ip_portmap_max = 0;
uint32_t ip_napt_tcp_timeout = IP_NAPT_TIMEOUT_MS_TCP;
uint32_t ip_napt_udp_timeout = IP_NAPT_TIMEOUT_MS_UDP;
void ICACHE_FLASH_ATTR
ip_napt_init(uint16_t max_nat, uint8_t max_portmap)
{
u16_t i;
ip_napt_max = max_nat;
ip_portmap_max = max_portmap;
ip_napt_table = (struct napt_table*)os_zalloc(sizeof(struct napt_table[ip_napt_max]));
ip_portmap_table = (struct portmap_table*)os_zalloc(sizeof(struct portmap_table[ip_portmap_max]));
for (i = 0; i < ip_napt_max - 1; i++)
ip_napt_table[i].next = i + 1;
ip_napt_table[i].next = NO_IDX;
}
void ICACHE_FLASH_ATTR
ip_napt_enable(u32_t addr, int enable)
{
struct netif *netif;
for (netif = netif_list; netif; netif = netif->next) {
if (netif_is_up(netif) && !ip_addr_isany(&netif->ip_addr) && netif->ip_addr.addr == addr) {
netif->napt = !!enable;
break;
}
}
}
void ICACHE_FLASH_ATTR
ip_napt_enable_no(u8_t number, int enable)
{
struct netif *netif;
for (netif = netif_list; netif; netif = netif->next) {
if (netif->num == number) {
netif->napt = !!enable;
break;
}
}
}
void ICACHE_FLASH_ATTR checksumadjust(unsigned char *chksum, unsigned char *optr,
int olen, unsigned char *nptr, int nlen)
/* assuming: unsigned char is 8 bits, long is 32 bits.
- chksum points to the chksum in the packet
- optr points to the old data in the packet
- nptr points to the new data in the packet
*/
{
long x, old, new;
x=chksum[0]*256+chksum[1];
x=~x & 0xFFFF;
while (olen)
{
old=optr[0]*256+optr[1]; optr+=2;
x-=old & 0xffff;
if (x<=0) { x--; x&=0xffff; }
olen-=2;
}
while (nlen)
{
new=nptr[0]*256+nptr[1]; nptr+=2;
x+=new & 0xffff;
if (x & 0x10000) { x++; x&=0xffff; }
nlen-=2;
}
x=~x & 0xFFFF;
chksum[0]=x/256; chksum[1]=x & 0xff;
}
/* t must be indexed by napt_free */
static void ICACHE_FLASH_ATTR
ip_napt_insert(struct napt_table *t)
{
u16_t ti = t - ip_napt_table;
if (ti != napt_free) *((int*)1)=1; //DEBUG
napt_free = t->next;
t->prev = NO_IDX;
t->next = napt_list;
if (napt_list != NO_IDX)
NT(napt_list)->prev = ti;
napt_list = ti;
if (napt_list_last == NO_IDX)
napt_list_last = ti;
#if LWIP_TCP
if (t->proto == IP_PROTO_TCP)
nr_active_napt_tcp++;
#endif
#if LWIP_UDP
if (t->proto == IP_PROTO_UDP)
nr_active_napt_udp++;
#endif
#if LWIP_ICMP
if (t->proto == IP_PROTO_ICMP)
nr_active_napt_icmp++;
#endif
//os_printf("T: %d, U: %d, I: %d\r\n", nr_active_napt_tcp, nr_active_napt_udp, nr_active_napt_icmp);
}
static void ICACHE_FLASH_ATTR
ip_napt_free(struct napt_table *t)
{
u16_t ti = t - ip_napt_table;
if (ti == napt_list)
napt_list = t->next;
if (ti == napt_list_last)
napt_list_last = t->prev;
if (t->next != NO_IDX)
NT(t->next)->prev = t->prev;
if (t->prev != NO_IDX)
NT(t->prev)->next = t->next;
t->prev = NO_IDX;
t->next = napt_free;
napt_free = ti;
#if LWIP_TCP
if (t->proto == IP_PROTO_TCP)
nr_active_napt_tcp--;
#endif
#if LWIP_UDP
if (t->proto == IP_PROTO_UDP)
nr_active_napt_udp--;
#endif
#if LWIP_ICMP
if (t->proto == IP_PROTO_ICMP)
nr_active_napt_icmp--;
#endif
LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_free\n"));
napt_debug_print();
}
#if LWIP_TCP
static u8_t ICACHE_FLASH_ATTR
ip_napt_find_port(u8_t proto, u16_t port)
{
int i, next;
for (i = napt_list; i != NO_IDX; i = next) {
struct napt_table *t = &ip_napt_table[i];
next = t->next;
if (t->proto == proto && t->mport == port)
return 1;
}
return 0;
}
static struct portmap_table * ICACHE_FLASH_ATTR
ip_portmap_find(u8_t proto, u16_t mport);
static u8_t ICACHE_FLASH_ATTR
tcp_listening(u16_t port)
{
struct tcp_pcb_listen *t;
for (t = tcp_listen_pcbs.listen_pcbs; t; t = t->next)
if (t->local_port == port)
return 1;
if (ip_portmap_find(IP_PROTO_TCP, port))
return 1;
return 0;
}
#endif // LWIP_TCP
#if LWIP_UDP
static u8_t ICACHE_FLASH_ATTR
udp_listening(u16_t port)
{
struct udp_pcb *pcb;
for (pcb = udp_pcbs; pcb; pcb = pcb->next)
if (pcb->local_port == port)
return 1;
if (ip_portmap_find(IP_PROTO_UDP, port))
return 1;
return 0;
}
#endif // LWIP_UDP
static u16_t ICACHE_FLASH_ATTR
ip_napt_new_port(u8_t proto, u16_t port)
{
if (PP_NTOHS(port) >= IP_NAPT_PORT_RANGE_START && PP_NTOHS(port) <= IP_NAPT_PORT_RANGE_END)
if (!ip_napt_find_port(proto, port) && !tcp_listening(port))
return port;
for (;;) {
port = PP_HTONS(IP_NAPT_PORT_RANGE_START +
os_random() % (IP_NAPT_PORT_RANGE_END - IP_NAPT_PORT_RANGE_START + 1));
if (ip_napt_find_port(proto, port))
continue;
#if LWIP_TCP
if (proto == IP_PROTO_TCP && tcp_listening(port))
continue;
#endif // LWIP_TCP
#if LWIP_UDP
if (proto == IP_PROTO_UDP && udp_listening(port))
continue;
#endif // LWIP_UDP
return port;
}
}
static struct napt_table* ICACHE_FLASH_ATTR
ip_napt_find(u8_t proto, u32_t addr, u16_t port, u16_t mport, u8_t dest)
{
u16_t i, next;
struct napt_table *t;
LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_find\n"));
LWIP_DEBUGF(NAPT_DEBUG, ("looking up in table %s: %"U16_F".%"U16_F".%"U16_F".%"U16_F", port: %u, mport: %u\n",
(dest ? "dest" : "src"),
ip4_addr1_16(&addr), ip4_addr2_16(&addr),
ip4_addr3_16(&addr), ip4_addr4_16(&addr),
PP_HTONS(port),
PP_HTONS(mport)));
napt_debug_print();
u32_t now = sys_now();
for (i = napt_list; i != NO_IDX; i = next) {
t = NT(i);
next = t->next;
#if LWIP_TCP
if (t->proto == IP_PROTO_TCP &&
(((t->finack1 && t->finack2 || !t->synack) &&
LWIP_TIME_DIFF(now, t->last) > IP_NAPT_TIMEOUT_MS_TCP_DISCON) ||
LWIP_TIME_DIFF(now, t->last) > ip_napt_tcp_timeout)) {
ip_napt_free(t);
continue;
}
#endif
#if LWIP_UDP
if (t->proto == IP_PROTO_UDP && LWIP_TIME_DIFF(now, t->last) > ip_napt_udp_timeout) {
ip_napt_free(t);
continue;
}
#endif
#if LWIP_ICMP
if (t->proto == IP_PROTO_ICMP && LWIP_TIME_DIFF(now, t->last) > IP_NAPT_TIMEOUT_MS_ICMP) {
ip_napt_free(t);
continue;
}
#endif
if (dest == 0 && t->proto == proto && t->src == addr && t->sport == port) {
t->last = now;
LWIP_DEBUGF(NAPT_DEBUG, ("found\n"));
return t;
}
if (dest == 1 && t->proto == proto && t->dest == addr && t->dport == port
&& t->mport == mport) {
t->last = now;
LWIP_DEBUGF(NAPT_DEBUG, ("found\n"));
return t;
}
}
LWIP_DEBUGF(NAPT_DEBUG, ("not found\n"));
return NULL;
}
static u16_t ICACHE_FLASH_ATTR
ip_napt_add(u8_t proto, u32_t src, u16_t sport, u32_t dest, u16_t dport)
{
struct napt_table *t = ip_napt_find(proto, src, sport, 0, 0);
if (t) {
t->last = sys_now();
t->dest = dest;
t->dport = dport;
/* move this entry to the top of napt_list */
ip_napt_free(t);
ip_napt_insert(t);
LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_add\n"));
napt_debug_print();
return t->mport;
}
t = NT(napt_free);
if (t) {
u16_t mport = sport;
#if LWIP_TCP
if (proto == IP_PROTO_TCP)
mport = ip_napt_new_port(IP_PROTO_TCP, sport);
#endif
#if LWIP_TCP
if (proto == IP_PROTO_UDP)
mport = ip_napt_new_port(IP_PROTO_UDP, sport);
#endif
t->last = sys_now();
t->src = src;
t->dest = dest;
t->sport = sport;
t->dport = dport;
t->mport = mport;
t->proto = proto;
t->fin1 = t->fin2 = t->finack1 = t->finack2 = t->synack = t->rst = 0;
ip_napt_insert(t);
LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_add\n"));
napt_debug_print();
return mport;
}
os_printf("NAT table full\n");
return 0;
}
u8_t ICACHE_FLASH_ATTR
ip_portmap_add(u8_t proto, u32_t maddr, u16_t mport, u32_t daddr, u16_t dport)
{
mport = PP_HTONS(mport);
dport = PP_HTONS(dport);
int i;
for (i = 0; i < ip_portmap_max; i++) {
struct portmap_table *p = &ip_portmap_table[i];
if (p->valid && p->proto == proto && p->mport == mport) {
p->dport = dport;
p->daddr = daddr;
} else if (!p->valid) {
p->maddr = maddr;
p->daddr = daddr;
p->mport = mport;
p->dport = dport;
p->proto = proto;
p->valid = 1;
return 1;
}
}
return 0;
}
static struct portmap_table * ICACHE_FLASH_ATTR
ip_portmap_find(u8_t proto, u16_t mport)
{
int i;
for (i = 0; i < ip_portmap_max; i++) {
struct portmap_table *p = &ip_portmap_table[i];
if (!p->valid)
return 0;
if (p->proto == proto && p->mport == mport)
return p;
}
return NULL;
}
static struct portmap_table * ICACHE_FLASH_ATTR
ip_portmap_find_dest(u8_t proto, u16_t dport, u32_t daddr)
{
int i;
for (i = 0; i < ip_portmap_max; i++) {
struct portmap_table *p = &ip_portmap_table[i];
if (!p->valid)
return 0;
if (p->proto == proto && p->dport == dport && p->daddr == daddr)
return p;
}
return NULL;
}
u8_t ICACHE_FLASH_ATTR
ip_portmap_remove(u8_t proto, u16_t mport)
{
mport = PP_HTONS(mport);
struct portmap_table *last = &ip_portmap_table[ip_portmap_max - 1];
struct portmap_table *m = ip_portmap_find(proto, mport);
if (!m)
return 0;
for (; m != last; m++)
memcpy(m, m + 1, sizeof(*m));
last->valid = 0;
return 1;
}
#if LWIP_TCP
void ICACHE_FLASH_ATTR
ip_napt_set_tcp_timeout(u32_t secs)
{
ip_napt_tcp_timeout = secs * 1000;
}
void ICACHE_FLASH_ATTR
ip_napt_modify_port_tcp(struct tcp_hdr *tcphdr, u8_t dest, u16_t newval)
{
if (dest) {
checksumadjust((char *)&tcphdr->chksum, (char *)&tcphdr->dest, 2, (char *)&newval, 2);
tcphdr->dest = newval;
} else {
checksumadjust((char *)&tcphdr->chksum, (char *)&tcphdr->src, 2, (char *)&newval, 2);
tcphdr->src = newval;
}
}
void ICACHE_FLASH_ATTR
ip_napt_modify_addr_tcp(struct tcp_hdr *tcphdr, ip_addr_p_t *oldval, u32_t newval)
{
checksumadjust((char *)&tcphdr->chksum, (char *)&oldval->addr, 4, (char *)&newval, 4);
}
#endif // LWIP_TCP
#if LWIP_UDP
void ICACHE_FLASH_ATTR
ip_napt_set_udp_timeout(u32_t secs)
{
ip_napt_udp_timeout = secs * 1000;
}
void ICACHE_FLASH_ATTR
ip_napt_modify_port_udp(struct udp_hdr *udphdr, u8_t dest, u16_t newval)
{
if (dest) {
checksumadjust((char *)&udphdr->chksum, (char *)&udphdr->dest, 2, (char *)&newval, 2);
udphdr->dest = newval;
} else {
checksumadjust((char *)&udphdr->chksum, (char *)&udphdr->src, 2, (char *)&newval, 2);
udphdr->src = newval;
}
}
void ICACHE_FLASH_ATTR
ip_napt_modify_addr_udp(struct udp_hdr *udphdr, ip_addr_p_t *oldval, u32_t newval)
{
checksumadjust((char *)&udphdr->chksum, (char *)&oldval->addr, 4, (char *)&newval, 4);
}
#endif // LWIP_UDP
void ICACHE_FLASH_ATTR
ip_napt_modify_addr(struct ip_hdr *iphdr, ip_addr_p_t *field, u32_t newval)
{
checksumadjust((char *)&IPH_CHKSUM(iphdr), (char *)&field->addr, 4, (char *)&newval, 4);
field->addr = newval;
}
/**
* NAPT for an input packet. It checks weather the destination is on NAPT
* table and modifythe packet destination address and port if needed.
*
* @param p the packet to forward (p->payload points to IP header)
* @param iphdr the IP header of the input packet
* @param inp the netif on which this packet was received
*/
static void ICACHE_FLASH_ATTR
ip_napt_recv(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
{
struct portmap_table *m;
struct napt_table *t;
#if LWIP_ICMP
/* NAPT for ICMP Echo Request using identifier */
if (IPH_PROTO(iphdr) == IP_PROTO_ICMP) {
struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
if (iecho->type == ICMP_ER) {
t = ip_napt_find(IP_PROTO_ICMP, iphdr->src.addr, iecho->id, iecho->id, 1);
if (!t)
return;
ip_napt_modify_addr(iphdr, &iphdr->dest, t->src);
return;
}
return;
}
#endif // LWIP_ICMP
#if LWIP_TCP
if (IPH_PROTO(iphdr) == IP_PROTO_TCP) {
struct tcp_hdr *tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
LWIP_DEBUGF(NAPT_DEBUG, ("ip_napt_recv\n"));
LWIP_DEBUGF(NAPT_DEBUG, ("src: %"U16_F".%"U16_F".%"U16_F".%"U16_F", dest: %"U16_F".%"U16_F".%"U16_F".%"U16_F", ",
ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src),
ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src),
ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest),
ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest)));
LWIP_DEBUGF(NAPT_DEBUG, ("sport %u, dport: %u\n",
PP_HTONS(tcphdr->src),
PP_HTONS(tcphdr->dest)));
m = ip_portmap_find(IP_PROTO_TCP, tcphdr->dest);
if (m) {
/* packet to mapped port: rewrite destination */
if (m->dport != tcphdr->dest)
ip_napt_modify_port_tcp(tcphdr, 1, m->dport);
ip_napt_modify_addr_tcp(tcphdr, &iphdr->dest, m->daddr);
ip_napt_modify_addr(iphdr, &iphdr->dest, m->daddr);
return;
}
t = ip_napt_find(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src, tcphdr->dest, 1);
if (!t)
return; /* Unknown TCP session; do nothing */
if (t->sport != tcphdr->dest)
ip_napt_modify_port_tcp(tcphdr, 1, t->sport);
ip_napt_modify_addr_tcp(tcphdr, &iphdr->dest, t->src);
ip_napt_modify_addr(iphdr, &iphdr->dest, t->src);
if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == (TCP_SYN|TCP_ACK))
t->synack = 1;
if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
t->fin1 = 1;
if (t->fin2 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
t->finack2 = 1; /* FIXME: Currently ignoring ACK seq... */
if (TCPH_FLAGS(tcphdr) & TCP_RST)
t->rst = 1;
return;
}
#endif // LWIP_TCP
#if LWIP_UDP
if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
m = ip_portmap_find(IP_PROTO_UDP, udphdr->dest);
if (m) {
/* packet to mapped port: rewrite destination */
if (m->dport != udphdr->dest)
ip_napt_modify_port_udp(udphdr, 1, m->dport);
ip_napt_modify_addr_udp(udphdr, &iphdr->dest, m->daddr);
ip_napt_modify_addr(iphdr, &iphdr->dest, m->daddr);
return;
}
t = ip_napt_find(IP_PROTO_UDP, iphdr->src.addr, udphdr->src, udphdr->dest, 1);
if (!t)
return; /* Unknown session; do nothing */
if (t->sport != udphdr->dest)
ip_napt_modify_port_udp(udphdr, 1, t->sport);
ip_napt_modify_addr_udp(udphdr, &iphdr->dest, t->src);
ip_napt_modify_addr(iphdr, &iphdr->dest, t->src);
return;
}
#endif // LWIP_UDP
}
/**
* NAPT for a forwarded packet. It checks weather we need NAPT and modify
* the packet source address and port if needed.
*
* @param p the packet to forward (p->payload points to IP header)
* @param iphdr the IP header of the input packet
* @param inp the netif on which this packet was received
* @param outp the netif on which this packet will be sent
* @return ERR_OK if packet should be sent, or ERR_RTE if it should be dropped
*/
static err_t ICACHE_FLASH_ATTR
ip_napt_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp, struct netif *outp)
{
if (!inp->napt)
return ERR_OK;
#if LWIP_ICMP
/* NAPT for ICMP Echo Request using identifier */
if (IPH_PROTO(iphdr) == IP_PROTO_ICMP) {
struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
if (iecho->type == ICMP_ECHO) {
/* register src addr and iecho->id and dest info */
ip_napt_add(IP_PROTO_ICMP, iphdr->src.addr, iecho->id, iphdr->dest.addr, iecho->id);
ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr);
}
return ERR_OK;
}
#endif
#if LWIP_TCP
if (IPH_PROTO(iphdr) == IP_PROTO_TCP) {
struct tcp_hdr *tcphdr = (struct tcp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
u16_t mport;
struct portmap_table *m = ip_portmap_find_dest(IP_PROTO_TCP, tcphdr->src, iphdr->src.addr);
if (m) {
/* packet from port-mapped dest addr/port: rewrite source to this node */
if (m->mport != tcphdr->src)
ip_napt_modify_port_tcp(tcphdr, 0, m->mport);
ip_napt_modify_addr_tcp(tcphdr, &iphdr->src, m->maddr);
ip_napt_modify_addr(iphdr, &iphdr->src, m->maddr);
return ERR_OK;
}
if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == TCP_SYN &&
PP_NTOHS(tcphdr->src) >= 1024) {
/* Register new TCP session to NAPT */
mport = ip_napt_add(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src,
iphdr->dest.addr, tcphdr->dest);
} else {
struct napt_table *t = ip_napt_find(IP_PROTO_TCP, iphdr->src.addr, tcphdr->src, 0, 0);
if (!t || t->dest != iphdr->dest.addr || t->dport != tcphdr->dest) {
#if LWIP_ICMP
icmp_dest_unreach(p, ICMP_DUR_PORT);
#endif
return ERR_RTE; /* Drop unknown TCP session */
}
mport = t->mport;
if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
t->fin2 = 1;
if (t->fin1 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
t->finack1 = 1; /* FIXME: Currently ignoring ACK seq... */
if (TCPH_FLAGS(tcphdr) & TCP_RST)
t->rst = 1;
}
if (mport != tcphdr->src)
ip_napt_modify_port_tcp(tcphdr, 0, mport);
ip_napt_modify_addr_tcp(tcphdr, &iphdr->src, outp->ip_addr.addr);
ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr);
return ERR_OK;
}
#endif
#if LWIP_UDP
if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
struct udp_hdr *udphdr = (struct udp_hdr *)((u8_t *)p->payload + IPH_HL(iphdr) * 4);
u16_t mport;
struct portmap_table *m = ip_portmap_find_dest(IP_PROTO_UDP, udphdr->src, iphdr->src.addr);
if (m) {
/* packet from port-mapped dest addr/port: rewrite source to this node */
if (m->mport != udphdr->src)
ip_napt_modify_port_udp(udphdr, 0, m->mport);
ip_napt_modify_addr_udp(udphdr, &iphdr->src, m->maddr);
ip_napt_modify_addr(iphdr, &iphdr->src, m->maddr);
return ERR_OK;
}
if (PP_NTOHS(udphdr->src) >= 1024) {
/* Register new UDP session */
mport = ip_napt_add(IP_PROTO_UDP, iphdr->src.addr, udphdr->src,
iphdr->dest.addr, udphdr->dest);
} else {
struct napt_table *t = ip_napt_find(IP_PROTO_UDP, iphdr->src.addr, udphdr->src, 0, 0);
if (!t || t->dest != iphdr->dest.addr || t->dport != udphdr->dest) {
#if LWIP_ICMP
icmp_dest_unreach(p, ICMP_DUR_PORT);
#endif
return ERR_RTE; /* Drop unknown UDP session */
}
mport = t->mport;
}
if (mport != udphdr->src)
ip_napt_modify_port_udp(udphdr, 0, mport);
ip_napt_modify_addr_udp(udphdr, &iphdr->src, outp->ip_addr.addr);
ip_napt_modify_addr(iphdr, &iphdr->src, outp->ip_addr.addr);
return ERR_OK;
}
#endif
return ERR_OK;
}
#endif // IP_NAPT
/**
* Forwards an IP packet. It finds an appropriate route for the
* packet, decrements the TTL value of the packet, adjusts the
* checksum and outputs the packet on the appropriate interface.
*
* @param p the packet to forward (p->payload points to IP header)
* @param iphdr the IP header of the input packet
* @param inp the netif on which this packet was received
*/
static void ICACHE_FLASH_ATTR
ip_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)
{
struct netif *netif;
PERF_START;
/* RFC3927 2.7: do not forward link-local addresses */
if (ip_addr_islinklocal(¤t_iphdr_dest)) {
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
goto return_noroute;
}
/* Find network interface where to forward this IP packet to. */
netif = ip_route(¤t_iphdr_dest);
if (netif == NULL) {
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n",
ip4_addr1_16(¤t_iphdr_dest), ip4_addr2_16(¤t_iphdr_dest),
ip4_addr3_16(¤t_iphdr_dest), ip4_addr4_16(¤t_iphdr_dest)));
goto return_noroute;
}
/* Do not forward packets onto the same network interface on which
* they arrived. */
if (netif == inp
#ifdef IP_ROUTING_TAB
/* ... except if it had been routed to another gw */
&& ip_addr_cmp(¤t_ip_new_dest, ¤t_iphdr_dest)
#endif
) {
LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not bouncing packets back on incoming interface.\n"));
goto return_noroute;
}
#ifdef IP_ROUTING_TAB
/* copy it - just in case there is a new dest after routing */
ip_addr_copy(current_iphdr_dest, current_ip_new_dest);
#endif
/* decrement TTL */
IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1);