TDI_STATUS InfoTdiSetRoute(PIP_INTERFACE IF, PVOID Buffer, UINT BufferSize) { IP_ADDRESS Address, Netmask, Router; PIPROUTE_ENTRY Route = Buffer; AddrInitIPv4( &Address, Route->Dest ); AddrInitIPv4( &Netmask, Route->Mask ); AddrInitIPv4( &Router, Route->Gw ); if (!Buffer || BufferSize < sizeof(IPROUTE_ENTRY)) return TDI_INVALID_PARAMETER; if (IF == Loopback) { DbgPrint("Failing attempt to add route to loopback adapter\n"); return TDI_INVALID_PARAMETER; } if( Route->Type == IP_ROUTE_TYPE_ADD ) { /* Add the route */ TI_DbgPrint(DEBUG_INFO,("Adding route (%s)\n", A2S(&Address))); if (!RouterCreateRoute( &Address, &Netmask, &Router, IF, Route->Metric1)) return TDI_NO_RESOURCES; return TDI_SUCCESS; } else if( Route->Type == IP_ROUTE_TYPE_DEL ) { TI_DbgPrint(DEBUG_INFO,("Removing route (%s)\n", A2S(&Address))); if (NT_SUCCESS(RouterRemoveRoute( &Address, &Router ))) return TDI_SUCCESS; else return TDI_INVALID_PARAMETER; } return TDI_INVALID_REQUEST; }
TDI_STATUS InfoTdiSetArptableMIB(PIP_INTERFACE IF, PVOID Buffer, UINT BufferSize) { PIPARP_ENTRY ArpEntry = Buffer; IP_ADDRESS Address; PNEIGHBOR_CACHE_ENTRY NCE; if (!Buffer || BufferSize < sizeof(IPARP_ENTRY)) return TDI_INVALID_PARAMETER; AddrInitIPv4(&Address, ArpEntry->LogAddr); if ((NCE = NBLocateNeighbor(&Address, IF))) NBRemoveNeighbor(NCE); if (NBAddNeighbor(IF, &Address, ArpEntry->PhysAddr, ArpEntry->AddrSize, NUD_PERMANENT, 0)) return TDI_SUCCESS; else return TDI_INVALID_PARAMETER; }
VOID ARPReceive( PVOID Context, PIP_PACKET Packet) /* * FUNCTION: Receives an ARP packet * ARGUMENTS: * Context = Pointer to context information (IP_INTERFACE) * Packet = Pointer to packet */ { PARP_HEADER Header; IP_ADDRESS SrcAddress; IP_ADDRESS DstAddress; PCHAR SenderHWAddress, SenderProtoAddress, TargetProtoAddress; PNEIGHBOR_CACHE_ENTRY NCE; PNDIS_PACKET NdisPacket; PIP_INTERFACE Interface = (PIP_INTERFACE)Context; ULONG BytesCopied, DataSize; PCHAR DataBuffer; PAGED_CODE(); TI_DbgPrint(DEBUG_ARP, ("Called.\n")); Packet->Header = ExAllocatePoolWithTag(PagedPool, sizeof(ARP_HEADER), PACKET_BUFFER_TAG); if (!Packet->Header) { TI_DbgPrint(DEBUG_ARP, ("Unable to allocate header buffer\n")); Packet->Free(Packet); return; } Packet->MappedHeader = FALSE; BytesCopied = CopyPacketToBuffer((PCHAR)Packet->Header, Packet->NdisPacket, Packet->Position, sizeof(ARP_HEADER)); if (BytesCopied != sizeof(ARP_HEADER)) { TI_DbgPrint(DEBUG_ARP, ("Unable to copy in header buffer\n")); Packet->Free(Packet); return; } Header = (PARP_HEADER)Packet->Header; /* FIXME: Ethernet only */ if (WN2H(Header->HWType) != 1) { TI_DbgPrint(DEBUG_ARP, ("Unknown ARP hardware type (0x%X).\n", WN2H(Header->HWType))); Packet->Free(Packet); return; } /* Check protocol type */ if (Header->ProtoType != ETYPE_IPv4) { TI_DbgPrint(DEBUG_ARP, ("Unknown ARP protocol type (0x%X).\n", WN2H(Header->ProtoType))); Packet->Free(Packet); return; } DataSize = (2 * Header->HWAddrLen) + (2 * Header->ProtoAddrLen); DataBuffer = ExAllocatePool(PagedPool, DataSize); if (!DataBuffer) { TI_DbgPrint(DEBUG_ARP, ("Unable to allocate data buffer\n")); Packet->Free(Packet); return; } BytesCopied = CopyPacketToBuffer(DataBuffer, Packet->NdisPacket, Packet->Position + sizeof(ARP_HEADER), DataSize); if (BytesCopied != DataSize) { TI_DbgPrint(DEBUG_ARP, ("Unable to copy in data buffer\n")); ExFreePool(DataBuffer); Packet->Free(Packet); return; } SenderHWAddress = (PVOID)(DataBuffer); SenderProtoAddress = (PVOID)(SenderHWAddress + Header->HWAddrLen); TargetProtoAddress = (PVOID)(SenderProtoAddress + Header->ProtoAddrLen + Header->HWAddrLen); AddrInitIPv4(&DstAddress, *((PULONG)TargetProtoAddress)); if (!AddrIsEqual(&DstAddress, &Interface->Unicast)) { ExFreePool(DataBuffer); Packet->Free(Packet); return; } AddrInitIPv4(&SrcAddress, *((PULONG)SenderProtoAddress)); /* Check if we know the sender */ NCE = NBLocateNeighbor(&SrcAddress, Interface); if (NCE) { /* We know the sender. Update the hardware address and state in our neighbor address cache */ NBUpdateNeighbor(NCE, SenderHWAddress, 0); } else { /* The packet had our protocol address as target. The sender may want to communicate with us soon, so add his address to our address cache */ NBAddNeighbor(Interface, &SrcAddress, SenderHWAddress, Header->HWAddrLen, 0, ARP_COMPLETE_TIMEOUT); } if (Header->Opcode != ARP_OPCODE_REQUEST) { ExFreePool(DataBuffer); Packet->Free(Packet); return; } /* This is a request for our address. Swap the addresses and send an ARP reply back to the sender */ NdisPacket = PrepareARPPacket( Interface, Header->HWType, /* Hardware type */ Header->ProtoType, /* Protocol type */ (UCHAR)Interface->AddressLength, /* Hardware address length */ (UCHAR)Header->ProtoAddrLen, /* Protocol address length */ Interface->Address, /* Sender's (local) hardware address */ &Interface->Unicast.Address.IPv4Address,/* Sender's (local) protocol address */ SenderHWAddress, /* Target's (remote) hardware address */ SenderProtoAddress, /* Target's (remote) protocol address */ ARP_OPCODE_REPLY); /* ARP reply */ if (NdisPacket) { PC(NdisPacket)->DLComplete = ARPTransmitComplete; (*Interface->Transmit)(Interface->Context, NdisPacket, 0, SenderHWAddress, LAN_PROTO_ARP); } ExFreePool(DataBuffer); Packet->Free(Packet); }