-
Notifications
You must be signed in to change notification settings - Fork 0
/
PortMapper.c
1852 lines (1300 loc) · 37 KB
/
PortMapper.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
/*
Copyright 2001-2018 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
// Module to provide a basic Gateway between IP over AX.25 and the Internet.
// Uses WinPcap on Windows, TAP Driver on Linux
// Basically operates as a mac level bridge, with headers converted between ax.25 and Ethernet.
// ARP frames are also reformatted, and monitored to build a simple routing table.
// Apps may not use ARP (MSYS is configured with an ax.25 default route, rather than an IP one),
// so the default route must be configured.
// Intended as a gateway for legacy apps, rather than a full function ip over ax.25 router.
// Suggested config is to use the Internet Ethernet Adapter, behind a NAT/PAT Router.
// The ax.25 applications will appear as single addresses on the Ethernet LAN
// The code can also switch packets between ax.25 interfaces
// First Version, July 2008
// Version 1.2.1 January 2009
// Add IP Address Mapping option
// June 2014. Convert to Router instead of MAC Bridge, and include a RIP44 decoder
// so packets can be routed from RF to/from encapsulated 44 net subnets.
// Routes may also be learned from received RF packets, or added from config file
/*
TODo ?Multiple Adapters
*/
// ip tuntap add dev bpqtap mode tap
// ifconfig bpqtap 44.131.4.19 mtu 256 up
#pragma data_seg("_BPQDATA")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#include <stdio.h>
#include <time.h>
#include "CHeaders.h"
#include "IPCode.h"
#ifdef WIN32
#include "pcap.h"
#endif
#ifndef LINBPQ
#include "kernelresource.h"
LRESULT CALLBACK ResWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
#endif
//#define s_addr S_un.S_addr
extern BPQVECSTRUC * IPHOSTVECTORPTR;
BOOL APIENTRY Send_AX(PMESSAGE Block, DWORD Len, UCHAR Port);
VOID SENDSABM(struct _LINKTABLE * LINK);
BOOL FindLink(UCHAR * LinkCall, UCHAR * OurCall, int Port, struct _LINKTABLE ** REQLINK);
BOOL ProcessConfig();
VOID RemoveARP(PARPDATA Arp);
VOID ProcessTunnelMsg(PIPMSG IPptr);
VOID ProcessRIP44Message(PIPMSG IPptr);
PROUTEENTRY LookupRoute(ULONG IPADDR, ULONG Mask, BOOL Add, BOOL * Found);
BOOL ProcessROUTELine(char * buf, BOOL Locked);
VOID DoRouteTimer();
PROUTEENTRY FindRoute(ULONG IPADDR);
VOID SendIPtoEncap(PIPMSG IPptr, ULONG Encap);
USHORT Generate_CHECKSUM(VOID * ptr1, int Len);
static VOID MapRouteIPMsg(PIPMSG IPptr);
BOOL Check_Checksum(VOID * ptr1, int Len);
static BOOL Send_ETH(VOID * Block, DWORD len);
VOID ProcessEthARPMsg(PETHARP arpptr);
static VOID SendARPMsg(PARPDATA Arp);
VOID SendICMPTimeExceeded(PIPMSG IPptr);
#define ARPTIMEOUT 3600
// ARP REQUEST/REPLY (Eth)
static ETHARP ETHARPREQMSG = {0};
static ARPDATA ** ARPRecords = NULL; // ARP Table - malloc'ed as needed
static int NumberofARPEntries = 0;
static ROUTEENTRY ** RouteRecords = NULL;
static int NumberofRoutes = 0;
//HANDLE hBPQNET = INVALID_HANDLE_VALUE;
static ULONG OurIPAddr = 0;
static ULONG OurIPBroadcast = 0;
static ULONG OurNetMask = 0xffffffff;
static BOOL WantTAP = FALSE;
static BOOL WantEncap = 0; // Run RIP44 and Net44 Encap
static int IPTTL = 128;
static int FramesForwarded = 0;
static int FramesDropped = 0;
static int ARPTimeouts = 0;
static int SecTimer = 10;
static BOOL NeedResolver = FALSE;
static HMENU hMenu;
extern HMENU hWndMenu;
static HMENU hPopMenu;
extern HKEY REGTREE;
extern int OffsetH, OffsetW;
extern HMENU hMainFrameMenu, hBaseMenu;
extern HWND ClientWnd, FrameWnd;
static int map_table_len = 0;
//int index=0; // pointer for table search
static int ResolveIndex=-1; // pointer to entry being resolved
static struct map_table_entry map_table[MAX_ENTRIES];
static int Windowlength, WindowParam;
static time_t ltime,lasttime;
static char ConfigClassName[]="CONFIG";
HWND hIPResWnd = 0;
BOOL IPMinimized;
extern char * PortConfig[];
static int baseline=0;
static unsigned char hostaddr[64];
// Following two fields used by stats to get round shared memmory problem
static ARPDATA Arp={0};
static int ARPFlag = -1;
// Following Buffer is used for msgs from WinPcap. Put the Enet message part way down the buffer,
// so there is room for ax.25 header instead of Enet header when we route the frame to ax.25
// Enet Header ia 14 bytes, AX.25 UI is 16
// Also used to reassemble NOS Fragmented ax.25 packets
static UCHAR Buffer[4096] = {0};
#define EthOffset 30 // Should be plenty
static DWORD IPLen = 0;
#ifdef WIN32
static UCHAR ourMACAddr[6] = {02,'B','P','Q',3,48};
#else
UCHAR ourMACAddr[6] = {02,'B','P','Q',0,1};
#endif
static LONG DefaultIPAddr = 0;
static IPSTATS IPStats = {0};
static UCHAR BPQDirectory[260];
static char ARPFN[MAX_PATH];
static HANDLE handle;
#ifdef WIN32
static pcap_t *adhandle = 0;
static pcap_t * (FAR * pcap_open_livex)(const char *, int, int, int, char *);
static int pcap_reopen_delay;
#endif
static char Adapter[256];
static int Promiscuous = 1; // Default to Promiscuous
#ifdef WIN32
static HINSTANCE PcapDriver=0;
typedef int (FAR *FARPROCX)();
static int (FAR * pcap_sendpacketx)();
static FARPROCX pcap_compilex;
static FARPROCX pcap_setfilterx;
static FARPROCX pcap_datalinkx;
static FARPROCX pcap_next_exx;
static FARPROCX pcap_geterrx;
static char Dllname[6]="wpcap";
FARPROCX GetAddress(char * Proc);
#else
#define pcap_compilex pcap_compile
#define pcap_open_livex pcap_open_live
#define pcap_setfilterx pcap_setfilter
#define pcap_datalinkx pcap_datalink
#define pcap_next_exx pcap_next_ex
#define pcap_geterrx pcap_geterr
#define pcap_sendpacketx pcap_sendpacket
#endif
VOID __cdecl Debugprintf(const char * format, ...);
static HANDLE hInstance;
void OpenTAP();
Dll BOOL APIENTRY Init_PM()
{
ARPDATA * ARPptr;
if (hIPResWnd)
{
PostMessage(hIPResWnd, WM_CLOSE,0,0);
// DestroyWindow(hIPResWnd);
Debugprintf("IP Init Destroying IP Resolver");
}
hIPResWnd= NULL;
ARPRecords = NULL; // ARP Table - malloc'ed as needed
NumberofARPEntries=0;
RouteRecords = NULL;
NumberofRoutes = 0;
ReadConfigFile();
// Clear old packets
memset(ETHARPREQMSG.MSGHDDR.DEST, 255, 6);
memcpy(ETHARPREQMSG.MSGHDDR.SOURCE, ourMACAddr, 6);
ETHARPREQMSG.MSGHDDR.ETYPE = 0x0608; // ARP
ETHARPREQMSG.HWTYPE=0x0100; // Eth
ETHARPREQMSG.PID=0x0008;
ETHARPREQMSG.HWADDRLEN = 6;
ETHARPREQMSG.IPADDRLEN = 4;
#ifdef WIN32
//
// Open PCAP Driver
if (Adapter[0]) // Don't have to have ethernet, if used just as ip over ax.25 switch
{
char buf[80];
if (OpenPCAP())
sprintf(buf,"Portmapper Using %s\n", Adapter);
else
sprintf(buf," Portmapper Unable to open %s\n", Adapter);
WritetoConsoleLocal(buf);
if (adhandle == NULL)
{
WritetoConsoleLocal("Failed to open pcap device - Portmapper Disabled\n");
return FALSE;
}
// Allocate ARP Entry for Default Gateway, and send ARP for it
if (DefaultIPAddr)
{
ARPptr = AllocARPEntry();
if (ARPptr != NULL)
{
ARPptr->ARPINTERFACE = 255;
ARPptr->ARPTYPE = 'E';
ARPptr->IPADDR = DefaultIPAddr;
ARPptr->LOCKED = TRUE;
SendARPMsg(ARPptr);
}
}
}
#else
// Linux - if TAP requested, open it
#ifndef MACBPQ
if (WantTAP)
OpenTAP();
#endif
#endif
#ifndef LINBPQ
if (NeedResolver)
{
WNDCLASS wc;
int i;
char WindowTitle[100];
int retCode, Type, Vallen;
HKEY hKey;
char Size[80];
RECT Rect = {0,0,0,0};
retCode = RegOpenKeyEx (REGTREE, "SOFTWARE\\G8BPQ\\BPQ32", 0, KEY_QUERY_VALUE, &hKey);
if (retCode == ERROR_SUCCESS)
{
Vallen=80;
retCode = RegQueryValueEx(hKey,"IPResSize",0,
(ULONG *)&Type,(UCHAR *)&Size,(ULONG *)&Vallen);
if (retCode == ERROR_SUCCESS)
sscanf(Size,"%d,%d,%d,%d,%d",&Rect.left,&Rect.right,&Rect.top,&Rect.bottom, &IPMinimized);
if (Rect.top < - 500 || Rect.left < - 500)
{
Rect.left = 0;
Rect.top = 0;
Rect.right = 600;
Rect.bottom = 400;
}
RegCloseKey(hKey);
}
// Fill in window class structure with parameters that describe
// the main window.
wc.style = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE;
wc.lpfnWndProc = (WNDPROC)ResWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(BPQICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL ;
wc.lpszClassName = "IPAppName";
// Register the window classes
RegisterClass(&wc);
i=GetLastError();
Windowlength=(map_table_len)*14+100;
WindowParam=WS_OVERLAPPEDWINDOW | WS_VSCROLL;
sprintf(WindowTitle,"PortM Resolver");
hIPResWnd = CreateMDIWindow("IPAppName", WindowTitle, WindowParam,
Rect.left - (OffsetW /2), Rect.top - OffsetH, Rect.right - Rect.left, Rect.bottom - Rect.top,
ClientWnd, hInstance, 1234);
hPopMenu = CreatePopupMenu();
AppendMenu(hPopMenu, MF_STRING, BPQREREAD, "ReRead Config");
SetScrollRange(hIPResWnd,SB_VERT,0, map_table_len,TRUE);
if (IPMinimized)
ShowWindow(hIPResWnd, SW_SHOWMINIMIZED);
else
ShowWindow(hIPResWnd, SW_RESTORE);
_beginthread(IPResolveNames, 0, NULL );
}
#endif
WritetoConsoleLocal("Portmapper Enabled\n");
return TRUE;
}
VOID PMClose()
{
}
union
{
struct sockaddr_in rxaddr;
struct sockaddr_in6 rxaddr6;
} RXaddr;
Dll BOOL APIENTRY Poll_PM()
{
int res;
struct pcap_pkthdr *header;
const u_char *pkt_data;
// Entered every 100 mS
// if ARPFlag set, copy requested ARP record (For BPQStatus GUI)
if (ARPFlag != -1)
{
memcpy(&Arp, ARPRecords[ARPFlag], sizeof (ARPDATA));
ARPFlag = -1;
}
SecTimer--;
if (SecTimer == 0)
{
SecTimer = 10;
DoARPTimer();
DoRouteTimer();
}
Pollloop:
#ifdef WIN32
if (adhandle)
{
res = pcap_next_exx(adhandle, &header, &pkt_data);
if (res > 0)
{
PETHMSG ethptr = (PETHMSG)&Buffer[EthOffset];
if (header->len > 1514)
{
// Debugprintf("Ether Packet Len = %d", header->len);
goto Pollloop;
}
memcpy(&Buffer[EthOffset],pkt_data, header->len);
if (ethptr->ETYPE == 0x0008)
{
ProcessEthIPMsg((PETHMSG)&Buffer[EthOffset]);
// PIPMSG ipptr = (PIPMSG)&Buffer[EthOffset+14];
// ProcessIPMsg(ipptr, ethptr->SOURCE, 'E', 255);
goto Pollloop;
}
if (ethptr->ETYPE == 0x0608)
{
ProcessEthARPMsg((PETHARP)ethptr);
goto Pollloop;
}
// Ignore anything else
goto Pollloop;
}
else
{
if (res < 0)
{
char * error = (char *)pcap_geterrx(adhandle) ;
Debugprintf(error);
if (OpenPCAP() == FALSE)
pcap_reopen_delay = 300;
}
}
}
else
{
// No handle.
if (Adapter[0]) // Don't have to have ethernet, if used just as ip over ax.25 switch
{
// Try reopening periodically
pcap_reopen_delay --;
if (pcap_reopen_delay < 0)
if (OpenPCAP() == FALSE)
pcap_reopen_delay = 300; // Retry every 30 seconds
}
}
#endif
return TRUE;
}
static BOOL Send_ETH(VOID * Block, DWORD len)
{
#ifdef WIN32
if (adhandle)
{
// if (len < 60) len = 60;
// Send down the packet
pcap_sendpacketx(adhandle, // Adapter
Block, // buffer with the packet
len); // size
}
#endif
return TRUE;
}
static VOID SendIPtoBPQDEV(PIPMSG IPptr, UCHAR * HWADDR)
{
// AX.25 headers are bigger, so there will always be room in buffer for enet header
PETHMSG Ethptr = (PETHMSG)IPptr;
int Len;
(UCHAR *)Ethptr--;
Len = ntohs(IPptr->IPLENGTH);
Len+=14; // Add eth Header
memcpy(Ethptr->DEST, HWADDR, 6);
memcpy(Ethptr->SOURCE, ourMACAddr, 6);
Ethptr->ETYPE= 0x0008;
Send_ETH(Ethptr,Len);
return;
}
static VOID ProcessEthIPMsg(PETHMSG Buffer)
{
PIPMSG ipptr = (PIPMSG)&Buffer[1];
if (memcmp(Buffer, ourMACAddr,6 ) != 0)
return; // Not for us
if (memcmp(&Buffer[6], ourMACAddr,6 ) == 0)
return; // Discard our sends
// if Checkum offload is active we get the packet before the NIC sees it (from PCAP)
if (ipptr->IPCHECKSUM == 0) // Windows seems to do this
{
// Generate IP and TCP/UDP checksums
int Len = ntohs(ipptr->IPLENGTH);
Len-=20;
ipptr->IPCHECKSUM = Generate_CHECKSUM(ipptr, 20);
if (ipptr->IPPROTOCOL == 6) // TCP
{
PTCPMSG TCP = (PTCPMSG)&ipptr->Data;
PHEADER PH = {0};
PH.IPPROTOCOL = 6;
PH.LENGTH = htons(Len);
memcpy(&PH.IPSOURCE, &ipptr->IPSOURCE, 4);
memcpy(&PH.IPDEST, &ipptr->IPDEST, 4);
TCP->CHECKSUM = ~Generate_CHECKSUM(&PH, 12);
TCP->CHECKSUM = Generate_CHECKSUM(TCP, Len);
}
}
ProcessIPMsg(ipptr, Buffer->SOURCE, 'E', 255);
}
static VOID ProcessEthARPMsg(PETHARP arpptr)
{
int i=0;
PARPDATA Arp;
BOOL Found;
if (memcmp(&arpptr->MSGHDDR.SOURCE, ourMACAddr,6 ) == 0 )
return; // Discard our sends
switch (arpptr->ARPOPCODE)
{
case 0x0100:
// We should only accept requests from our subnet - we might have more than one net on iterface
if ((arpptr->SENDIPADDR & OurNetMask) != (OurIPAddr & OurNetMask))
return;
if (arpptr->TARGETIPADDR == 0) // Request for 0.0.0.0
return;
// Add to our table, as we will almost certainly want to send back to it
Arp = LookupARP(arpptr->SENDIPADDR, TRUE, &Found);
if (Found)
goto AlreadyThere; // Already there
if (Arp == NULL) return; // No point of table full
Arp->IPADDR = arpptr->SENDIPADDR;
Arp->ARPTYPE = 'E';
Arp->ARPINTERFACE = 255;
Arp->ARPTIMER = ARPTIMEOUT;
SaveARP();
AlreadyThere:
memcpy(Arp->HWADDR, arpptr->SENDHWADDR ,6);
Arp->ARPVALID = TRUE;
if (arpptr->TARGETIPADDR == OurIPAddr)
{
ULONG Save = arpptr->TARGETIPADDR;
arpptr->ARPOPCODE = 0x0200;
memcpy(arpptr->TARGETHWADDR, arpptr->SENDHWADDR ,6);
memcpy(arpptr->SENDHWADDR, ourMACAddr ,6);
arpptr->TARGETIPADDR = arpptr->SENDIPADDR;
arpptr->SENDIPADDR = Save;
memcpy(arpptr->MSGHDDR.DEST, arpptr->MSGHDDR.SOURCE ,6);
memcpy(arpptr->MSGHDDR.SOURCE, ourMACAddr ,6);
Send_ETH(arpptr,42);
return;
}
break;
case 0x0200:
if (memcmp(&arpptr->MSGHDDR.DEST, ourMACAddr,6 ) != 0 )
return; // Not for us
// Update ARP Cache
Arp = LookupARP(arpptr->SENDIPADDR, TRUE, &Found);
if (Found)
goto Update;
if (Arp == NULL)
goto SendBack;
Update:
Arp->IPADDR = arpptr->SENDIPADDR;
memcpy(Arp->HWADDR, arpptr->SENDHWADDR ,6);
Arp->ARPTYPE = 'E';
Arp->ARPINTERFACE = 255;
Arp->ARPVALID = TRUE;
Arp->ARPTIMER = ARPTIMEOUT;
SaveARP();
SendBack:
// Send Back to Originator of ARP Request
if (arpptr->TARGETIPADDR == OurIPAddr) // Reply to our request?
break;
default:
break;
}
return;
}
static int CheckSumAndSend(PIPMSG IPptr, PTCPMSG TCPmsg, USHORT Len)
{
struct _IPMSG PH = {0};
IPptr->IPCHECKSUM = 0;
PH.IPPROTOCOL = 6;
PH.IPLENGTH = htons(Len);
memcpy(&PH.IPSOURCE, &IPptr->IPSOURCE, 4);
memcpy(&PH.IPDEST, &IPptr->IPDEST, 4);
TCPmsg->CHECKSUM = ~Generate_CHECKSUM(&PH, 20);
TCPmsg->CHECKSUM = Generate_CHECKSUM(TCPmsg, Len);
// No need to do IP checksum as RouteIPMessage doesit
// CHECKSUM IT
// IPptr->IPCHECKSUM = Generate_CHECKSUM(IPptr, 20);
MapRouteIPMsg(IPptr);
return 0;
}
static VOID ProcessIPMsg(PIPMSG IPptr, UCHAR * MACADDR, char Type, UCHAR Port)
{
ULONG Dest;
PARPDATA Arp;
BOOL Found;
int index, Len;
PTCPMSG TCPptr;
PUDPMSG UDPptr;
if (IPptr->VERLEN != 0x45) return; // Only support Type = 4, Len = 20
if (!CheckIPChecksum(IPptr)) return;
// Make sure origin ia in ARP Table
Arp = LookupARP(IPptr->IPSOURCE.addr, TRUE, &Found);
if (!Found)
{
// Add if possible
if (Arp != NULL)
{
Arp->IPADDR = IPptr->IPSOURCE.addr;
if (Type == 'E')
{
memcpy(Arp->HWADDR, MACADDR, 6);
}
else
{
memcpy(Arp->HWADDR, MACADDR, 7);
Arp->HWADDR[6] &= 0x7e;
}
Arp->ARPTYPE = Type;
Arp->ARPINTERFACE = Port;
Arp->ARPVALID = TRUE;
Arp->ARPTIMER = ARPTIMEOUT;
SaveARP();
}
}
else
Arp->ARPTIMER = ARPTIMEOUT; // Refresh
// See if for us - if not pass to router
Dest = IPptr->IPDEST.addr;
if (Dest == OurIPAddr || Dest == 0xffffffff || Dest == OurIPBroadcast)
goto ForUs;
return;
ForUs:
// if (IPptr->IPPROTOCOL == 4) // AMPRNET Tunnelled Packet
// {
// ProcessTunnelMsg(IPptr);
// return;
// }
if (IPptr->IPPROTOCOL == 1) // ICMP
{
ProcessICMPMsg(IPptr);
return;
}
// Support UDP for SNMP
if (IPptr->IPPROTOCOL == 17) // UDP
{
UDPptr = (PUDPMSG)&IPptr->Data;
if (UDPptr->DESTPORT == htons(161))
{
ProcessSNMPMessage(IPptr);
return;
}
}
// See if for a mapped Address
if (IPptr->IPPROTOCOL != 6) return; // Only TCP
TCPptr = (PTCPMSG)&IPptr->Data;
Len = ntohs(IPptr->IPLENGTH);
Len-=20;
for (index=0; index < map_table_len; index++)
{
if ((map_table[index].sourceport == TCPptr->DESTPORT) &&
map_table[index].sourceipaddr == IPptr->IPSOURCE.addr)
{
// Outgoing Message - replace Dest IP address and Port. Source Port remains unchanged
IPptr->IPSOURCE.addr = OurIPAddr;
IPptr->IPDEST.addr = map_table[index].mappedipaddr;
TCPptr->DESTPORT = map_table[index].mappedport;
CheckSumAndSend(IPptr, TCPptr, Len);
return;
}
if ((map_table[index].mappedport == TCPptr->SOURCEPORT) &&
map_table[index].mappedipaddr == IPptr->IPSOURCE.addr)
{
// Incomming Message - replace Dest IP address and Source Port
IPptr->IPSOURCE.addr = OurIPAddr;
IPptr->IPDEST.addr = map_table[index].sourceipaddr;
TCPptr->SOURCEPORT = map_table[index].sourceport;
CheckSumAndSend(IPptr, TCPptr, Len);
return;
}
}
}
static VOID ProcessICMPMsg(PIPMSG IPptr)
{
int Len;
PICMPMSG ICMPptr = (PICMPMSG)&IPptr->Data;
Len = ntohs(IPptr->IPLENGTH);
Len-=20;
Check_Checksum(ICMPptr, Len);
if (ICMPptr->ICMPTYPE == 8)
{
// ICMP_ECHO
ICMPptr->ICMPTYPE = 0; // Convert to Reply
// CHECKSUM IT
ICMPptr->ICMPCHECKSUM = 0;
ICMPptr->ICMPCHECKSUM = Generate_CHECKSUM(ICMPptr, Len);
// Swap Dest to Origin
IPptr->IPDEST = IPptr->IPSOURCE;
IPptr->IPSOURCE.addr = OurIPAddr;
IPptr->IPTTL = IPTTL;
// IPptr->IPCHECKSUM = 0;
// IPptr->IPCHECKSUM = Generate_CHECKSUM(IPptr, 20); // RouteIPMsg redoes checksum
MapRouteIPMsg(IPptr); // Send Back
}
if (ICMPptr->ICMPTYPE == 0)
{
// ICMP_REPLY:
UCHAR * BUFFER = GetBuff();
UCHAR * ptr1;
struct _MESSAGE * Msg;
TRANSPORTENTRY * Session = L4TABLE;
char IP[20];
unsigned char work[4];
Session += ICMPptr->ICMPID;
if (BUFFER == NULL)
return;
ptr1 = &BUFFER[7];
memcpy(work, &IPptr->IPSOURCE, 4);
sprintf(IP, "%d.%d.%d.%d", work[0], work[1], work[2], work[3]);
*ptr1++ = 0xf0; // PID
ptr1 += sprintf(ptr1, "Ping Response from %s", IP);
*ptr1++ = 0x0d; // CR
Len = ptr1 - BUFFER;
Msg = (struct _MESSAGE *)BUFFER;
Msg->LENGTH = Len;
Msg->CHAIN = NULL;
C_Q_ADD(&Session->L4TX_Q, (UINT *)BUFFER);
PostDataAvailable(Session);
return;
}
}
static VOID SendICMPMessage(PIPMSG IPptr, int Type, int Code, int P2)
{
PICMPMSG ICMPptr = (PICMPMSG)&IPptr->Data;
UCHAR * ptr;
if (OurIPAddr == 0)
return; // Can't do much without one
if (IPptr->IPPROTOCOL == ICMP && ICMPptr->ICMPTYPE == 11)
return; // Don't send Time Exceeded for TimeExceded
// Copy the Original IP Header and first 8 bytes of packet down the buffer
ptr = (UCHAR *) ICMPptr;
memmove(ptr + 8, IPptr, 28); // IP header plus 8 data
// We swap Souce to Dest, Convert to ICMP 11 and send back first 8 bytes of packet after header
IPptr->IPDEST = IPptr->IPSOURCE;
IPptr->IPSOURCE.addr = OurIPAddr;
IPptr->IPPROTOCOL = ICMP;
IPptr->IPTTL = IPTTL;
IPptr->FRAGWORD = 0;
IPptr->IPLENGTH = htons(56); // IP Header ICMP Header IP Header 8 Data
memset (ICMPptr, 0, 8);
ICMPptr->ICMPTYPE = Type;
ICMPptr->ICMPCODE = Code;
ICMPptr->ICMPSEQUENCE = htons(P2);
ICMPptr->ICMPCHECKSUM = Generate_CHECKSUM(ICMPptr, 36);
MapRouteIPMsg(IPptr);
}
static VOID MapRouteIPMsg(PIPMSG IPptr)
{
PARPDATA Arp;
BOOL Found;
// We rely on the ARP messages generated by either end to route frames.
// If address is not in ARP cache (say call originated by MSYS), send to our default route
// Decremnent TTL and Recalculate header checksum
IPptr->IPTTL--;
if (IPptr->IPTTL == 0)
{
SendICMPTimeExceeded(IPptr);
return; // Should we send time exceeded????
}
IPptr->IPCHECKSUM = 0;
IPptr->IPCHECKSUM = Generate_CHECKSUM(IPptr, 20);
// Look up ARP
Arp = LookupARP(IPptr->IPDEST.addr, FALSE, &Found);
// If enabled, look in Net44 Encap Routes
if (!Found && DefaultIPAddr)
Arp = LookupARP(DefaultIPAddr, FALSE, &Found);
if (!Found)
return; // No route or default