12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- socket.c
- Abstract:
- This module implements kernel support for sockets.
- Author:
- Evan Green 4-Apr-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- #include "iop.h"
- #include "unsocket.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- VOID
- IopDestroySocket (
- PSOCKET Socket
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Store a pointer to the core networking interface.
- //
- NET_INTERFACE IoNetInterface;
- BOOL IoNetInterfaceInitialized = FALSE;
- //
- // ------------------------------------------------------------------ Functions
- //
- KERNEL_API
- VOID
- IoInitializeCoreNetworking (
- PNET_INTERFACE Interface
- )
- /*++
- Routine Description:
- This routine initializes the interface between the kernel and the core
- networking library. This routine should not be called by random drivers.
- Arguments:
- Interface - Supplies a pointer to the core networking library interface.
- Return Value:
- None.
- --*/
- {
- ASSERT((Interface->CreateSocket != NULL) &&
- (Interface->DestroySocket != NULL) &&
- (Interface->BindToAddress != NULL) &&
- (Interface->Listen != NULL) &&
- (Interface->Accept != NULL) &&
- (Interface->Connect != NULL) &&
- (Interface->CloseSocket != NULL) &&
- (Interface->Send != NULL) &&
- (Interface->Receive != NULL) &&
- (Interface->GetSetSocketInformation != NULL) &&
- (Interface->Shutdown != NULL) &&
- (Interface->UserControl != NULL));
- if (IoNetInterfaceInitialized != FALSE) {
- ASSERT(FALSE);
- return;
- }
- RtlCopyMemory(&IoNetInterface, Interface, sizeof(NET_INTERFACE));
- IoNetInterfaceInitialized = TRUE;
- return;
- }
- KERNEL_API
- ULONG
- IoSocketAddReference (
- PSOCKET Socket
- )
- /*++
- Routine Description:
- This routine increases the reference count on a socket.
- Arguments:
- Socket - Supplies a pointer to the socket whose reference count should be
- incremented.
- Return Value:
- Returns the old reference count.
- --*/
- {
- ULONG OldReferenceCount;
- OldReferenceCount = RtlAtomicAdd32(&(Socket->ReferenceCount), 1);
- ASSERT((OldReferenceCount != 0) & (OldReferenceCount < 0x20000000));
- return OldReferenceCount;
- }
- KERNEL_API
- ULONG
- IoSocketReleaseReference (
- PSOCKET Socket
- )
- /*++
- Routine Description:
- This routine decreases the reference count of a socket, and destroys the
- socket if in this call the reference count drops to zero.
- Arguments:
- Socket - Supplies a pointer to the socket whose reference count should be
- decremented.
- Return Value:
- Returns the old reference count.
- --*/
- {
- ULONG OldReferenceCount;
- OldReferenceCount = RtlAtomicAdd32(&(Socket->ReferenceCount), -1);
- ASSERT((OldReferenceCount != 0) & (OldReferenceCount < 0x20000000));
- if (OldReferenceCount == 1) {
- IopDestroySocket(Socket);
- }
- return OldReferenceCount;
- }
- KERNEL_API
- KSTATUS
- IoSocketCreatePair (
- NET_DOMAIN_TYPE Domain,
- NET_SOCKET_TYPE Type,
- ULONG Protocol,
- ULONG OpenFlags,
- PIO_HANDLE IoHandles[2]
- )
- /*++
- Routine Description:
- This routine creates a pair of sockets that are connected to each other.
- Arguments:
- Domain - Supplies the network domain to use on the socket.
- Type - Supplies the socket connection type.
- Protocol - Supplies the raw protocol value used on the network.
- OpenFlags - Supplies a bitfield of open flags governing the new handles.
- See OPEN_FLAG_* definitions.
- IoHandles - Supplies an array where the two I/O handles to the connected
- sockets will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- if (Domain == NetDomainLocal) {
- Status = IopCreateUnixSocketPair(Type, Protocol, OpenFlags, IoHandles);
- } else {
- Status = STATUS_DOMAIN_NOT_SUPPORTED;
- }
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketCreate (
- NET_DOMAIN_TYPE Domain,
- NET_SOCKET_TYPE Type,
- ULONG Protocol,
- ULONG OpenFlags,
- PIO_HANDLE *IoHandle
- )
- /*++
- Routine Description:
- This routine allocates resources associated with a new socket.
- Arguments:
- Domain - Supplies the network domain to use on the socket.
- Type - Supplies the socket connection type.
- Protocol - Supplies the raw protocol value used on the network.
- OpenFlags - Supplies the open flags for the socket. See OPEN_FLAG_*
- definitions.
- IoHandle - Supplies a pointer where a pointer to the new socket's I/O
- handle will be returned.
- Return Value:
- Status code.
- --*/
- {
- SOCKET_CREATION_PARAMETERS Parameters;
- FILE_PERMISSIONS Permissions;
- PKPROCESS Process;
- PSOCKET Socket;
- KSTATUS Status;
- Process = PsGetCurrentProcess();
- Parameters.Domain = Domain;
- Parameters.Type = Type;
- Parameters.Protocol = Protocol;
- Parameters.ExistingSocket = NULL;
- Permissions = FILE_PERMISSION_ALL & ~(Process->Umask);
- Status = IopOpen(FALSE,
- NULL,
- NULL,
- 0,
- IO_ACCESS_READ | IO_ACCESS_WRITE,
- OpenFlags,
- IoObjectSocket,
- &Parameters,
- Permissions,
- IoHandle);
- if (KSUCCESS(Status)) {
- Socket = NULL;
- Status = IoGetSocketFromHandle(*IoHandle, &Socket);
- ASSERT(KSUCCESS(Status));
- ASSERT(Socket != NULL);
- Socket->IoHandle = *IoHandle;
- }
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketBindToAddress (
- BOOL FromKernelMode,
- PIO_HANDLE Handle,
- PVOID Link,
- PNETWORK_ADDRESS Address,
- PSTR Path,
- UINTN PathSize
- )
- /*++
- Routine Description:
- This routine binds the socket to the given address and starts listening for
- client requests.
- Arguments:
- FromKernelMode - Supplies a boolean indicating if the request is coming
- from kernel mode or user mode. This value affects the root path node
- to traverse for local domain sockets.
- Handle - Supplies a pointer to the socket handle to bind.
- Link - Supplies an optional pointer to a link to bind to.
- Address - Supplies a pointer to the address to bind the socket to.
- Path - Supplies an optional pointer to a path, required if the network
- address is a local socket.
- PathSize - Supplies the size of the path in bytes including the null
- terminator.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketBindToAddressEnd;
- }
- //
- // If it's a local domain socket and there's an address, bind it into the
- // file system.
- //
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketBindToAddress(FromKernelMode,
- Handle,
- Address,
- Path,
- PathSize);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.BindToAddress(Socket, Link, Address);
- }
- }
- if (!KSUCCESS(Status)) {
- goto SocketBindToAddressEnd;
- }
- SocketBindToAddressEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketListen (
- PIO_HANDLE Handle,
- ULONG BacklogCount
- )
- /*++
- Routine Description:
- This routine adds a bound socket to the list of listening sockets,
- officially allowing sockets to attempt to connect to it.
- Arguments:
- Handle - Supplies a pointer to the socket to mark as listening.
- BacklogCount - Supplies the number of attempted connections that can be
- queued before additional connections are refused.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketListenEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketListen(Socket, BacklogCount);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Listen(Socket, BacklogCount);
- }
- }
- SocketListenEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketAccept (
- PIO_HANDLE Handle,
- PIO_HANDLE *NewConnectionSocket,
- PNETWORK_ADDRESS RemoteAddress,
- PSTR *RemotePath,
- PUINTN RemotePathSize
- )
- /*++
- Routine Description:
- This routine accepts an incoming connection on a listening connection-based
- socket.
- Arguments:
- Handle - Supplies a pointer to the socket to accept a connection from.
- NewConnectionSocket - Supplies a pointer where a new socket will be
- returned that represents the accepted connection with the remote
- host.
- RemoteAddress - Supplies a pointer where the address of the connected
- remote host will be returned.
- RemotePath - Supplies a pointer where a string containing the remote path
- will be returned on success. The caller does not own this string, it is
- connected with the new socket coming out. This only applies to local
- sockets.
- RemotePathSize - Supplies a pointer where the size of the remote path in
- bytes will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- *RemotePath = NULL;
- *RemotePathSize = 0;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketAcceptEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketAccept(Socket,
- NewConnectionSocket,
- RemoteAddress,
- RemotePath,
- RemotePathSize);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Accept(Socket,
- NewConnectionSocket,
- RemoteAddress);
- }
- }
- SocketAcceptEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketConnect (
- BOOL FromKernelMode,
- PIO_HANDLE Handle,
- PNETWORK_ADDRESS Address,
- PSTR RemotePath,
- UINTN RemotePathSize
- )
- /*++
- Routine Description:
- This routine attempts to make an outgoing connection to a server.
- Arguments:
- FromKernelMode - Supplies a boolean indicating if the request is coming
- from kernel mode or user mode.
- Handle - Supplies a pointer to the socket to use for the connection.
- Address - Supplies a pointer to the address to connect to.
- RemotePath - Supplies a pointer to the path to connect to, if this is a
- local socket.
- RemotePathSize - Supplies the size of the remote path buffer in bytes,
- including the null terminator.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketConnectEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketConnect(FromKernelMode,
- Socket,
- Address,
- RemotePath,
- RemotePathSize);
- } else {
- if (RemotePathSize != 0) {
- Status = STATUS_INVALID_PARAMETER;
- goto SocketConnectEnd;
- }
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Connect(Socket, Address);
- }
- }
- SocketConnectEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketSendData (
- BOOL FromKernelMode,
- PIO_HANDLE Handle,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- )
- /*++
- Routine Description:
- This routine sends the given data buffer through the network.
- Arguments:
- FromKernelMode - Supplies a boolean indicating if the request is coming
- from kernel mode or user mode. This value affects the root path node
- to traverse for local domain sockets.
- Handle - Supplies a pointer to the socket to send the data to.
- Parameters - Supplies a pointer to the socket I/O parameters.
- IoBuffer - Supplies a pointer to the I/O buffer containing the data to
- send.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Socket = NULL;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketSendData;
- }
- if ((Parameters->SocketIoFlags & SOCKET_IO_NON_BLOCKING) != 0) {
- Parameters->TimeoutInMilliseconds = 0;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketSendData(FromKernelMode,
- Socket,
- Parameters,
- IoBuffer);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Send(FromKernelMode,
- Socket,
- Parameters,
- IoBuffer);
- }
- }
- SocketSendData:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketReceiveData (
- BOOL FromKernelMode,
- PIO_HANDLE Handle,
- PSOCKET_IO_PARAMETERS Parameters,
- PIO_BUFFER IoBuffer
- )
- /*++
- Routine Description:
- This routine is called by the user to receive data from the socket.
- Arguments:
- FromKernelMode - Supplies a boolean indicating if the request is coming
- from kernel mode or user mode. This value affects the root path node
- to traverse for local domain sockets.
- Handle - Supplies a pointer to the socket to receive data from.
- Parameters - Supplies a pointer to the socket I/O parameters.
- IoBuffer - Supplies a pointer to the I/O buffer where the received data
- will be returned.
- Return Value:
- STATUS_SUCCESS if any bytes were read.
- STATUS_TIMEOUT if the request timed out.
- STATUS_BUFFER_TOO_SMALL if the incoming datagram was too large for the
- buffer. The remainder of the datagram is discarded in this case.
- Other error codes on other failures.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Socket = NULL;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketReceiveDataEnd;
- }
- if ((Parameters->SocketIoFlags & SOCKET_IO_NON_BLOCKING) != 0) {
- Parameters->TimeoutInMilliseconds = 0;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketReceiveData(FromKernelMode,
- Socket,
- Parameters,
- IoBuffer);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Receive(FromKernelMode,
- Socket,
- Parameters,
- IoBuffer);
- }
- }
- SocketReceiveDataEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketGetSetInformation (
- PIO_HANDLE IoHandle,
- SOCKET_INFORMATION_TYPE InformationType,
- UINTN SocketOption,
- PVOID Data,
- PUINTN DataSize,
- BOOL Set
- )
- /*++
- Routine Description:
- This routine gets or sets information about the given socket.
- Arguments:
- IoHandle - Supplies a pointer to the I/O handle of the socket.
- InformationType - Supplies the socket information type category to which
- specified option belongs.
- SocketOption - Supplies the option to get or set, which is specific to the
- information type. The type of this value is generally
- SOCKET_<information_type>_OPTION.
- Data - Supplies a pointer to the data buffer where the data is either
- returned for a get operation or given for a set operation. If the
- data buffer is too small on a get operation, the truncated information
- will be copied to the data buffer and the routine will return
- STATUS_BUFFER_TOO_SMALL.
- DataSize - Supplies a pointer that on input constains the size of the data
- buffer. On output, this contains the required size of the data buffer.
- Set - Supplies a boolean indicating if this is a get operation (FALSE) or
- a set operation (TRUE).
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_INVALID_PARAMETER if the data is not appropriate for the socket
- option.
- STATUS_BUFFER_TOO_SMALL if the socket option information does not fit in
- the supplied buffer. On a get request, the data buffer will be filled
- with the truncated socket information.
- STATUS_NOT_SUPPORTED_BY_PROTOCOL if the socket option or information type
- is not supported by the socket.
- STATUS_NOT_A_SOCKET if the given handle wasn't a socket.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- ASSERT((Data == NULL) || (Data >= KERNEL_VA_START));
- Status = IoGetSocketFromHandle(IoHandle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketGetSetInformationEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketGetSetSocketInformation(Socket,
- InformationType,
- SocketOption,
- Data,
- DataSize,
- Set);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.GetSetSocketInformation(Socket,
- InformationType,
- SocketOption,
- Data,
- DataSize,
- Set);
- }
- }
- SocketGetSetInformationEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketShutdown (
- PIO_HANDLE IoHandle,
- ULONG ShutdownType
- )
- /*++
- Routine Description:
- This routine shuts down communication with a given socket.
- Arguments:
- IoHandle - Supplies a pointer to the I/O handle of the socket.
- ShutdownType - Supplies the shutdown type to perform. See the
- SOCKET_SHUTDOWN_* definitions.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_NOT_A_SOCKET if the given handle wasn't a socket.
- Other error codes on failure.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Status = IoGetSocketFromHandle(IoHandle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketShutdownEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketShutdown(Socket, ShutdownType);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.Shutdown(Socket, ShutdownType);
- }
- }
- SocketShutdownEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoSocketUserControl (
- PIO_HANDLE Handle,
- ULONG CodeNumber,
- BOOL FromKernelMode,
- PVOID ContextBuffer,
- UINTN ContextBufferSize
- )
- /*++
- Routine Description:
- This routine handles user control requests destined for a socket.
- Arguments:
- Handle - Supplies the open file handle.
- CodeNumber - Supplies the minor code of the request.
- FromKernelMode - Supplies a boolean indicating whether or not this request
- (and the buffer associated with it) originates from user mode (FALSE)
- or kernel mode (TRUE).
- ContextBuffer - Supplies a pointer to the context buffer allocated by the
- caller for the request.
- ContextBufferSize - Supplies the size of the supplied context buffer.
- Return Value:
- Status code.
- --*/
- {
- PSOCKET Socket;
- KSTATUS Status;
- Status = IoGetSocketFromHandle(Handle, &Socket);
- if (!KSUCCESS(Status)) {
- goto SocketUserControlEnd;
- }
- if (Socket->Domain == NetDomainLocal) {
- Status = STATUS_NOT_SUPPORTED;
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.UserControl(Socket,
- CodeNumber,
- FromKernelMode,
- ContextBuffer,
- ContextBufferSize);
- }
- }
- SocketUserControlEnd:
- return Status;
- }
- KERNEL_API
- KSTATUS
- IoGetSocketFromHandle (
- PIO_HANDLE IoHandle,
- PSOCKET *Socket
- )
- /*++
- Routine Description:
- This routine returns the socket structure from inside an I/O handle. This
- routine is usually only used by networking protocol to get their own
- structures for the socket they create in the "accept" function.
- Arguments:
- IoHandle - Supplies a pointer to the I/O handle whose corresponding socket
- is desired.
- Socket - Supplies a pointer where a pointer to the socket corresponding to
- the given handle will be returned on success.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_NOT_A_SOCKET if the given handle wasn't a socket.
- --*/
- {
- PFILE_OBJECT FileObject;
- FileObject = IoHandle->FileObject;
- if (FileObject->Properties.Type != IoObjectSocket) {
- return STATUS_NOT_A_SOCKET;
- }
- *Socket = FileObject->SpecialIo;
- return STATUS_SUCCESS;
- }
- VOID
- IoSysSocketCreatePair (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that creates a pair of connected
- sockets.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- ULONG HandleFlags;
- ULONG OpenFlags;
- PSYSTEM_CALL_SOCKET_CREATE_PAIR Parameters;
- PKPROCESS Process;
- PIO_HANDLE Sockets[2];
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketCreatePair);
- Parameters = (PSYSTEM_CALL_SOCKET_CREATE_PAIR)SystemCallParameter;
- Parameters->Socket1 = INVALID_HANDLE;
- Parameters->Socket2 = INVALID_HANDLE;
- Process = PsGetCurrentProcess();
- Sockets[0] = NULL;
- Sockets[1] = NULL;
- HandleFlags = 0;
- if ((Parameters->OpenFlags & SYS_OPEN_FLAG_CLOSE_ON_EXECUTE) != 0) {
- HandleFlags |= FILE_DESCRIPTOR_CLOSE_ON_EXECUTE;
- }
- OpenFlags = Parameters->OpenFlags & OPEN_FLAG_NON_BLOCKING;
- Status = IoSocketCreatePair(Parameters->Domain,
- Parameters->Type,
- Parameters->Protocol,
- OpenFlags,
- Sockets);
- if (!KSUCCESS(Status)) {
- goto CreateSocketPairEnd;
- }
- //
- // Create the handles for the sockets.
- //
- Status = ObCreateHandle(Process->HandleTable,
- Sockets[0],
- HandleFlags,
- &(Parameters->Socket1));
- if (!KSUCCESS(Status)) {
- goto CreateSocketPairEnd;
- }
- Status = ObCreateHandle(Process->HandleTable,
- Sockets[1],
- HandleFlags,
- &(Parameters->Socket2));
- if (!KSUCCESS(Status)) {
- //
- // Destory the first handle manually.
- //
- ObDestroyHandle(Process->HandleTable, Parameters->Socket1);
- Parameters->Socket1 = INVALID_HANDLE;
- goto CreateSocketPairEnd;
- }
- Status = STATUS_SUCCESS;
- CreateSocketPairEnd:
- if (!KSUCCESS(Status)) {
- ASSERT((Parameters->Socket1 == INVALID_HANDLE) &&
- (Parameters->Socket2 == INVALID_HANDLE));
- if (Sockets[0] != NULL) {
- IoClose(Sockets[0]);
- Sockets[0] = NULL;
- }
- if (Sockets[1] != NULL) {
- IoClose(Sockets[1]);
- Sockets[1] = NULL;
- }
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketCreate (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that creates a new socket.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- ULONG HandleFlags;
- PIO_HANDLE IoHandle;
- ULONG OpenFlags;
- PSYSTEM_CALL_SOCKET_CREATE Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketCreate);
- Parameters = (PSYSTEM_CALL_SOCKET_CREATE)SystemCallParameter;
- Parameters->Socket = INVALID_HANDLE;
- Process = PsGetCurrentProcess();
- ASSERT(Process != PsGetKernelProcess());
- HandleFlags = 0;
- if ((Parameters->OpenFlags & SYS_OPEN_FLAG_CLOSE_ON_EXECUTE) != 0) {
- HandleFlags |= FILE_DESCRIPTOR_CLOSE_ON_EXECUTE;
- }
- OpenFlags = Parameters->OpenFlags & SYS_OPEN_FLAG_NON_BLOCKING;
- IoHandle = NULL;
- Status = IoSocketCreate(Parameters->Domain,
- Parameters->Type,
- Parameters->Protocol,
- OpenFlags,
- &IoHandle);
- if (!KSUCCESS(Status)) {
- goto CreateSocketEnd;
- }
- //
- // Create a handle table entry for this socket.
- //
- Status = ObCreateHandle(Process->HandleTable,
- IoHandle,
- HandleFlags,
- &(Parameters->Socket));
- if (!KSUCCESS(Status)) {
- goto CreateSocketEnd;
- }
- CreateSocketEnd:
- if (!KSUCCESS(Status)) {
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Socket = INVALID_HANDLE;
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketBind (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine attempts to bind a socket to a local address.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PIO_HANDLE IoHandle;
- PSYSTEM_CALL_SOCKET_BIND Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketBind);
- Parameters = (PSYSTEM_CALL_SOCKET_BIND)SystemCallParameter;
- Process = PsGetCurrentProcess();
- //
- // Get the I/O handle.
- //
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketBindEnd;
- }
- Status = IoSocketBindToAddress(FALSE,
- IoHandle,
- NULL,
- &(Parameters->Address),
- Parameters->Path,
- Parameters->PathSize);
- if (!KSUCCESS(Status)) {
- goto SysSocketBindEnd;
- }
- SysSocketBindEnd:
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketListen (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that makes a socket listen and become
- eligible to accept new incoming connections.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PIO_HANDLE IoHandle;
- PSYSTEM_CALL_SOCKET_LISTEN Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketListen);
- Parameters = (PSYSTEM_CALL_SOCKET_LISTEN)SystemCallParameter;
- Process = PsGetCurrentProcess();
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketListenEnd;
- }
- Status = IoSocketListen(IoHandle, Parameters->BacklogCount);
- if (!KSUCCESS(Status)) {
- goto SysSocketListenEnd;
- }
- SysSocketListenEnd:
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketAccept (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that accepts a new incoming
- connection on a socket and spins it off into another socket.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- UINTN CopySize;
- ULONG HandleFlags;
- PIO_HANDLE IoHandle;
- PIO_HANDLE NewHandle;
- PSYSTEM_CALL_SOCKET_ACCEPT Parameters;
- PKPROCESS Process;
- PSTR RemotePath;
- UINTN RemotePathSize;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketAccept);
- NewHandle = NULL;
- Parameters = (PSYSTEM_CALL_SOCKET_ACCEPT)SystemCallParameter;
- Parameters->NewSocket = INVALID_HANDLE;
- Process = PsGetCurrentProcess();
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketAcceptEnd;
- }
- //
- // Run the actual accept function, which will pop out a new socket that is
- // unconnected to an I/O handle.
- //
- RemotePath = NULL;
- RemotePathSize = 0;
- Status = IoSocketAccept(IoHandle,
- &NewHandle,
- &(Parameters->Address),
- &RemotePath,
- &RemotePathSize);
- if (!KSUCCESS(Status)) {
- goto SysSocketAcceptEnd;
- }
- if ((Parameters->OpenFlags & SYS_OPEN_FLAG_NON_BLOCKING) != 0) {
- NewHandle->OpenFlags |= OPEN_FLAG_NON_BLOCKING;
- }
- HandleFlags = 0;
- if ((Parameters->OpenFlags & SYS_OPEN_FLAG_CLOSE_ON_EXECUTE) != 0) {
- HandleFlags |= FILE_DESCRIPTOR_CLOSE_ON_EXECUTE;
- }
- //
- // Finally, create a user mode handle for this socket.
- //
- Status = ObCreateHandle(Process->HandleTable,
- NewHandle,
- HandleFlags,
- &(Parameters->NewSocket));
- if (!KSUCCESS(Status)) {
- goto SysSocketAcceptEnd;
- }
- //
- // Copy the remote path over.
- //
- if (RemotePath != NULL) {
- CopySize = RemotePathSize;
- if (CopySize > Parameters->RemotePathSize) {
- CopySize = Parameters->RemotePathSize;
- }
- if (CopySize != 0) {
- Status = MmCopyToUserMode(Parameters->RemotePath,
- RemotePath,
- CopySize);
- }
- Parameters->RemotePathSize = RemotePathSize;
- if (!KSUCCESS(Status)) {
- goto SysSocketAcceptEnd;
- }
- }
- SysSocketAcceptEnd:
- if (!KSUCCESS(Status)) {
- if (NewHandle != NULL) {
- IoIoHandleReleaseReference(NewHandle);
- }
- }
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketConnect (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that reaches and and attempts to
- connect with another socket.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PIO_HANDLE IoHandle;
- PSYSTEM_CALL_SOCKET_CONNECT Parameters;
- PSTR PathCopy;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketConnect);
- Parameters = (PSYSTEM_CALL_SOCKET_CONNECT)SystemCallParameter;
- PathCopy = NULL;
- Process = PsGetCurrentProcess();
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketConnectEnd;
- }
- if (Parameters->RemotePathSize != 0) {
- Status = MmCreateCopyOfUserModeString(Parameters->RemotePath,
- Parameters->RemotePathSize,
- UNIX_SOCKET_ALLOCATION_TAG,
- &PathCopy);
- if (!KSUCCESS(Status)) {
- goto SysSocketConnectEnd;
- }
- }
- Status = IoSocketConnect(FALSE,
- IoHandle,
- &(Parameters->Address),
- PathCopy,
- Parameters->RemotePathSize);
- if (!KSUCCESS(Status)) {
- goto SysSocketConnectEnd;
- }
- SysSocketConnectEnd:
- if (PathCopy != NULL) {
- MmFreePagedPool(PathCopy);
- }
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketPerformIo (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that sends a packet to a specific
- destination or receives data from a destination. Sockets may also use the
- generic perform I/O operations if the identity of the remote address is
- either already known or not needed.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- IO_BUFFER IoBuffer;
- PIO_HANDLE IoHandle;
- SOCKET_IO_PARAMETERS IoParameters;
- PSYSTEM_CALL_SOCKET_PERFORM_IO Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketPerformIo);
- Parameters = (PSYSTEM_CALL_SOCKET_PERFORM_IO)SystemCallParameter;
- Process = PsGetCurrentProcess();
- ASSERT(SYS_WAIT_TIME_INDEFINITE == WAIT_TIME_INDEFINITE);
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketPerformIoEnd;
- }
- Status = MmCopyFromUserMode(&IoParameters,
- Parameters->Parameters,
- sizeof(SOCKET_IO_PARAMETERS));
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformIoEnd;
- }
- IoParameters.IoFlags &= SYS_IO_FLAG_MASK;
- Status = MmInitializeIoBuffer(&IoBuffer,
- Parameters->Buffer,
- INVALID_PHYSICAL_ADDRESS,
- IoParameters.Size,
- 0);
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformIoEnd;
- }
- //
- // Non-blocking handles always have a timeout of zero.
- //
- if ((IoHandle->OpenFlags & OPEN_FLAG_NON_BLOCKING) != 0) {
- IoParameters.TimeoutInMilliseconds = 0;
- }
- if ((IoParameters.IoFlags & SYS_IO_FLAG_WRITE) != 0) {
- Status = IoSocketSendData(FALSE, IoHandle, &IoParameters, &IoBuffer);
- //
- // Send a pipe signal if the returning status was "broken pipe".
- //
- if (Status == STATUS_BROKEN_PIPE) {
- ASSERT(Process != PsGetKernelProcess());
- PsSignalProcess(Process, SIGNAL_BROKEN_PIPE, NULL);
- }
- } else {
- Status = IoSocketReceiveData(FALSE, IoHandle, &IoParameters, &IoBuffer);
- }
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformIoEnd;
- }
- SysSocketPerformIoEnd:
- MmCopyToUserMode(Parameters->Parameters,
- &IoParameters,
- sizeof(SOCKET_IO_PARAMETERS));
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketPerformVectoredIo (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine handles the system call that performs socket I/O using I/O
- vectors.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PIO_BUFFER IoBuffer;
- PIO_HANDLE IoHandle;
- SOCKET_IO_PARAMETERS IoParameters;
- PSYSTEM_CALL_SOCKET_PERFORM_VECTORED_IO Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketPerformVectoredIo);
- IoBuffer = NULL;
- Parameters = (PSYSTEM_CALL_SOCKET_PERFORM_VECTORED_IO)SystemCallParameter;
- Process = PsGetCurrentProcess();
- ASSERT(SYS_WAIT_TIME_INDEFINITE == WAIT_TIME_INDEFINITE);
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketPerformVectoredIoEnd;
- }
- Status = MmCopyFromUserMode(&IoParameters,
- Parameters->Parameters,
- sizeof(SOCKET_IO_PARAMETERS));
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformVectoredIoEnd;
- }
- IoParameters.IoFlags &= SYS_IO_FLAG_MASK;
- Status = MmCreateIoBufferFromVector(Parameters->VectorArray,
- FALSE,
- Parameters->VectorCount,
- &IoBuffer);
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformVectoredIoEnd;
- }
- //
- // Non-blocking handles always have a timeout of zero.
- //
- if ((IoHandle->OpenFlags & OPEN_FLAG_NON_BLOCKING) != 0) {
- IoParameters.TimeoutInMilliseconds = 0;
- }
- if ((IoParameters.IoFlags & SYS_IO_FLAG_WRITE) != 0) {
- Status = IoSocketSendData(FALSE, IoHandle, &IoParameters, IoBuffer);
- //
- // Send a pipe signal if the returning status was "broken pipe".
- //
- if (Status == STATUS_BROKEN_PIPE) {
- ASSERT(Process != PsGetKernelProcess());
- PsSignalProcess(Process, SIGNAL_BROKEN_PIPE, NULL);
- }
- } else {
- Status = IoSocketReceiveData(FALSE, IoHandle, &IoParameters, IoBuffer);
- }
- if (!KSUCCESS(Status)) {
- goto SysSocketPerformVectoredIoEnd;
- }
- SysSocketPerformVectoredIoEnd:
- if (IoBuffer != NULL) {
- MmFreeIoBuffer(IoBuffer);
- }
- MmCopyToUserMode(Parameters->Parameters,
- &IoParameters,
- sizeof(SOCKET_IO_PARAMETERS));
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketGetSetInformation (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine implements the system call for getting or setting socket
- information.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PVOID Buffer;
- UINTN CopySize;
- KSTATUS CopyStatus;
- PIO_HANDLE IoHandle;
- PSYSTEM_CALL_SOCKET_GET_SET_INFORMATION Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketGetSetInformation);
- Buffer = NULL;
- Parameters = (PSYSTEM_CALL_SOCKET_GET_SET_INFORMATION)SystemCallParameter;
- Process = PsGetCurrentProcess();
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketGetSetInformationEnd;
- }
- //
- // Create a paged pool buffer to hold the data.
- //
- CopySize = 0;
- if (Parameters->DataSize != 0) {
- Buffer = MmAllocatePagedPool(Parameters->DataSize,
- SOCKET_INFORMATION_ALLOCATION_TAG);
- if (Buffer == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto SysSocketGetSetInformationEnd;
- }
- CopySize = Parameters->DataSize;
- //
- // Copy the data into the kernel mode buffer.
- //
- Status = MmCopyFromUserMode(Buffer,
- Parameters->Data,
- Parameters->DataSize);
- if (!KSUCCESS(Status)) {
- goto SysSocketGetSetInformationEnd;
- }
- }
- Status = IoSocketGetSetInformation(IoHandle,
- Parameters->InformationType,
- Parameters->Option,
- Buffer,
- &(Parameters->DataSize),
- Parameters->Set);
- //
- // Copy the data back into user mode, even on set operations.
- //
- if (CopySize > Parameters->DataSize) {
- CopySize = Parameters->DataSize;
- }
- if (CopySize != 0) {
- CopyStatus = MmCopyToUserMode(Parameters->Data, Buffer, CopySize);
- if ((KSUCCESS(Status)) && (!KSUCCESS(CopyStatus))) {
- Status = CopyStatus;
- }
- }
- SysSocketGetSetInformationEnd:
- if (Buffer != NULL) {
- MmFreePagedPool(Buffer);
- }
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- VOID
- IoSysSocketShutdown (
- ULONG SystemCallNumber,
- PVOID SystemCallParameter,
- PTRAP_FRAME TrapFrame,
- PULONG ResultSize
- )
- /*++
- Routine Description:
- This routine implements the system call for shutting down communication to
- a socket.
- Arguments:
- SystemCallNumber - Supplies the system call number that was requested.
- SystemCallParameter - Supplies a pointer to the parameters supplied with
- the system call. This structure will be a stack-local copy of the
- actual parameters passed from user-mode.
- TrapFrame - Supplies a pointer to the trap frame generated by this jump
- from user mode to kernel mode.
- ResultSize - Supplies a pointer where the system call routine returns the
- size of the parameter structure to be copied back to user mode. The
- value returned here must be no larger than the original parameter
- structure size. The default is the original size of the parameters.
- Return Value:
- None.
- --*/
- {
- PIO_HANDLE IoHandle;
- PSYSTEM_CALL_SOCKET_SHUTDOWN Parameters;
- PKPROCESS Process;
- KSTATUS Status;
- ASSERT(SystemCallNumber == SystemCallSocketShutdown);
- Parameters = (PSYSTEM_CALL_SOCKET_SHUTDOWN)SystemCallParameter;
- Process = PsGetCurrentProcess();
- IoHandle = ObGetHandleValue(Process->HandleTable, Parameters->Socket, NULL);
- if (IoHandle == NULL) {
- Status = STATUS_INVALID_HANDLE;
- goto SysSocketShutdownEnd;
- }
- Status = IoSocketShutdown(IoHandle, Parameters->ShutdownType);
- SysSocketShutdownEnd:
- //
- // Release the reference that was added when the handle was looked up.
- //
- if (IoHandle != NULL) {
- IoIoHandleReleaseReference(IoHandle);
- }
- Parameters->Status = Status;
- return;
- }
- KSTATUS
- IopCreateSocket (
- PVOID Parameter,
- FILE_PERMISSIONS CreatePermissions,
- PFILE_OBJECT *FileObject
- )
- /*++
- Routine Description:
- This routine allocates resources associated with a new socket.
- Arguments:
- Parameter - Supplies the parameters the socket was created with.
- CreatePermissions - Supplies the permissions to create the file with.
- FileObject - Supplies a pointer where the new file object representing the
- socket will be returned on success.
- Return Value:
- Status code.
- --*/
- {
- BOOL Created;
- NETWORK_ADDRESS LocalAddress;
- PFILE_OBJECT NewFileObject;
- PSOCKET_CREATION_PARAMETERS Parameters;
- FILE_PROPERTIES Properties;
- PVOID RootObject;
- PSOCKET Socket;
- KSTATUS Status;
- PKTHREAD Thread;
- Created = FALSE;
- NewFileObject = NULL;
- Parameters = (PSOCKET_CREATION_PARAMETERS)Parameter;
- //
- // If there are no parameters, then this file object is being created from
- // a leftover file system entry. Just succeed, but it will never be able to
- // be opened. The common case for this is a request for the file properties.
- //
- if (Parameters == NULL) {
- ASSERT(*FileObject != NULL);
- Status = STATUS_SUCCESS;
- goto CreateSocketEnd;
- }
- Socket = NULL;
- //
- // In cases where a Unix socket is trying to bind to a new path entry,
- // there's already a socket that's been created. Use that one.
- //
- if (Parameters->ExistingSocket != NULL) {
- Socket = Parameters->ExistingSocket;
- ASSERT(Socket->IoState != NULL);
- IoSocketAddReference(Socket);
- //
- // Most of the time, a socket needs to be created.
- //
- } else {
- if (Parameters->Domain == NetDomainLocal) {
- Status = IopCreateUnixSocket(Parameters->Domain,
- Parameters->Type,
- Parameters->Protocol,
- &Socket);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- goto CreateSocketEnd;
- }
- Status = IoNetInterface.CreateSocket(Parameters->Domain,
- Parameters->Type,
- Parameters->Protocol,
- &Socket);
- }
- if (!KSUCCESS(Status)) {
- goto CreateSocketEnd;
- }
- IoSocketAddReference(Socket);
- //
- // Fill in the standard parts of the socket structure.
- //
- Socket->Domain = Parameters->Domain;
- Socket->Type = Parameters->Type;
- if (Socket->IoState == NULL) {
- Socket->IoState = IoCreateIoObjectState(FALSE);
- if (Socket->IoState == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto CreateSocketEnd;
- }
- }
- }
- //
- // Create or look up a file object for the socket if needed.
- //
- if (*FileObject == NULL) {
- Thread = KeGetCurrentThread();
- RtlZeroMemory(&Properties, sizeof(FILE_PROPERTIES));
- RootObject = ObGetRootObject();
- Properties.DeviceId = OBJECT_MANAGER_DEVICE_ID;
- Properties.FileId = (UINTN)Socket;
- Properties.Type = IoObjectSocket;
- Properties.UserId = Thread->Identity.EffectiveUserId;
- Properties.GroupId = Thread->Identity.EffectiveGroupId;
- Properties.HardLinkCount = 1;
- Properties.Permissions = CreatePermissions;
- KeGetSystemTime(&(Properties.StatusChangeTime));
- RtlCopyMemory(&(Properties.ModifiedTime),
- &(Properties.StatusChangeTime),
- sizeof(SYSTEM_TIME));
- RtlCopyMemory(&(Properties.AccessTime),
- &(Properties.StatusChangeTime),
- sizeof(SYSTEM_TIME));
- Status = IopCreateOrLookupFileObject(&Properties,
- RootObject,
- FILE_OBJECT_FLAG_EXTERNAL_IO_STATE,
- &NewFileObject,
- &Created);
- if (!KSUCCESS(Status)) {
- goto CreateSocketEnd;
- }
- ASSERT((Created != FALSE) || (Socket == Parameters->ExistingSocket));
- *FileObject = NewFileObject;
- }
- ASSERT(((*FileObject)->IoState == NULL) &&
- (((*FileObject)->Flags & FILE_OBJECT_FLAG_EXTERNAL_IO_STATE) != 0));
- (*FileObject)->IoState = Socket->IoState;
- (*FileObject)->SpecialIo = Socket;
- //
- // The socket is finally ready to go. If it is a raw socket it needs to get
- // bound to the any address.
- //
- if ((Socket->Type == NetSocketRaw) &&
- (Socket->Domain != NetDomainLocal)) {
- ASSERT(Parameters->ExistingSocket == NULL);
- RtlZeroMemory(&LocalAddress, sizeof(NETWORK_ADDRESS));
- LocalAddress.Domain = Socket->Domain;
- Status = IoNetInterface.BindToAddress(Socket, NULL, &LocalAddress);
- if (!KSUCCESS(Status)) {
- goto CreateSocketEnd;
- }
- }
- Status = STATUS_SUCCESS;
- CreateSocketEnd:
- //
- // On both success and failure, the file object's ready event needs to be
- // signaled. Other threads may be waiting on the event.
- //
- if (*FileObject != NULL) {
- ASSERT((KeGetEventState((*FileObject)->ReadyEvent) == NotSignaled) ||
- (KeGetEventState((*FileObject)->ReadyEvent) ==
- NotSignaledWithWaiters));
- KeSignalEvent((*FileObject)->ReadyEvent, SignalOptionSignalAll);
- }
- if (!KSUCCESS(Status)) {
- if ((Socket != NULL) && ((*FileObject)->SpecialIo != Socket)) {
- IoSocketReleaseReference(Socket);
- }
- if (NewFileObject != NULL) {
- IopFileObjectReleaseReference(NewFileObject);
- *FileObject = NULL;
- }
- }
- return Status;
- }
- KSTATUS
- IopPerformSocketIoOperation (
- PIO_HANDLE Handle,
- PIO_CONTEXT IoContext
- )
- /*++
- Routine Description:
- This routine reads from or writes to a socket.
- Arguments:
- Handle - Supplies a pointer to the socket I/O handle.
- IoContext - Supplies a pointer to the I/O context.
- Return Value:
- Status code. A failing status code does not necessarily mean no I/O made it
- in or out. Check the bytes completed value in the I/O context to find out
- how much occurred.
- --*/
- {
- SOCKET_IO_PARAMETERS IoParameters;
- KSTATUS Status;
- ASSERT(IoContext->IoBuffer != NULL);
- RtlZeroMemory(&IoParameters, sizeof(SOCKET_IO_PARAMETERS));
- IoParameters.Size = IoContext->SizeInBytes;
- IoParameters.TimeoutInMilliseconds = IoContext->TimeoutInMilliseconds;
- if (IoContext->Write != FALSE) {
- Status = IoSocketSendData(FALSE,
- Handle,
- &IoParameters,
- IoContext->IoBuffer);
- } else {
- Status = IoSocketReceiveData(FALSE,
- Handle,
- &IoParameters,
- IoContext->IoBuffer);
- }
- IoContext->BytesCompleted = IoParameters.Size;
- return Status;
- }
- KSTATUS
- IopOpenSocket (
- PIO_HANDLE IoHandle
- )
- /*++
- Routine Description:
- This routine opens a socket connection.
- Arguments:
- IoHandle - Supplies a pointer to the I/O handle for the socket being opened.
- Return Value:
- Status code.
- --*/
- {
- PFILE_OBJECT FileObject;
- PSOCKET Socket;
- FileObject = IoHandle->FileObject;
- ASSERT(FileObject->Properties.Type == IoObjectSocket);
- if (IoHandle->Access == 0) {
- return STATUS_SUCCESS;
- }
- Socket = FileObject->SpecialIo;
- if (Socket == NULL) {
- return STATUS_NOT_READY;
- }
- return STATUS_SUCCESS;
- }
- KSTATUS
- IopCloseSocket (
- PIO_HANDLE IoHandle
- )
- /*++
- Routine Description:
- This routine closes a socket connection.
- Arguments:
- IoHandle - Supplies a pointer to the socket handle to close.
- Return Value:
- Status code.
- --*/
- {
- PFILE_OBJECT FileObject;
- PSOCKET Socket;
- KSTATUS Status;
- FileObject = IoHandle->FileObject;
- if (FileObject->Properties.Type != IoObjectSocket) {
- Status = STATUS_NOT_A_SOCKET;
- goto CloseSocketEnd;
- }
- if (IoHandle->Access == 0) {
- return STATUS_SUCCESS;
- }
- Socket = FileObject->SpecialIo;
- if (Socket->Domain == NetDomainLocal) {
- Status = IopUnixSocketClose(Socket);
- } else {
- if (IoNetInterfaceInitialized == FALSE) {
- Status = STATUS_NOT_IMPLEMENTED;
- } else {
- Status = IoNetInterface.CloseSocket(Socket);
- if (!KSUCCESS(Status)) {
- goto CloseSocketEnd;
- }
- }
- }
- CloseSocketEnd:
- return Status;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- VOID
- IopDestroySocket (
- PSOCKET Socket
- )
- /*++
- Routine Description:
- This routine destroys a socket object.
- Arguments:
- Socket - Supplies a pointer to the socket to close.
- Return Value:
- Status code.
- --*/
- {
- ASSERT(Socket->ReferenceCount == 0);
- ASSERT(Socket->IoState != NULL);
- IoDestroyIoObjectState(Socket->IoState);
- Socket->IoState = NULL;
- if (Socket->Domain == NetDomainLocal) {
- IopDestroyUnixSocket(Socket);
- } else {
- IoNetInterface.DestroySocket(Socket);
- }
- return;
- }
|