lan.sys is a simple driver providing access to ndis. This stuff is

ripped from tcpip.sys in preparation for a big breakup.

svn path=/trunk/; revision=10884
This commit is contained in:
Art Yerkes 2004-09-17 15:50:16 +00:00
parent 865a63875c
commit 885107a541
8 changed files with 1880 additions and 0 deletions

View file

@ -0,0 +1,114 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS TCP/IP protocol driver
* FILE: include/debug.h
* PURPOSE: Debugging support macros
* DEFINES: DBG - Enable debug output
* NASSERT - Disable assertions
*/
#ifndef __DEBUG_H
#define __DEBUG_H
#define NORMAL_MASK 0x000000FF
#define SPECIAL_MASK 0xFFFFFF00
#define MIN_TRACE 0x00000001
#define MID_TRACE 0x00000002
#define MAX_TRACE 0x00000003
#define DEBUG_CHECK 0x00000100
#define DEBUG_MEMORY 0x00000200
#define DEBUG_PBUFFER 0x00000400
#define DEBUG_IRP 0x00000800
#define DEBUG_REFCOUNT 0x00001000
#define DEBUG_ADDRFILE 0x00002000
#define DEBUG_DATALINK 0x00004000
#define DEBUG_ARP 0x00008000
#define DEBUG_IP 0x00010000
#define DEBUG_UDP 0x00020000
#define DEBUG_TCP 0x00040000
#define DEBUG_ICMP 0x00080000
#define DEBUG_ROUTER 0x00100000
#define DEBUG_RCACHE 0x00200000
#define DEBUG_NCACHE 0x00400000
#define DEBUG_CPOINT 0x00800000
#define DEBUG_ULTRA 0xFFFFFFFF
#ifdef DBG
extern DWORD DebugTraceLevel;
#ifdef _MSC_VER
#define LA_DbgPrint(_t_, _x_) \
if (((DebugTraceLevel & NORMAL_MASK) >= _t_) || \
((DebugTraceLevel & _t_) > NORMAL_MASK)) { \
DbgPrint("(%s:%d) ", __FILE__, __LINE__); \
DbgPrint _x_ ; \
}
#else /* _MSC_VER */
#define LA_DbgPrint(_t_, _x_) \
if (((DebugTraceLevel & NORMAL_MASK) >= _t_) || \
((DebugTraceLevel & _t_) > NORMAL_MASK)) { \
DbgPrint("(%s:%d)(%s) ", __FILE__, __LINE__, __FUNCTION__); \
DbgPrint _x_ ; \
}
#endif /* _MSC_VER */
#if 0
#ifdef ASSERT
#undef ASSERT
#endif
#ifdef NASSERT
#define ASSERT(x)
#else /* NASSERT */
#define ASSERT(x) if (!(x)) { LA_DbgPrint(MIN_TRACE, ("Assertion "#x" failed at %s:%d\n", __FILE__, __LINE__)); KeBugCheck(0); }
#endif /* NASSERT */
#endif
#define ASSERT_IRQL(x) ASSERT(KeGetCurrentIrql() <= (x))
#else /* DBG */
#define LA_DbgPrint(_t_, _x_)
#if 0
#define ASSERT_IRQL(x)
#define ASSERT(x)
#endif
#endif /* DBG */
#define assert(x) ASSERT(x)
#define assert_irql(x) ASSERT_IRQL(x)
#ifdef _MSC_VER
#define UNIMPLEMENTED \
LA_DbgPrint(MIN_TRACE, ("The function at %s:%d is unimplemented, \
but come back another day.\n", __FILE__, __LINE__));
#else /* _MSC_VER */
#define UNIMPLEMENTED \
LA_DbgPrint(MIN_TRACE, ("(%s:%d)(%s) is unimplemented, \
but come back another day.\n", __FILE__, __LINE__, __FUNCTION__));
#endif /* _MSC_VER */
#define CHECKPOINT \
do { LA_DbgPrint(DEBUG_CHECK, ("(%s:%d)\n", __FILE__, __LINE__)); } while(0);
#define CP CHECKPOINT
#include <memtrack.h>
#endif /* __DEBUG_H */
/* EOF */

View file

@ -0,0 +1,166 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS TCP/IP protocol driver
* FILE: include/lan.h
* PURPOSE: LAN adapter definitions
*/
#ifndef __LAN_H
#define __LAN_H
/* NDIS version this driver supports */
#define NDIS_VERSION_MAJOR 4
#define NDIS_VERSION_MINOR 0
/* Macros */
#define MIN(value1, value2) \
((value1 < value2)? value1 : value2)
#define MAX(value1, value2) \
((value1 > value2)? value1 : value2)
#define NDIS_BUFFER_TAG FOURCC('n','b','u','f')
#define NDIS_PACKET_TAG FOURCC('n','p','k','t')
/* Media we support */
#define MEDIA_ETH 0
#define MAX_MEDIA 1
#define IEEE_802_ADDR_LENGTH 6
/* Ethernet header layout */
typedef struct ETH_HEADER {
UCHAR DstAddr[IEEE_802_ADDR_LENGTH]; /* Destination MAC address */
UCHAR SrcAddr[IEEE_802_ADDR_LENGTH]; /* Source MAC address */
USHORT EType; /* Ethernet protocol type */
} ETH_HEADER, *PETH_HEADER;
#define MAX_MEDIA_ETH sizeof(ETH_HEADER)
/* Broadcast masks */
#define BCAST_ETH_MASK 0x01
/* Broadcast values to check against */
#define BCAST_ETH_CHECK 0x01
/* Offset of broadcast address */
#define BCAST_ETH_OFFSET 0x00
typedef struct _LAN_ADDRESS_C {
LIST_ENTRY ListEntry;
LAN_ADDRESS ClientPart;
} LAN_ADDRESS_C, *PLAN_ADDRESS_C;
/* Per adapter information */
typedef struct LAN_ADAPTER {
LIST_ENTRY ListEntry; /* Entry on list */
LIST_ENTRY AddressList; /* Addresses associated */
LIST_ENTRY ForeignList; /* List of known addresses */
KSPIN_LOCK Lock; /* Lock for this structure */
UINT Index; /* Adapter Index */
UCHAR State; /* State of the adapter */
KEVENT Event; /* Opening event */
PVOID Context; /* Upper layer context information */
NDIS_HANDLE NdisHandle; /* NDIS binding handle */
NDIS_STATUS NdisStatus; /* NDIS status of last request */
NDIS_MEDIUM Media; /* Media type */
UCHAR HWAddress[IEEE_802_ADDR_LENGTH]; /* Local HW address */
UINT HWAddressLength; /* Length of HW address */
UCHAR BCastMask; /* Mask for checking broadcast */
UCHAR BCastCheck; /* Value to check against */
UCHAR BCastOffset; /* Offset in frame to check against */
UCHAR HeaderSize; /* Size of link-level header */
USHORT MTU; /* Maximum Transfer Unit */
UINT MinFrameSize; /* Minimum frame size in bytes */
UINT MaxPacketSize; /* Maximum packet size when sending */
UINT MaxSendPackets; /* Maximum number of packets per send */
UINT MacOptions; /* MAC options for NIC driver/adapter */
UINT Speed; /* Link speed */
UINT PacketFilter; /* Packet filter for this adapter */
UINT Lookahead; /* Lookahead for adapter */
UNICODE_STRING RegistryPath; /* Registry path for later query */
} LAN_ADAPTER, *PLAN_ADAPTER;
typedef struct _LAN_PACKET {
PNDIS_PACKET NdisPacket;
PETH_HEADER EthHeader;
UINT TotalSize;
} LAN_PACKET, *PLAN_PACKET;
typedef struct _LAN_PROTOCOL {
LIST_ENTRY ListEntry;
LIST_ENTRY ReadIrpListHead;
UINT Id;
UINT LastServicePass;
UINT Buffered;
UINT NumEtherTypes;
USHORT EtherType[1];
} LAN_PROTOCOL, *PLAN_PROTOCOL;
typedef struct _LAN_DEVICE_EXT {
NDIS_HANDLE NdisProtocolHandle;
KSPIN_LOCK Lock;
LIST_ENTRY AdapterListHead;
LIST_ENTRY ProtocolListHead;
UINT AdapterId;
UINT ProtoId;
} LAN_DEVICE_EXT, *PLAN_DEVICE_EXT;
/* LAN adapter state constants */
#define LAN_STATE_OPENING 0
#define LAN_STATE_RESETTING 1
#define LAN_STATE_STARTED 2
#define LAN_STATE_STOPPED 3
/* Size of out lookahead buffer */
#define LOOKAHEAD_SIZE 128
/* Ethernet types. We swap constants so we can compare values at runtime
without swapping them there */
#define ETYPE_IPv4 WH2N(0x0800)
#define ETYPE_IPv6 WH2N(0x86DD)
#define ETYPE_ARP WH2N(0x0806)
/* Protocols */
#define LAN_PROTO_IPv4 0x0000 /* Internet Protocol version 4 */
#define LAN_PROTO_IPv6 0x0001 /* Internet Protocol version 6 */
#define LAN_PROTO_ARP 0x0002 /* Address Resolution Protocol */
NDIS_STATUS LANRegisterAdapter(
PNDIS_STRING AdapterName,
PNDIS_STRING RegistryPath);
NDIS_STATUS LANUnregisterAdapter(PLAN_ADAPTER Adapter);
NTSTATUS LANRegisterProtocol(PNDIS_STRING Name);
VOID LANUnregisterProtocol(VOID);
NDIS_STATUS NDISCall(
PLAN_ADAPTER Adapter,
NDIS_REQUEST_TYPE Type,
NDIS_OID OID,
PVOID Buffer,
UINT Length);
void GetDataPtr( PNDIS_PACKET Packet,
UINT Offset,
PCHAR *DataOut,
PUINT Size );
NDIS_STATUS AllocatePacketWithBufferX( PNDIS_PACKET *NdisPacket,
PCHAR Data, UINT Len,
PCHAR File, UINT Line );
VOID FreeNdisPacketX( PNDIS_PACKET Packet, PCHAR File, UINT Line );
NDIS_STATUS InitNdisPools();
VOID CloseNdisPools();
PLAN_ADAPTER FindAdapterByIndex( PLAN_DEVICE_EXT DeviceExt, UINT Index );
#endif /* __LAN_H */
/* EOF */

View file

@ -0,0 +1,23 @@
#ifndef MEMTRACK_H
#define MEMTRACK_H
#ifndef FOURCC
#define FOURCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
#endif
#define AllocatePacketWithBuffer(x,y,z) AllocatePacketWithBufferX(x,y,z,__FILE__,__LINE__)
#define FreeNdisPacket(x) FreeNdisPacketX(x,__FILE__,__LINE__)
#define MTMARK()
#define Track(x,y)
#define TrackingInit()
#define TrackDump()
#define Untrack(x)
#define TrackTag(x)
#define exAllocatePoolWithTag(x,y,z) ExAllocatePoolWithTag(x,y,z)
#define exAllocatePool(x,y) ExAllocatePool(x,y)
#define exFreePool(x) ExFreePool(x)
#define TrackWithTag(w,x,y,z)
#define UntrackFL(x,y,z)
#endif/*MEMMTRAC_H*/

View file

@ -0,0 +1,15 @@
#ifndef _LAN_PRECOMP_H
#define _LAN_PRECOMP_H
#include <limits.h>
#include <ddk/ntddk.h>
#include <ddk/ndis.h>
#include <rosrtl/string.h>
#include <roscfg.h>
#include <debug.h>
#include <pseh.h>
#include "net/lan.h"
#include "lan.h"
#include "arp.h"
#endif/*_LAN_PRECOMP_H*/

View file

@ -0,0 +1,38 @@
#include <defines.h>
#include <reactos/resource.h>
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
VS_VERSION_INFO VERSIONINFO
FILEVERSION RES_UINT_FV_MAJOR,RES_UINT_FV_MINOR,RES_UINT_FV_REVISION,RES_UINT_FV_BUILD
PRODUCTVERSION RES_UINT_PV_MAJOR,RES_UINT_PV_MINOR,RES_UINT_PV_REVISION,RES_UINT_PV_BUILD
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x2L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", RES_STR_COMPANY_NAME
VALUE "FileDescription", "TCP/IP protocol driver\0"
VALUE "FileVersion", "0.0.0\0"
VALUE "InternalName", "tcpip\0"
VALUE "LegalCopyright", RES_STR_LEGAL_COPYRIGHT
VALUE "OriginalFilename", "tcpip.sys\0"
VALUE "ProductName", RES_STR_PRODUCT_NAME
VALUE "ProductVersion", RES_STR_PRODUCT_VERSION
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,149 @@
#include "precomp.h"
NDIS_HANDLE GlobalPacketPool = NULL;
NDIS_HANDLE GlobalBufferPool = NULL;
NDIS_STATUS InitNdisPools() {
NDIS_STATUS NdisStatus;
/* Last argument is extra space size */
NdisAllocatePacketPool( &NdisStatus, &GlobalPacketPool, 100, 0 );
if( !NT_SUCCESS(NdisStatus) ) return NdisStatus;
NdisAllocateBufferPool( &NdisStatus, &GlobalBufferPool, 100 );
if( !NT_SUCCESS(NdisStatus) )
NdisFreePacketPool(GlobalPacketPool);
return NdisStatus;
}
VOID CloseNdisPools() {
if( GlobalPacketPool ) NdisFreePacketPool( GlobalPacketPool );
if( GlobalBufferPool ) NdisFreeBufferPool( GlobalBufferPool );
}
__inline INT SkipToOffset(
PNDIS_BUFFER Buffer,
UINT Offset,
PCHAR *Data,
PUINT Size)
/*
* FUNCTION: Skip Offset bytes into a buffer chain
* ARGUMENTS:
* Buffer = Pointer to NDIS buffer
* Offset = Number of bytes to skip
* Data = Address of a pointer that on return will contain the
* address of the offset in the buffer
* Size = Address of a pointer that on return will contain the
* size of the destination buffer
* RETURNS:
* Offset into buffer, -1 if buffer chain was smaller than Offset bytes
* NOTES:
* Buffer may be NULL
*/
{
for (;;) {
if (!Buffer)
return -1;
NdisQueryBuffer(Buffer, (PVOID)Data, Size);
if (Offset < *Size) {
*Data = (PCHAR)((ULONG_PTR) *Data + Offset);
*Size -= Offset;
break;
}
Offset -= *Size;
NdisGetNextBuffer(Buffer, &Buffer);
}
return Offset;
}
void GetDataPtr( PNDIS_PACKET Packet,
UINT Offset,
PCHAR *DataOut,
PUINT Size ) {
PNDIS_BUFFER Buffer;
NdisQueryPacket(Packet, NULL, NULL, &Buffer, NULL);
if( !Buffer ) return;
SkipToOffset( Buffer, Offset, DataOut, Size );
}
#undef NdisAllocatePacket
#undef NdisAllocateBuffer
#undef NdisFreeBuffer
#undef NdisFreePacket
NDIS_STATUS AllocatePacketWithBufferX( PNDIS_PACKET *NdisPacket,
PCHAR Data, UINT Len,
PCHAR File, UINT Line ) {
PNDIS_PACKET Packet;
PNDIS_BUFFER Buffer;
NDIS_STATUS Status;
PCHAR NewData;
NewData = ExAllocatePool( NonPagedPool, Len );
if( !NewData ) return NDIS_STATUS_NOT_ACCEPTED; // XXX
TrackWithTag(EXALLOC_TAG, NewData, File, Line);
if( Data )
RtlCopyMemory(NewData, Data, Len);
NdisAllocatePacket( &Status, &Packet, GlobalPacketPool );
if( Status != NDIS_STATUS_SUCCESS ) {
ExFreePool( NewData );
return Status;
}
TrackWithTag(NDIS_PACKET_TAG, Packet, File, Line);
NdisAllocateBuffer( &Status, &Buffer, GlobalBufferPool, NewData, Len );
if( Status != NDIS_STATUS_SUCCESS ) {
ExFreePool( NewData );
FreeNdisPacket( Packet );
}
TrackWithTag(NDIS_BUFFER_TAG, Buffer, File, Line);
NdisChainBufferAtFront( Packet, Buffer );
*NdisPacket = Packet;
return NDIS_STATUS_SUCCESS;
}
VOID FreeNdisPacketX
( PNDIS_PACKET Packet,
PCHAR File,
UINT Line )
/*
* FUNCTION: Frees an NDIS packet
* ARGUMENTS:
* Packet = Pointer to NDIS packet to be freed
*/
{
PNDIS_BUFFER Buffer, NextBuffer;
LA_DbgPrint(DEBUG_PBUFFER, ("Packet (0x%X)\n", Packet));
/* Free all the buffers in the packet first */
NdisQueryPacket(Packet, NULL, NULL, &Buffer, NULL);
for (; Buffer != NULL; Buffer = NextBuffer) {
PVOID Data;
UINT Length;
NdisGetNextBuffer(Buffer, &NextBuffer);
NdisQueryBuffer(Buffer, &Data, &Length);
NdisFreeBuffer(Buffer);
UntrackFL(File,Line,Buffer);
ExFreePool(Data);
UntrackFL(File,Line,Data);
}
/* Finally free the NDIS packet discriptor */
NdisFreePacket(Packet);
UntrackFL(File,Line,Packet);
}

View file

@ -0,0 +1,33 @@
# $Id: makefile,v 1.1 2004/09/17 15:50:15 arty Exp $
PATH_TO_TOP = ../../..
# TARGET_REGTESTS = yes
TARGET_TYPE = driver
TARGET_NAME = lan
TARGET_PCH = include/precomp.h
# -DMEMTRACK
TARGET_CFLAGS = \
-D__USE_W32API \
-DNDIS40 \
-DMEMTRACK \
-Wall -Werror \
-I./include \
-I$(PATH_TO_TOP)/w32api/include \
-I$(PATH_TO_TOP)/include
TARGET_DDKLIBS = ndis.a \
$(PATH_TO_TOP)/dk/w32/lib/pseh.a
TARGET_CLEAN = \
lan/*.o
TARGET_OBJECTS = lan/lan.o lan/routines.o
include $(PATH_TO_TOP)/rules.mak
include $(TOOLS_PATH)/helper.mk