summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ppp/alias.c
blob: 5a99fb948d63b3acc846787468af884ae90a1f24 (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
/*
    Alias.c provides supervisory control for the functions of the
    packet aliasing software.  It consists of routines to monitor
    TCP connection state, protocol-specific aliasing routines,
    limited fragment handling and the two primary outside world
    functional interfaces: PacketAliasIn and PacketAliasOut.

    The other C program files are briefly described. The data
    structure framework which holds information needed to translate
    packets is encapsulated in alias_db.c.  Data is accessed by
    function calls, so other segments of the program need not
    know about the underlying data structures.  Alias_ftp.c contains
    special code for modifying the ftp PORT command used to establish
    data connections.  Alias_util.c contains a few utility routines.

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

    Version 1.0 August, 1996  (cjm)

    Version 1.1 August 20, 1996  (cjm)
        PPP host accepts incoming connections for ports 0 to 1023.

    Version 1.2 September 7, 1996 (cjm)
        Fragment handling error in alias_db.c corrected.

    Version 1.4 September 16, 1996 (cjm)
        - A more generalized method for handling incoming
          connections, without the 0-1023 restriction, is
          implemented in alias_db.c
        - Improved ICMP support in alias.c.  Traceroute
          packet streams can now be correctly aliased.
        - TCP connection closing logic simplified in
          alias.c and now allows for additional 1 minute
          "grace period" after FIN or RST is observed.
    Version 1.5 September 17, 1996 (cjm)
        Corrected error in handling incoming UDP packets with 0 checksum.
    Version 1.6 September 18, 1996 (cjm)
        Simplified ICMP aliasing scheme.  Should now support
        traceroute from Win95 as well as FreeBSD.
         
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <arpa/inet.h>

#include "alias.p"

#define FTP_CONTROL_PORT_NUMBER 21




/* TCP Handling Routines

    TcpMonitorIn()  -- These routines monitor TCP connections, and
    TcpMonitorOut() -- delete a link node when a connection is closed.

These routines look for SYN, ACK and RST flags to determine when TCP
connections open and close.  When a TCP connection closes, the data
structure containing packet aliasing information is deleted after
a timeout period.
*/

void
TcpMonitorIn(pip, link)
struct ip *pip;
char *link;
{
    struct tcphdr *tc;

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

    switch (GetStateIn(link))
    {
        case 0:
            if (tc->th_flags & TH_SYN) SetStateIn(link, 1);
            break;
        case 1:
            if (tc->th_flags & TH_FIN
             || tc->th_flags & TH_RST) SetStateIn(link, 2);
    }
}

void
TcpMonitorOut(pip, link)
struct ip *pip;
char *link;
{
    struct tcphdr *tc;

    tc = (struct tcphdr *) ((char *) pip + (pip->ip_hl << 2));
     
    switch (GetStateOut(link))
    {
        case 0:
            if (tc->th_flags & TH_SYN) SetStateOut(link, 1);
            break;
        case 1:
            if (tc->th_flags & TH_FIN
             || tc->th_flags & TH_RST) SetStateOut(link, 2);
    }
}





/* Protocol Specific Packet Aliasing Routines 

    IcmpAliasIn(), IcmpAliasIn1(), IcmpAliasIn2
    IcmpAliasOut(), IcmpAliasOut1()
    UdpAliasIn(), UdpAliasOut()
    TcpAliasIn(), TcpAliasOut()

These routines handle protocol specific details of packet aliasing.
One may observe a certain amount of repetitive arithmetic in these
functions, the purpose of which is to compute a revised checksum
without actually summing over the entire data packet, which could be
unnecessarily time consuming.

The purpose of the packet aliasing routines is to replace the source
address of the outgoing packet and then correctly put it back for
any incoming packets.  For TCP and UDP, ports are also re-mapped.

For ICMP echo/timestamp requests and replies, the following scheme
is used: the sequence number is replaced by an alias for the outgoing
packet and this sequence number, plus the id and remote address are
used to find the packet on the return path.

ICMP error messages are handled by looking at the IP fragment
in the data section of the message.

For TCP and UDP protocols, a port number is chosen for an outgoing
packet, and then incoming packets are identified by IP address and
port number.  For TCP packets, there is additional logic in the event
that sequence and ack numbers have been altered (as is the case for
FTP data port commands).

The port numbers used by the packet aliasing module are not true
ports in the Unix sense.  No sockets are actually bound to ports.
They are more correctly placeholders.

All packets are aliased, whether they come from the gateway machine
or other machines on a local area network.
*/

void
IcmpAliasIn1(pip)
struct ip *pip;
{
/*
    Un-alias incoming echo and timestamp replies
*/
    char *link;
    struct icmp *ic;

    ic = (struct icmp *) ((char *) pip + (pip->ip_hl << 2));

/* Get source address from ICMP data field and restore original data */
    link = FindIcmpIn(pip->ip_src, ic->icmp_id, ic->icmp_seq);
    if (link != NULL_PTR)
    {
        u_short original_seq;
        int accumulate;

        original_seq = GetOriginalPort(link);

/* Adjust ICMP checksum */
        accumulate = ic->icmp_cksum;
        accumulate += ic->icmp_seq;
        accumulate -= original_seq;

        if (accumulate < 0)
        {
            accumulate = -accumulate;
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            ic->icmp_cksum = (u_short) ~accumulate;
        }
        else
        {
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            ic->icmp_cksum = (u_short) accumulate;
        }

/* Put original sequence number back in */
        ic->icmp_seq = original_seq;

/* Put original address back into IP header */
        pip->ip_dst = GetOriginalAddress(link);

/* Delete unneeded data structure */
        DeleteLink(link);
    }
}

void
IcmpAliasIn2(pip)
struct ip *pip;
{
/*
    Alias incoming ICMP error messages containing
    IP header and first 64 bits of datagram.
*/
    struct ip *ip;
    struct icmp *ic, *ic2;
    struct udphdr *ud;
    struct tcphdr *tc;
    char *link;

    ic = (struct icmp *) ((char *) pip + (pip->ip_hl << 2));
    ip = (struct ip *) ic->icmp_data;

    ud = (struct udphdr *) ((char *) ip + (ip->ip_hl <<2));
    tc = (struct tcphdr *) ud;
    ic2 = (struct icmp *) ud;

    if (ip->ip_p == IPPROTO_UDP)
        link = FindUdpIn(ip->ip_dst, ud->uh_dport, ud->uh_sport);
    else if (ip->ip_p == IPPROTO_TCP)
        link = FindTcpIn(ip->ip_dst, tc->th_dport, tc->th_sport);
    else if (ip->ip_p == IPPROTO_ICMP)
        if (ic2->icmp_type == ICMP_ECHO || ic2->icmp_type == ICMP_TSTAMP)
            link = FindIcmpIn(ip->ip_dst, ic2->icmp_id, ic2->icmp_seq);
         else
            link = NULL_PTR;
    else
        link = NULL_PTR;

    if (link != NULL_PTR)
    {
        if (ip->ip_p == IPPROTO_UDP || ip->ip_p == IPPROTO_TCP)
        {
            u_short *sptr;
            int accumulate;
            struct in_addr original_address;
            u_short original_port;

            original_address = GetOriginalAddress(link);
            original_port = GetOriginalPort(link);
    
/* Adjust ICMP checksum */
            accumulate = ic->icmp_cksum;
            sptr = (u_short *) &(ip->ip_src);
            accumulate += *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &original_address;
            accumulate -= *sptr++;
            accumulate -= *sptr;
            accumulate += ud->uh_sport;
            accumulate -= original_port;

            if (accumulate < 0)
            {
                accumulate = -accumulate;
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ic->icmp_cksum = (u_short) ~accumulate;
            }
            else
            {
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ic->icmp_cksum = (u_short) accumulate;
            }

/* Un-alias address in IP header */
            pip->ip_dst = original_address;

/* Un-alias address and port number of original IP packet
fragment contained in ICMP data section */
            ip->ip_src = original_address;
            ud->uh_sport = original_port; 
        }
        else if (pip->ip_p == IPPROTO_ICMP)
        {
            u_short *sptr;
            int accumulate;
            struct in_addr original_address;
            u_short original_seq;

            original_address = GetOriginalAddress(link);
            original_seq = GetOriginalPort(link);

/* Adjust ICMP checksum */
            accumulate = ic->icmp_cksum;
            sptr = (u_short *) &(ip->ip_src);
            accumulate += *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &original_address;
            accumulate -= *sptr++;
            accumulate -= *sptr;
            accumulate += ic2->icmp_seq;
            accumulate -= original_seq;

            if (accumulate < 0)
            {
                accumulate = -accumulate;
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ic->icmp_cksum = (u_short) ~accumulate;
            }
            else
            {
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ic->icmp_cksum = (u_short) accumulate;
            }

/* Un-alias address in IP header */
            pip->ip_dst = original_address;

/* Un-alias address of original IP packet and seqence number of 
   embedded icmp datagram */
            ip->ip_src = original_address;
            ic2->icmp_seq = original_seq;
        }
    }
}

void
IcmpAliasIn(pip)
struct ip *pip;
{
    struct icmp *ic;

    ic = (struct icmp *) ((char *) pip + (pip->ip_hl << 2));

    switch (ic->icmp_type)
    {
        case ICMP_ECHOREPLY:
        case ICMP_TSTAMPREPLY:
            if (ic->icmp_code == 0)
            {
                IcmpAliasIn1(pip);
            }
            break;
        case ICMP_UNREACH:
        case ICMP_SOURCEQUENCH:
        case ICMP_TIMXCEED:
        case ICMP_PARAMPROB:
            IcmpAliasIn2(pip);
            break;
    }
}


void
IcmpAliasOut1(pip)
struct ip *pip;
{
/*
    Alias ICMP echo and timestamp packets
*/
    char *link;
    struct icmp *ic;

    ic = (struct icmp *) ((char *) pip + (pip->ip_hl << 2));

/* Save overwritten data for when echo packet returns */
    link = FindIcmpOut(pip->ip_src, pip->ip_dst, ic->icmp_id, ic->icmp_seq);
    if (link != NULL_PTR)
    {
        u_short alias_seq;
        int accumulate;

        alias_seq = GetAliasPort(link);

/* Since data field is being modified, adjust ICMP checksum */
        accumulate = ic->icmp_cksum;
        accumulate += ic->icmp_seq;
        accumulate -= alias_seq;

        if (accumulate < 0)
        {
            accumulate = -accumulate;
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            ic->icmp_cksum = (u_short) ~accumulate;
        }
        else
        {
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            ic->icmp_cksum = (u_short) accumulate;
        }

/* Alias sequence number */
        ic->icmp_seq = alias_seq;

/* Change source address */
        pip->ip_src = GetAliasAddress();
    }
}

void
IcmpAliasOut(pip)
struct ip *pip;
{
    struct icmp *ic;

    ic = (struct icmp *) ((char *) pip + (pip->ip_hl << 2));

    switch (ic->icmp_type)
    {
        case ICMP_ECHO:
        case ICMP_TSTAMP:
            if (ic->icmp_code == 0)
            {
                IcmpAliasOut1(pip);
            }
            break;
    }
}

void
UdpAliasIn(pip)
struct ip *pip;
{
    struct udphdr *ud;
    char *link;

    ud = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));

    link = FindUdpIn(pip->ip_src, ud->uh_sport, ud->uh_dport);
    if (link != NULL_PTR)
    {
        struct in_addr alias_address;
        u_short alias_port;
        int accumulate;
        u_short *sptr;

        alias_address = GetAliasAddress();
        pip->ip_dst = GetOriginalAddress(link);
        alias_port = ud->uh_dport;
        ud->uh_dport = GetOriginalPort(link);

/* If UDP checksum is not zero, then adjust since destination port */
/* is being unaliased and destination port is being altered.       */
        if (ud->uh_sum != 0)
        {
            accumulate = ud->uh_sum;
            accumulate += alias_port;
            accumulate -= ud->uh_dport;
            sptr = (u_short *) &alias_address;
            accumulate += *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &(pip->ip_dst);
            accumulate -= *sptr++;
            accumulate -= *sptr;

            if (accumulate < 0)
            {
                accumulate = -accumulate;
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ud->uh_sum = (u_short) ~accumulate;
            }
            else
            {
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ud->uh_sum = (u_short) accumulate;
            }
        }
    }
}

void
UdpAliasOut(pip)
struct ip *pip;
{
    struct udphdr *ud;
    char *link;

    ud = (struct udphdr *) ((char *) pip + (pip->ip_hl << 2));

    link = FindUdpOut(pip->ip_src, pip->ip_dst, ud->uh_sport, ud->uh_dport);
    if (link != NULL_PTR)
    {
        u_short alias_port;

        alias_port = GetAliasPort(link);

/* If UDP checksum is not zero, adjust since source port is */
/* being aliased and source address is being altered        */
        if (ud->uh_sum != 0)
        {
            struct in_addr alias_address;
            int accumulate;
            u_short *sptr;

            alias_address = GetAliasAddress();

            accumulate = ud->uh_sum;
            accumulate += ud->uh_sport;
            accumulate -= alias_port;
            sptr = (u_short *) &(pip->ip_src);
            accumulate += *sptr++;
            accumulate += *sptr;
            sptr = (u_short *) &alias_address;
            accumulate -= *sptr++;
            accumulate -= *sptr;

            if (accumulate < 0)
            {
                accumulate = -accumulate;
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ud->uh_sum = (u_short) ~accumulate;
            }
            else
            {
                accumulate = (accumulate >> 16) + (accumulate & 0xffff);
                accumulate += accumulate >> 16;
                ud->uh_sum = (u_short) accumulate;
            }
        }

/* Put alias port in TCP header */
        ud->uh_sport = alias_port;

/* Change source address */
        pip->ip_src = GetAliasAddress();
    }
}



void
TcpAliasIn(pip)
struct ip *pip;
{
    struct tcphdr *tc;
    char *link;

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

    link = FindTcpIn(pip->ip_src, tc->th_sport, tc->th_dport);
    if (link != NULL_PTR)
    {
        struct in_addr alias_address;
        u_short alias_port;
        int accumulate;
        u_short *sptr;

        alias_address = GetAliasAddress();
        pip->ip_dst = GetOriginalAddress(link);
        alias_port = tc->th_dport;
        tc->th_dport = GetOriginalPort(link);

/* Adjust TCP checksum since destination port is being unaliased */
/* and destination port is being altered.                        */
        accumulate = tc->th_sum;
        accumulate += alias_port;
        accumulate -= tc->th_dport;
        sptr = (u_short *) &alias_address;
        accumulate += *sptr++;
        accumulate += *sptr;
        sptr = (u_short *) &(pip->ip_dst);
        accumulate -= *sptr++;
        accumulate -= *sptr;

/* See if ack number needs to be modified */
        if (GetAckModified(link) == 1)
        {
            int delta;

            delta = GetDeltaAckIn(pip, link);
            if (delta != 0)
            {
                sptr = (u_short *) &tc->th_ack;
                accumulate += *sptr++;
                accumulate += *sptr;
                tc->th_ack = htonl(ntohl(tc->th_ack) - delta);
                sptr = (u_short *) &tc->th_ack;
                accumulate -= *sptr++;
                accumulate -= *sptr;
            }
        }

/* Finish checksum modification */
        if (accumulate < 0)
        {
            accumulate = -accumulate;
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            tc->th_sum = (u_short) ~accumulate;
        }
        else
        {
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            tc->th_sum = (u_short) accumulate;
        }

/* Monitor TCP connection state */
        TcpMonitorIn(pip, link);
    }
}

void
TcpAliasOut(pip)
struct ip *pip;
{
    struct tcphdr *tc;
    char *link;

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

    link = FindTcpOut(pip->ip_src, pip->ip_dst, tc->th_sport, tc->th_dport);
    if (link !=NULL_PTR)
    {
        struct in_addr alias_address;
        u_short alias_port;
        int accumulate;
        u_short *sptr;

        alias_address = GetAliasAddress();
        alias_port = GetAliasPort(link);

/* Monitor tcp connection state */
        TcpMonitorOut(pip, link);

/* Special processing for ftp connection */
        if (ntohs(tc->th_dport) == FTP_CONTROL_PORT_NUMBER
         || ntohs(tc->th_sport) == FTP_CONTROL_PORT_NUMBER)
            HandleFtpOut(pip, link);

/* Adjust TCP checksum since source port is being aliased */
/* and source address is being altered                    */
        accumulate = tc->th_sum;
        accumulate += tc->th_sport;
        accumulate -= alias_port;
        sptr = (u_short *) &(pip->ip_src);
        accumulate += *sptr++;
        accumulate += *sptr;
        sptr = (u_short *) &alias_address;
        accumulate -= *sptr++;
        accumulate -= *sptr;

/* Modify sequence number if necessary */
        if (GetAckModified(link) == 1)
        {
            int delta;

            delta = GetDeltaSeqOut(pip, link);
            if (delta != 0)
            {
                sptr = (u_short *) &tc->th_seq;
                accumulate += *sptr++;
                accumulate += *sptr;
                tc->th_seq = htonl(ntohl(tc->th_seq) + delta);
                sptr = (u_short *) &tc->th_seq;
                accumulate -= *sptr++;
                accumulate -= *sptr;
            }
        }

/* Finish up checksum calculation */
        if (accumulate < 0)
        {
            accumulate = -accumulate;
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            tc->th_sum = (u_short) ~accumulate;
        }
        else
        {
            accumulate = (accumulate >> 16) + (accumulate & 0xffff);
            accumulate += accumulate >> 16;
            tc->th_sum = (u_short) accumulate;
        }

/* Put alias address in TCP header */
        tc->th_sport = alias_port;

/* Change source address */
        pip->ip_src = GetAliasAddress();
    }
}




/* Fragment Handling

    FragmentIn()
    FragmentOut()

The packet aliasing module has a limited ability for handling IP
fragments.  If the ICMP, TCP or UDP header is in the first fragment
received, then the id number of the IP packet is saved, and other
fragments are identified according to their ID number and IP address
they were sent from.

In general, fragments seem few and far between these days.  One way
to generate them is with a ping request specifying a large data segment.
This is how the software here was tested.

In principle, out-of-order IP fragments could also be handled by saving
fragments until the header fragment came in and then sending them on
their way.  However, this violates a basic interface rule of the
aliasing module in which individual packets are sent for remapping,
and nothing is actually known about how to write these packets to a
device interface.
*/

void
FragmentIn(pip)
struct ip *pip;
{
    char *link;

    link = FindFragmentIn2(pip->ip_src);
    if (link != NULL_PTR)
        GetFragmentAddr(link, pip->ip_id, pip->ip_p, &(pip->ip_dst) );
}

void
FragmentOut(pip)
struct ip *pip;
{
    pip->ip_src = GetAliasAddress();
}








/* Outside World Access

        PacketAliasIn()
        PacketAliasOut()
*/

void
PacketAliasIn(pip)
struct ip *pip;
{
    int checksum_ok;

/* Verify initial checksum */
    if (IpChecksum(pip) == 0)
        checksum_ok = 1;
    else
        checksum_ok = 0;

    if ( (ntohs(pip->ip_off) & IP_OFFMASK) == 0 )
    {
        switch (pip->ip_p)
        {
            case IPPROTO_ICMP:
                IcmpAliasIn(pip);
                break;
            case IPPROTO_UDP:
                UdpAliasIn(pip);
                break;
            case IPPROTO_TCP:
                TcpAliasIn(pip);
                break;
        }
        if (ntohs(pip->ip_off) & IP_MF)
        {
            char *link;

            link = FindFragmentIn1(pip->ip_src);
            if (link != NULL_PTR)
                SetFragmentData(link, pip->ip_id, pip->ip_p, pip->ip_dst);
        }
    }
    else
    {
        FragmentIn(pip);
    }

/*  adjust IP checksum, if original is correct */
    if (checksum_ok == 1)
    {
        pip->ip_sum = 0;
        pip->ip_sum = IpChecksum(pip);
    }
}

void
PacketAliasOut(pip)
struct ip *pip;
{
    int checksum_ok;

    if (IpChecksum(pip) == 0)
	checksum_ok = 1;
    else
        checksum_ok = 0;

    if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0)
    {
        switch (pip->ip_p)
        {
            case IPPROTO_ICMP:
                IcmpAliasOut(pip);
                break;
            case IPPROTO_UDP:
                UdpAliasOut(pip);
                break;
            case IPPROTO_TCP:
                TcpAliasOut(pip);
                break;
        }
    }
    else
    {
        FragmentOut(pip);
    }

/* Adjust IP checksum, if original is correct */
    if (checksum_ok == 1)
    {
        pip->ip_sum = 0;
        pip->ip_sum = IpChecksum(pip);
    }
}
OpenPOWER on IntegriCloud