Fiddling with x86_64 and i486 builds
[tpg/acess2.git] / Modules / IPStack / ipv6.c
1 /*
2  * Acess2 IP Stack
3  * - IPv6 Protcol Handling
4  */
5 #include "ipstack.h"
6 #include "link.h"
7 #include "ipv6.h"
8
9 // === IMPORTS ===
10 extern tInterface       *gIP_Interfaces;
11 extern Uint32   IPv4_Netmask(int FixedBits);
12
13 // === PROTOTYPES ===
14  int    IPv6_Initialise();
15 void    IPv6_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer);
16 tInterface      *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast);
17
18 // === CODE ===
19 /**
20  * \brief Initialise the IPv6 handling code
21  */
22 int IPv6_Initialise()
23 {
24         Link_RegisterType(IPV6_ETHERNET_ID, IPv6_int_GetPacket);
25         return 1;
26 }
27
28 /**
29  * \fn void IPv6_int_GetPacket(tInterface *Interface, tMacAddr From, int Length, void *Buffer)
30  * \brief Process an IPv6 Packet
31  * \param Interface     Input interface
32  * \param From  Source MAC address
33  * \param Length        Packet length
34  * \param Buffer        Packet data
35  */
36 void IPv6_int_GetPacket(tAdapter *Interface, tMacAddr From, int Length, void *Buffer)
37 {
38         tIPv6Header     *hdr = Buffer;
39         if(Length < sizeof(tIPv6Header))        return;
40         
41         hdr->Head = ntohl(hdr->Head);
42         
43         //if( ((hdr->Head >> (20+8)) & 0xF) != 6 )
44         if( hdr->Version != 6 )
45                 return;
46         
47         Log_Debug("IPv6", "hdr = {");
48         //Log_Debug("IPv6", " .Version       = %i", (hdr->Head >> (20+8)) & 0xF );
49         //Log_Debug("IPv6", " .TrafficClass  = %i", (hdr->Head >> (20)) & 0xFF );
50         //Log_Debug("IPv6", " .FlowLabel     = %i", hdr->Head & 0xFFFFF );
51         Log_Debug("IPv6", " .Version       = %i", hdr->Version );
52         Log_Debug("IPv6", " .TrafficClass  = %i", hdr->TrafficClass );
53         Log_Debug("IPv6", " .FlowLabel     = %i", hdr->FlowLabel );
54         Log_Debug("IPv6", " .PayloadLength = 0x%04x", ntohs(hdr->PayloadLength) );
55         Log_Debug("IPv6", " .NextHeader    = 0x%02x", hdr->NextHeader );
56         Log_Debug("IPv6", " .HopLimit      = 0x%02x", hdr->HopLimit );
57         Log_Debug("IPv6", " .Source        = %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", hdr->Source );
58         Log_Debug("IPv6", " .Destination   = %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", hdr->Destination );
59         Log_Debug("IPv6", "}");
60         
61 }
62
63 /**
64  * \fn tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address)
65  * \brief Searches an adapter for a matching address
66  * \param Adapter       Source adapter
67  * \param Address       Destination Address
68  * \param Broadcast     Allow broadcast?
69  */
70 tInterface *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broadcast)
71 {
72          int    i, j;
73         tInterface      *iface = NULL;
74         Uint32  netmask;
75         
76         for( iface = gIP_Interfaces; iface; iface = iface->Next)
77         {
78                 // Check for this adapter
79                 if( iface->Adapter != Adapter ) continue;
80                 
81                 // Skip non-IPv6 Interfaces
82                 if( iface->Type != 6 )  continue;
83                 
84                 // If the address is a perfect match, return this interface
85                 if( IP6_EQU(Address, iface->IP6.Address) )      return iface;
86                 
87                 // Check if we want to match broadcast addresses
88                 if( !Broadcast )        continue;
89                 
90                 // Check for broadcast
91                 if( iface->IP6.SubnetBits > 32 && Address.L[0] != iface->IP6.Address.L[0] )
92                         continue;
93                 if( iface->IP6.SubnetBits > 64 && Address.L[1] != iface->IP6.Address.L[1] )
94                         continue;
95                 if( iface->IP6.SubnetBits > 96 && Address.L[2] != iface->IP6.Address.L[2] )
96                         continue;
97                 
98                 j = iface->IP6.SubnetBits / 32;
99                 i = iface->IP6.SubnetBits % 32;
100                 netmask = IPv4_Netmask( iface->IP6.SubnetBits % 32 );
101                 
102                 // Check the last bit of the netmask
103                 if( (Address.L[j] >> i) != (iface->IP6.Address.L[j] >> i) )     continue;
104                 
105                 // Check that the host portion is one
106                 if( (Address.L[j] & ~netmask) != (0xFFFFFFFF & ~netmask) )      continue;
107                 if( j >= 2 && Address.L[3] != 0xFFFFFFFF)       continue;
108                 if( j >= 1 && Address.L[2] != 0xFFFFFFFF)       continue;
109                 if( j >= 0 && Address.L[1] != 0xFFFFFFFF)       continue;
110                 
111                 return iface;
112         }
113         return NULL;
114 }

UCC git Repository :: git.ucc.asn.au