summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ppp/alias_db.c
blob: 420494bac1bffefeb173c4b6a6c9f2a67ad02526 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
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
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
/*
    Alias_db.c encapsulates all data structures used for storing
    packet aliasing data.  Other parts of the aliasing software
    access data through functions provided in this file.

    Data storage is based on the notion of a "link", which is
    established for ICMP echo/reply packets, UDP datagrams and
    TCP stream connections.  A link stores the original source
    and destination addresses.  For UDP and TCP, it also stores
    source and destination port numbers, as well as an alias
    port number.

    There is a facility for sweeping through and deleting old
    links as new packets are sent through.  A simple timeout is
    used for ICMP and UDP links.  TCP links are left alone unless
    there is an incomplete connection, in which case the link
    can be deleted after a certain amount of time.  TCP links which
    properly close are deleted by a function call to DeleteLink()
    from alias.c.


    This software is placed into the public domain with no restrictions
    on its distribution.

    Initial version: August, 1996  (cjm)

    Version 1.4: September 16, 1996 (cjm)
        Facility for handling incoming links added.

    Version 1.6: September 18, 1996 (cjm)
        ICMP data handling simplified.
*/


/* Include files */

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "defs.h"


/* Constants */

#define FTP_CONTROL_PORT_NUMBER 21
#define LINK_TABLE_SIZE 101
#define LINK_ICMP 1
#define LINK_UDP  2
#define LINK_TCP  3
#define LINK_FRAGMENT_ID 4
#define N_LINK_ICMP_DATA 20
#define N_LINK_ID_DATA 20
#define N_LINK_TCP_DATA 20

#define ICMP_EXPIRE_TIME 60
#define UDP_EXPIRE_TIME 60
#define TCP_EXPIRE_TIME 60
#define FRAGMENT_EXPIRE_TIME 60

#define ALIAS_PORT_BASE 30000
#define ALIAS_PORT_MASK 0x7fff
#define ALIAS_PORT_TABLE_SIZE 0x8000


/* If MONITOR_ALIAS is defined, a message will be printed to
/var/log/alias.log every time a link is created or deleted.  This
is useful for debugging
*/

/* #define MONITOR_ALIAS */


/* If ALLOW_INCOMING is defined, then incoming connections (e.g.
to ftp, telnet or web servers will be handled.  Otherwise, these
types of connections will be prevented by the aliasing mechanism.
*/

#define ALLOW_INCOMING



/* Data Structures 

    The fundamental data structure used in this program is
    "struct link_record".  Whenever a TCP connection is made,
    a UDP datagram is sent out, or an ICMP echo request is made,
    a link record is made (if it has not already been created).

    The link record is identified by the source address/port
    and the destination address/port. In the case of an ICMP
    echo request, which has no port numbers, these fields are
    set to zero.

    The link record also can store some auxiliary data.  For ICMP
    echo requests, there is space allocated to save id and sequence
    numbers.  For tcp connections that have had sequence and
    acknowledgment modifications, data space is available to
    track these changes.  A state field is used to keep track in
    changes to the tcp connection state.  Id numbers of fragments
    can also be stored in the auxiliary space.

    The link records are chained together and initially referenced
    from a table in order to shorten lookup time.
*/

struct id_data_record     /* used to save data about ip fragments */
{
    u_short id;
    u_char protocol;
    struct in_addr src_addr;
    int active;
};

struct id_dat
{
    int index;
    struct id_data_record data[N_LINK_ID_DATA];
};

struct ack_data_record     /* used to save changes to ack/seq numbers */
{
    u_long ack_old;
    u_long ack_new;
    int delta;
    int active;
};

struct tcp_state           /* Information about tcp connection */
{
    int in;                /* State for outside -> inside  */
    int out;               /* State for inside  -> outside */
    int index;             /* Index to ack data array */
    int ack_modified;      /* Indicates whether ack and seq numbers have
                              been modified */
};

struct tcp_dat
{
    struct tcp_state state;
    struct ack_data_record ack[N_LINK_TCP_DATA];
};

struct link_record               /* Main data structure */
{
    struct in_addr src_addr;
    struct in_addr dst_addr;
    u_short src_port;
    u_short dst_port;
    u_short alias_port;
    long timestamp;
    int passthrough;             /* set to 1 if non-aliasing link */
    int link_type;               /* Type of link: tcp, udp, icmp, frag */
    int table_index;             /* Index number in lookup table */
    int socket;                  /* Socket used to bind alias port */
    struct link_record *next;    /* Pointer to next data structure */
    struct link_record *last;    /* Pointer to previous data structure */
    union                        /* Auxiliary data */
    {
        struct id_dat   *id;
        struct tcp_dat  *tcp;
    } data;
};



/* Function Prototypes */
#include "alias.p"



/* Global Variables 

    The global variables listed here are only accessed from
    within alias_db.c.  
*/

struct in_addr aliasAddress;      /* Address written onto source */
                                  /*    field of IP packet.      */
struct link_record 
    *linkTable[LINK_TABLE_SIZE];  /* Lookup table of pointers to */
                                  /*   chains of link records.   */


char portTable[ALIAS_PORT_TABLE_SIZE];  /* Table showing which ports   */
                                        /* are in use (no distinction  */
                                        /* is made between TCP and UDP */
u_short alias_sequence;   /* used for aliasing sequence numbers of */
                          /*   ICMP packets                        */

int icmpLinkCount;        /* Storage for link statistics */
int udpLinkCount;
int tcpLinkCount;
int fragmentLinkCount;

int cleanupIndex;         /* Index to chain of link table being */
                          /*   inspected for old links          */

int monitorAlias;         /* Debug variable (set to 1 when active) */
FILE *monitorFile;



/* Internal utility routines (used only in alias_db.c)

    StartPoint     -- find lookup table start point
    GetNewPort     -- find and reserve new alias port number
    SetTimestamp   -- save last access time
    SeqDiff        -- difference between two TCP sequences
*/

int
StartPoint(dst_addr, dst_port, link_type)
struct in_addr dst_addr;
u_short dst_port;
int link_type;
{
    u_long n;

    n =  dst_addr.s_addr;
    n += dst_port;
    n += link_type;
    return(n % LINK_TABLE_SIZE);
}

u_short
GetNewPort()
{
    int i;

    for (i=0; i<20; i++)
    {
        int port_offset;

        port_offset = (int) (random() & ALIAS_PORT_MASK);
        if (portTable[port_offset] == 0)
        {
            portTable[port_offset] = 1;
            return( htons(ALIAS_PORT_BASE + port_offset) );
        }
    }
            
    fprintf(stderr,
      "PacketAlias/GetNewPort: Can't allocate port number.\n");
    return(0);
}

void
SetTimestamp(clink)
char *clink;
{
    struct link_record *link;
    struct timezone tz;
    struct timeval tv;

    if (clink != NULL_PTR)
    {
        link = (struct link_record *) clink;
        gettimeofday(&tv, &tz);
        link->timestamp = tv.tv_sec;
    }
}

int
SeqDiff(x, y)
u_long x;
u_long y;
{
/* Return the difference between two TCP sequence numbers */

/*
    This function is encapsulated in case there are any unusual
    arithmetic conditions that need to be considered.
*/

    return (ntohl(y) - ntohl(x));
}

void ShowAliasStats()
{
/* Used for debugging */

    fprintf(monitorFile,
        "ShowAliasStats: icmp=%d, udp=%d, tcp=%d, frag=%d  /  tot=%d\n",
        icmpLinkCount, udpLinkCount, tcpLinkCount, fragmentLinkCount,
        icmpLinkCount+udpLinkCount+tcpLinkCount+fragmentLinkCount);
    fflush(monitorFile);
}




/* Internal routines for finding, deleting and adding links

    CleanupAliasData()     - remove all link chains from lookup table
    IncrementalCleanup()   - look for stale links in a single chain
    FindLink1()            - find link based on original ports
    FindLink2()            - find link based on alias port
    DeleteLink()           - remove link
    AddLink()              - add link 
*/

void CleanupAliasData()
{
    struct link_record *link;
    int i, icount;

    icount = 0;
    for (i=0; i<LINK_TABLE_SIZE; i++)
    {
        link = linkTable[i];
        linkTable[i] = NULL_PTR;
        while (link != NULL_PTR)
        {
            struct link_record *link_next;
            link_next = link->next; icount++;
            DeleteLink((char *)link);
            link = link_next;
        }
    }

    icmpLinkCount = 0;
    udpLinkCount = 0;
    tcpLinkCount = 0;
    fragmentLinkCount = 0;

    cleanupIndex =0;
}

void IncrementalCleanup()
{
    int icount;
    long secs;
    struct timeval tv;
    struct timezone tz;
    struct link_record *link;

    gettimeofday(&tv, &tz);
    secs = tv.tv_sec;

    icount = 0;
    link = linkTable[cleanupIndex++];
    while (link != NULL_PTR)
    {
        long idelta;
        struct link_record *link_next;

        link_next = link->next; 
        idelta = secs - link->timestamp;
        switch (link->link_type)
        {
            case LINK_ICMP:
                if (idelta > ICMP_EXPIRE_TIME)
                {
                    DeleteLink((char *) link);
                    icount++;
                }
                break;
            case LINK_UDP:
                if (idelta > UDP_EXPIRE_TIME)
                {
                    DeleteLink((char *) link);
                    icount++;
                }
                break;
            case LINK_TCP:
                if (idelta > TCP_EXPIRE_TIME)
                {
                    struct tcp_dat *tcp_aux;

                    tcp_aux = link->data.tcp; 
                    if (tcp_aux->state.in  != 1
                     || tcp_aux->state.out != 1)
                    {
                        DeleteLink((char *) link);
                        icount++;
                    }
                }
                break;
            case LINK_FRAGMENT_ID:
                if (idelta > FRAGMENT_EXPIRE_TIME)
                {
                    DeleteLink((char *) link);
                    icount++;
                }
                break;
        }
        link = link_next;
    }

    if (cleanupIndex == LINK_TABLE_SIZE)
        cleanupIndex = 0;
}

char *
FindLink1(src_addr, dst_addr, src_port, dst_port, link_type)
struct in_addr src_addr, dst_addr;
u_short src_port, dst_port;
int link_type;
{
    int i;
    struct link_record *link;

    i = StartPoint(dst_addr, dst_port, link_type);
    link = linkTable[i];
    while (link != NULL_PTR)
    {
        if (link->src_addr.s_addr == src_addr.s_addr
         && link->dst_addr.s_addr == dst_addr.s_addr
         && link->dst_port        == dst_port
         && link->src_port        == src_port
         && link->link_type       == link_type)
        {
            break;
        }
        link = link->next;
    }
    SetTimestamp((char *) link);
    IncrementalCleanup();
    return((char *) link);
}

char *
FindLink2(dst_addr, dst_port, alias_port, link_type)
struct in_addr dst_addr;
u_short dst_port, alias_port;
int link_type;
{
    int i;
    struct link_record *link;

    i = StartPoint(dst_addr, dst_port, link_type);
    link = linkTable[i];
    while (link != NULL_PTR)
    {
        if (link->dst_addr.s_addr == dst_addr.s_addr
         && link->dst_port        == dst_port
         && link->alias_port      == alias_port
         && link->link_type       == link_type)
        {
            break;
        }
        link = link->next;
    }
    SetTimestamp((char *) link);
    IncrementalCleanup();
    return((char *) link);
}

void
DeleteLink(clink)
char *clink;
{
    struct link_record *link;
    struct link_record *link_last;
    struct link_record *link_next;

    link = (struct link_record *) clink;

    link_last = link->last;
    link_next = link->next;

    if (link_last != NULL_PTR)
        link_last->next = link_next;
    else
        linkTable[link->table_index] = link->next;

    if (link_next != NULL_PTR)
        link_next->last = link_last;

    switch(link->link_type)
    {
        int port;

        case LINK_ICMP:
            icmpLinkCount--;
            break;
        case LINK_UDP:
            udpLinkCount--;
            port = ntohs(link->alias_port);
            if (port != 0 && link->passthrough == 0)
                portTable[port - ALIAS_PORT_BASE] = 0;
            break;
        case LINK_TCP:
            tcpLinkCount--;
            port = ntohs(link->alias_port);
            if (port != 0 && link->passthrough == 0)
                portTable[port - ALIAS_PORT_BASE] = 0;
            free(link->data.tcp);
            break;
        case LINK_FRAGMENT_ID:
            fragmentLinkCount--;
            free(link->data.id);
            break;
    }

    free(link);

    if (monitorAlias == 1)
    {
        ShowAliasStats();
    }
}

char *
AddLink(src_addr, dst_addr, src_port, dst_port, alias_port, link_type)
struct in_addr src_addr, dst_addr;
u_short src_port, dst_port, alias_port;
int link_type;
{
    int i;
    struct link_record *link, *last_link;

    i = StartPoint(dst_addr, dst_port, link_type);
    last_link = NULL_PTR;
    link = linkTable[i];
    while (link != NULL_PTR)
    {
        last_link = link;
        link = link->next;
    }

    link = malloc( sizeof(struct link_record) );
    if (link != NULL_PTR)
    {
        if (last_link != NULL_PTR)
            last_link->next = link;
        else
            linkTable[i] = link;
        link->next = NULL_PTR;
        link->table_index = i;
        link->last = last_link;
        link->src_addr = src_addr;
        link->dst_addr = dst_addr;
        link->src_port = src_port;
        link->dst_port = dst_port;
        link->alias_port = 0;
        link->link_type = link_type;
        link->passthrough = 0;

        switch(link_type)
        {
            struct id_dat   *aux_id;
            struct tcp_dat  *aux_tcp;

            case LINK_ICMP:
                icmpLinkCount++;
                link->alias_port = alias_port;
                break;
            case LINK_UDP:
                udpLinkCount++;
                link->alias_port = alias_port;
                break;
            case LINK_TCP:
                link->alias_port = alias_port;
                aux_tcp = malloc( sizeof(struct tcp_dat) );
                link->data.tcp = aux_tcp;
                if (aux_tcp != NULL_PTR)
                {
                    tcpLinkCount++;
                    aux_tcp->state.in = 0;
                    aux_tcp->state.out = 0;
                    aux_tcp->state.index = 0;
                    aux_tcp->state.ack_modified = 0;
                    for (i=0; i<N_LINK_TCP_DATA; i++)
                        aux_tcp->ack[i].active = 0;
                }
                else
                {
                    fprintf(stderr,
                    "PacketAlias/AddLink:Cannot allocate auxiliary TCP data"
                    );
                }
                break;
            case LINK_FRAGMENT_ID:
                aux_id = malloc( sizeof(struct id_dat) );
                link->data.id = aux_id;
                if (aux_id != NULL_PTR)
                {
                    fragmentLinkCount++;
                    aux_id->index = 0;
                    for (i=0; i<N_LINK_ID_DATA; i++)
                        aux_id->data[i].active = 0;
                }
                else
                {
                    fprintf(stderr,
                    "PacketAlias/AddLink:Cannot allocate auxiliary id data"
                    );
                }
                break;
        }
    }
    else
    {
        fprintf(stderr,
        "PacketAlias/AddLink: Cannot dynamically allocate memory.\n");
    }

    SetTimestamp((char *) link);
    IncrementalCleanup();

    if (monitorAlias == 1)
    {
        ShowAliasStats();
    }

    return((char *) link);
}
        


/* External routines for finding/adding links

    FindIcmpIn(), FindIcmpOut()
    FindFragmentIn1(), FindFragmentIn2()
    FindUdpIn(), FindUdpOut()
    FindTcpIn(), FindTcpOut()
*/

char *
FindIcmpIn(dst_addr, id, seq_alias)
struct in_addr dst_addr;
u_short id, seq_alias;
{
    return(FindLink2(dst_addr, id, seq_alias, LINK_ICMP));
}

char *
FindIcmpOut(src_addr, dst_addr, id, seq)
struct in_addr src_addr, dst_addr;
u_short id, seq;
{
    char *link;

    link = FindLink1(src_addr, dst_addr, seq, id, LINK_ICMP);
    if (link == NULL_PTR)
        link = AddLink(src_addr, dst_addr, seq, id,
                        alias_sequence++, LINK_ICMP);
    return((char *) link);
}

char *
FindFragmentIn1(dst_addr)
struct in_addr dst_addr;
{
    char *link;

    link = FindLink2(dst_addr, 0, 0, LINK_FRAGMENT_ID);
    if (link == NULL_PTR)
        link = AddLink(aliasAddress, dst_addr, 0, 0, 0, LINK_FRAGMENT_ID);
    return(link);
}

char *
FindFragmentIn2(dst_addr)
struct in_addr dst_addr;
{
    return(FindLink2(dst_addr, 0, 0, LINK_FRAGMENT_ID));
}

char *
FindUdpIn(dst_addr, dst_port, alias_port)
struct in_addr dst_addr;
u_short dst_port, alias_port;
{
    char *link;

    link = FindLink2(dst_addr, dst_port, alias_port, LINK_UDP);

#ifdef ALLOW_INCOMING
    if (link == NULL_PTR)
        link = AddLink(GetAliasAddress(), dst_addr,
                       alias_port, dst_port, alias_port,
                       LINK_UDP);
        if (link != NULL_PTR)
            ((struct link_record *) link)->passthrough = 1;
#endif

   return(link);
}

char *
FindUdpOut(src_addr, dst_addr, src_port, dst_port)
struct in_addr src_addr, dst_addr;
u_short src_port, dst_port;
{
    char *link;

    link = FindLink1(src_addr, dst_addr, src_port, dst_port, LINK_UDP);
    if (link == NULL_PTR)
        link = AddLink(src_addr, dst_addr,
                       src_port, dst_port, GetNewPort(),
                       LINK_UDP);
    return(link);
}

char *
FindTcpIn(dst_addr, dst_port, alias_port)
struct in_addr dst_addr;
u_short dst_port, alias_port;
{
    char *link;

    link = FindLink2(dst_addr, dst_port, alias_port, LINK_TCP);

#ifdef ALLOW_INCOMING
    if (link == NULL_PTR)
        link = AddLink(GetAliasAddress(), dst_addr,
                       alias_port, dst_port, alias_port,
                       LINK_TCP);
        if (link != NULL_PTR)
            ((struct link_record *) link)->passthrough = 1;
#endif

    return(link);
}

char *
FindTcpOut(src_addr, dst_addr, src_port, dst_port)
struct in_addr src_addr, dst_addr;
u_short src_port, dst_port;
{
    char *link;

    link = FindLink1(src_addr, dst_addr, src_port, dst_port, LINK_TCP);
    if (link == NULL_PTR)
        link = AddLink(src_addr, dst_addr,
                       src_port, dst_port, GetNewPort(),
                       LINK_TCP);
    return(link);
}




/* External routines for getting or changing link data

    SetFragmentData(), GetFragmentData()
    SetStateIn(), SetStateOut(), GetStateIn(), GetStateOut()
    GetOriginalAddress(), GetDestAddress(), GetAliasAddress()
    GetOriginalPort(), GetDestPort(), GetAliasPort()
    SetAckModified(), GetAckModified()
    GetDeltaAckIn(), GetDeltaSeqOut(), AddSeq()
*/

void
SetFragmentData(clink, idnum, protocol, src_addr)
char *clink;
u_short idnum;
u_char protocol;
struct in_addr src_addr;
{
    struct link_record *link;
    struct id_dat *idx;
    int i;
    link = (struct link_record *) clink;

    idx = link->data.id;
    if (idx != NULL_PTR)
    {
        i = idx->index;
        idx->data[i].id       = idnum;
        idx->data[i].protocol = protocol;
        idx->data[i].src_addr = src_addr;
        idx->data[i].active   = 1;
        i++;
        if (i == N_LINK_ID_DATA)
            idx->index = 0;
        else
            idx->index = i;
    }
}

void
GetFragmentAddr(clink, idnum, protocol, src_addr)
char *clink;
u_short idnum;
u_char protocol;
struct in_addr *src_addr;
{
    struct link_record *link;
    int i;

    link = (struct link_record *) clink;

    for (i=0; i<N_LINK_ID_DATA; i++)
    {
        struct id_dat *idx;

        idx = link->data.id;

        if (idx->data[i].active == 1)
        {
            if (idx->data[i].id       == idnum
             && idx->data[i].protocol == protocol)
            {
                *src_addr = idx->data[i].src_addr;
                break;
            }
        }
    }
}

void
SetStateIn(clink, state)
char *clink;
int state;
{
    struct link_record *link;

/* TCP input state */
    link = (struct link_record *) clink;
    (link->data.tcp)->state.in = state;
}

void
SetStateOut(clink, state)
char *clink;
int state;
{
    struct link_record *link;

/* TCP output state */
    link = (struct link_record *) clink;
    (link->data.tcp)->state.out = state;
}

int
GetStateIn(clink)
char *clink;
{
    struct link_record *link;

/* TCP input state */
    link = (struct link_record *) clink;
    return( (link->data.tcp)->state.in);
}

int
GetStateOut(clink)
char *clink;
{
    struct link_record *link;

/* TCP output state */
    link = (struct link_record *) clink;
    return( (link->data.tcp)->state.out);
}

struct in_addr
GetOriginalAddress(clink)
char *clink;
{
    struct link_record *link;

    link = (struct link_record *) clink;
    return(link->src_addr);
}

struct in_addr
GetDestAddress(clink)
char *clink;
{
    struct link_record *link;

    link = (struct link_record *) clink;
    return(link->dst_addr);
}

struct in_addr
GetAliasAddress()
{
    return(aliasAddress);
}

u_short
GetOriginalPort(clink)
char *clink;
{
    struct link_record *link;

    link = (struct link_record *) clink;
    return(link->src_port);
}

u_short
GetDestPort(clink)
char *clink;
{
    struct link_record *link;

    link = (struct link_record *) clink;
    return(link->dst_port);
}

u_short
GetAliasPort(clink)
char *clink;
{
    struct link_record *link;

    link = (struct link_record *) clink;
    return(link->alias_port);
}

void
SetAckModified(clink)
char *clink;
{
    struct link_record *link;

/* Indicate that ack numbers have been modified in a TCP connection */
    link = (struct link_record *) clink;
    (link->data.tcp)->state.ack_modified = 1;
}

int
GetAckModified(clink)
char *clink;
{
    struct link_record *link;

/* See if ack numbers have been modified */
    link = (struct link_record *) clink;
    return( (link->data.tcp)->state.ack_modified );
}

int
GetDeltaAckIn(pip, clink)
struct ip *pip;
char *clink;
{
/*
Find out how much the ack number has been altered for an incoming
TCP packet.  To do this, a circular list is ack numbers where the TCP
packet size was altered is searched. 
*/

    int i;
    struct link_record *link;
    struct tcphdr *tc;
    int delta, ack_diff_min;
    u_long ack;

    link = (struct link_record *) clink;
    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
    ack      = tc->th_ack;

    delta = 0;
    ack_diff_min = -1;
    for (i=0; i<N_LINK_TCP_DATA; i++)
    {
        struct ack_data_record x;

        x = (link->data.tcp)->ack[i];
        if (x.active == 1)
        {
            int ack_diff;

            ack_diff = SeqDiff(x.ack_new, ack);
            if (ack_diff >= 0)
            {
                if (ack_diff_min >= 0)
                {
                    if (ack_diff < ack_diff_min)
                    {
                        delta = x.delta;
                        ack_diff_min = ack_diff;
                    }
                }
                else
                {
                    delta = x.delta;
                    ack_diff_min = ack_diff;
                }
            }
        }
    }
    return (delta);
}

int
GetDeltaSeqOut(pip, clink)
struct ip *pip;
char *clink;
{
/*
Find out how much the seq number has been altered for an outgoing
TCP packet.  To do this, a circular list is ack numbers where the TCP
packet size was altered is searched. 
*/

    int i;
    struct link_record *link;
    struct tcphdr *tc;
    int delta, seq_diff_min;
    u_long seq;

    link = (struct link_record *) clink;
    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
    seq = tc->th_seq;

    delta = 0;
    seq_diff_min = -1;
    for (i=0; i<N_LINK_TCP_DATA; i++)
    {
        struct ack_data_record x;

        x = (link->data.tcp)->ack[i];
        if (x.active == 1)
        {
            int seq_diff;

            seq_diff = SeqDiff(x.ack_old, seq);
            if (seq_diff >= 0)
            {
                if (seq_diff_min >= 0)
                {
                    if (seq_diff < seq_diff_min)
                    {
                        delta = x.delta;
                        seq_diff_min = seq_diff;
                    }
                }
                else
                {
                    delta = x.delta;
                    seq_diff_min = seq_diff;
                }
            }
        }
    }
    return (delta);
}


void
AddSeq(pip, clink, delta)
struct ip *pip;
char *clink;
int delta;
{
/*
When a TCP packet has been altered in length, save this
information in a circular list.  If enough packets have
been altered, then this list will begin to overwrite itself.
*/

    struct tcphdr *tc;
    struct link_record *link;
    struct ack_data_record x;
    int hlen, tlen, dlen;
    int i;

    link = (struct link_record *) clink;
    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));

    hlen = (pip->ip_hl + tc->th_off) << 2;
    tlen = ntohs(pip->ip_len);
    dlen = tlen - hlen;

    x.ack_old = htonl(ntohl(tc->th_seq) + dlen);
    x.ack_new = htonl(ntohl(tc->th_seq) + dlen + delta);
    x.delta = delta;
    x.active = 1;

    i = (link->data.tcp)->state.index;
    (link->data.tcp)->ack[i] = x;

    i++;
    if (i == N_LINK_TCP_DATA)
        (link->data.tcp)->state.index = 0;
    else
        (link->data.tcp)->state.index = i;
}








/* Outside world interfaces

    SetAliasAddress()
    InitAliasLog()
    InitAlias()
*/

void
SetAliasAddress(addr)
struct in_addr addr;
{
    aliasAddress = addr;
    CleanupAliasData();
    if (monitorAlias == 1)
    {
        fprintf(monitorFile,
            "SetAliasAddress: %s\n", inet_ntoa(aliasAddress));
        fflush(monitorFile);
    }
}

void
InitAliasLog()
{
    monitorAlias = 1;
    monitorFile = fopen("/var/log/alias.log", "a");
    printf("Alias log file opened.\n");
}

void
InitAlias()
{
    int i;

    for (i=0; i<LINK_TABLE_SIZE; i++)
        linkTable[i] = NULL_PTR;

    for (i=0; i<ALIAS_PORT_TABLE_SIZE; i++)
        portTable[i] = 0;

    alias_sequence = 0;

    icmpLinkCount = 0;
    udpLinkCount = 0;
    tcpLinkCount = 0;
    fragmentLinkCount = 0;
    cleanupIndex =0;
    monitorAlias = 0;

    if (mode & MODE_ALIAS)
      printf("Packet aliasing enabled.\n");

#ifdef MONITOR_ALIAS
    InitAliasLog();
#endif
}
OpenPOWER on IntegriCloud