raw.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. raw.c
  9. Abstract:
  10. This module implements the raw socket protocol.
  11. Author:
  12. Chris Stevens 20-May-2014
  13. Environment:
  14. Kernel
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. //
  20. // Protocol drivers are supposed to be able to stand on their own (ie be able to
  21. // be implemented outside the core net library). For the builtin ones, avoid
  22. // including netcore.h, but still redefine those functions that would otherwise
  23. // generate imports.
  24. //
  25. #define NET_API __DLLEXPORT
  26. #include <minoca/kernel/driver.h>
  27. #include <minoca/net/netdrv.h>
  28. //
  29. // ---------------------------------------------------------------- Definitions
  30. //
  31. //
  32. // Define the allocation tag used by the raw socket protocol.
  33. //
  34. #define RAW_PROTOCOL_ALLOCATION_TAG 0x21707352 // '!psR'
  35. //
  36. // Define the default size of the RAW socket's receive data buffer, in bytes.
  37. //
  38. #define RAW_DEFAULT_RECEIVE_BUFFER_SIZE (256 * _1KB)
  39. //
  40. // Define the minimum receive buffer size.
  41. //
  42. #define RAW_MIN_RECEIVE_BUFFER_SIZE _2KB
  43. //
  44. // Define the default minimum number of bytes necessary for the RAW socket to
  45. // become readable.
  46. //
  47. #define RAW_DEFAULT_RECEIVE_MINIMUM 1
  48. //
  49. // Define the minimum number of bytes necessary for RAW sockets to become
  50. // writable. There is no minimum and bytes are immediately sent on the wire.
  51. //
  52. #define RAW_SEND_MINIMUM 1
  53. //
  54. // Define the maximum packet size allowed on a raw socket.
  55. //
  56. #define RAW_MAX_PACKET_SIZE MAX_ULONG
  57. //
  58. // ------------------------------------------------------ Data Type Definitions
  59. //
  60. /*++
  61. Structure Description:
  62. This structure defines a raw socket protocol's data socket.
  63. Members:
  64. NetSocket - Stores the common core networking parameters.
  65. ReceivedPacketList - Stores the list of packets ready to be read by the
  66. user.
  67. ReceiveLock - Stores the lock that protects the received packets list,
  68. dropped packet count, and various receive buffer parameters. This lock
  69. must always be acquired at low level.
  70. ReceiveBufferTotalSize - Stores the total size of the receive buffer, in
  71. bytes. Packets that are received but will not fit in the buffer are
  72. discarded.
  73. ReceiveBufferFreeSize - Stores the receive buffer's free space, in bytes.
  74. Packets that are received but do not fit in the free space are
  75. discarded.
  76. ReceiveTimeout - Stores the maximum amount of time, in milliseconds, that
  77. the socket will wait when receiving data.
  78. ReceiveMinimum - Stores the minimum amount of bytes that must be available
  79. before the socket is made readable. This is ignored.
  80. DroppedPacketCount - Stores the number of packets that have been dropped
  81. because the receive queue was full.
  82. ShutdownTypes - Stores the mask of shutdowns that have occurred on this
  83. socket.
  84. MaxPacketSize - Stores the maximum size of RAW datagrams.
  85. --*/
  86. typedef struct _RAW_SOCKET {
  87. NET_SOCKET NetSocket;
  88. LIST_ENTRY ReceivedPacketList;
  89. PQUEUED_LOCK ReceiveLock;
  90. ULONG ReceiveBufferTotalSize;
  91. ULONG ReceiveBufferFreeSize;
  92. ULONG ReceiveTimeout;
  93. ULONG ReceiveMinimum;
  94. ULONG DroppedPacketCount;
  95. ULONG MaxPacketSize;
  96. ULONG ShutdownTypes;
  97. } RAW_SOCKET, *PRAW_SOCKET;
  98. /*++
  99. Structure Description:
  100. This structure defines a raw socket protocol received message.
  101. Members:
  102. ListEntry - Stores pointers to the next and previous packets.
  103. Address - Stores the network address where this data came from.
  104. DataBuffer - Stores a pointer to the buffer containing the actual data.
  105. Size - Stores the number of bytes in the data buffer.
  106. --*/
  107. typedef struct _RAW_RECEIVED_PACKET {
  108. LIST_ENTRY ListEntry;
  109. NETWORK_ADDRESS Address;
  110. PVOID DataBuffer;
  111. ULONG Size;
  112. } RAW_RECEIVED_PACKET, *PRAW_RECEIVED_PACKET;
  113. /*++
  114. Structure Description:
  115. This structure defines a raw socket option.
  116. Members:
  117. InformationType - Stores the information type for the socket option.
  118. Option - Stores the type-specific option identifier.
  119. Size - Stores the size of the option value, in bytes.
  120. SetAllowed - Stores a boolean indicating whether or not the option is
  121. allowed to be set.
  122. --*/
  123. typedef struct _RAW_SOCKET_OPTION {
  124. SOCKET_INFORMATION_TYPE InformationType;
  125. UINTN Option;
  126. UINTN Size;
  127. BOOL SetAllowed;
  128. } RAW_SOCKET_OPTION, *PRAW_SOCKET_OPTION;
  129. //
  130. // ----------------------------------------------- Internal Function Prototypes
  131. //
  132. KSTATUS
  133. NetpRawCreateSocket (
  134. PNET_PROTOCOL_ENTRY ProtocolEntry,
  135. PNET_NETWORK_ENTRY NetworkEntry,
  136. ULONG NetworkProtocol,
  137. PNET_SOCKET *NewSocket
  138. );
  139. VOID
  140. NetpRawDestroySocket (
  141. PNET_SOCKET Socket
  142. );
  143. KSTATUS
  144. NetpRawBindToAddress (
  145. PNET_SOCKET Socket,
  146. PNET_LINK Link,
  147. PNETWORK_ADDRESS Address
  148. );
  149. KSTATUS
  150. NetpRawListen (
  151. PNET_SOCKET Socket
  152. );
  153. KSTATUS
  154. NetpRawAccept (
  155. PNET_SOCKET Socket,
  156. PIO_HANDLE *NewConnectionSocket,
  157. PNETWORK_ADDRESS RemoteAddress
  158. );
  159. KSTATUS
  160. NetpRawConnect (
  161. PNET_SOCKET Socket,
  162. PNETWORK_ADDRESS Address
  163. );
  164. KSTATUS
  165. NetpRawClose (
  166. PNET_SOCKET Socket
  167. );
  168. KSTATUS
  169. NetpRawShutdown (
  170. PNET_SOCKET Socket,
  171. ULONG ShutdownType
  172. );
  173. KSTATUS
  174. NetpRawSend (
  175. BOOL FromKernelMode,
  176. PNET_SOCKET Socket,
  177. PSOCKET_IO_PARAMETERS Parameters,
  178. PIO_BUFFER IoBuffer
  179. );
  180. VOID
  181. NetpRawProcessReceivedData (
  182. PNET_RECEIVE_CONTEXT ReceiveContext
  183. );
  184. KSTATUS
  185. NetpRawProcessReceivedSocketData (
  186. PNET_SOCKET Socket,
  187. PNET_RECEIVE_CONTEXT ReceiveContext
  188. );
  189. KSTATUS
  190. NetpRawReceive (
  191. BOOL FromKernelMode,
  192. PNET_SOCKET Socket,
  193. PSOCKET_IO_PARAMETERS Parameters,
  194. PIO_BUFFER IoBuffer
  195. );
  196. KSTATUS
  197. NetpRawGetSetInformation (
  198. PNET_SOCKET Socket,
  199. SOCKET_INFORMATION_TYPE InformationType,
  200. UINTN SocketOption,
  201. PVOID Data,
  202. PUINTN DataSize,
  203. BOOL Set
  204. );
  205. KSTATUS
  206. NetpRawUserControl (
  207. PNET_SOCKET Socket,
  208. ULONG CodeNumber,
  209. BOOL FromKernelMode,
  210. PVOID ContextBuffer,
  211. UINTN ContextBufferSize
  212. );
  213. //
  214. // -------------------------------------------------------------------- Globals
  215. //
  216. NET_PROTOCOL_ENTRY NetRawProtocol = {
  217. {NULL, NULL},
  218. NetSocketRaw,
  219. SOCKET_INTERNET_PROTOCOL_RAW,
  220. 0,
  221. NULL,
  222. NULL,
  223. {{0}, {0}, {0}},
  224. {
  225. NetpRawCreateSocket,
  226. NetpRawDestroySocket,
  227. NetpRawBindToAddress,
  228. NetpRawListen,
  229. NetpRawAccept,
  230. NetpRawConnect,
  231. NetpRawClose,
  232. NetpRawShutdown,
  233. NetpRawSend,
  234. NetpRawProcessReceivedData,
  235. NetpRawProcessReceivedSocketData,
  236. NetpRawReceive,
  237. NetpRawGetSetInformation,
  238. NetpRawUserControl
  239. }
  240. };
  241. RAW_SOCKET_OPTION NetRawSocketOptions[] = {
  242. {
  243. SocketInformationBasic,
  244. SocketBasicOptionSendBufferSize,
  245. sizeof(ULONG),
  246. TRUE
  247. },
  248. {
  249. SocketInformationBasic,
  250. SocketBasicOptionSendMinimum,
  251. sizeof(ULONG),
  252. FALSE
  253. },
  254. {
  255. SocketInformationBasic,
  256. SocketBasicOptionReceiveBufferSize,
  257. sizeof(ULONG),
  258. TRUE
  259. },
  260. {
  261. SocketInformationBasic,
  262. SocketBasicOptionReceiveMinimum,
  263. sizeof(ULONG),
  264. TRUE
  265. },
  266. {
  267. SocketInformationBasic,
  268. SocketBasicOptionReceiveTimeout,
  269. sizeof(SOCKET_TIME),
  270. TRUE
  271. },
  272. };
  273. //
  274. // ------------------------------------------------------------------ Functions
  275. //
  276. VOID
  277. NetpRawInitialize (
  278. VOID
  279. )
  280. /*++
  281. Routine Description:
  282. This routine initializes support for raw sockets.
  283. Arguments:
  284. None.
  285. Return Value:
  286. None.
  287. --*/
  288. {
  289. KSTATUS Status;
  290. //
  291. // Register the raw socket handler with the core networking library. There
  292. // is no real "raw protocol", so this is a special protocol that gets to
  293. // filter packets from every protocol.
  294. //
  295. Status = NetRegisterProtocol(&NetRawProtocol, NULL);
  296. if (!KSUCCESS(Status)) {
  297. ASSERT(FALSE);
  298. }
  299. return;
  300. }
  301. KSTATUS
  302. NetpRawCreateSocket (
  303. PNET_PROTOCOL_ENTRY ProtocolEntry,
  304. PNET_NETWORK_ENTRY NetworkEntry,
  305. ULONG NetworkProtocol,
  306. PNET_SOCKET *NewSocket
  307. )
  308. /*++
  309. Routine Description:
  310. This routine allocates resources associated with a new socket. The protocol
  311. driver is responsible for allocating the structure (with additional length
  312. for any of its context). The core networking library will fill in the
  313. common header when this routine returns.
  314. Arguments:
  315. ProtocolEntry - Supplies a pointer to the protocol information.
  316. NetworkEntry - Supplies a pointer to the network information.
  317. NetworkProtocol - Supplies the raw protocol value for this socket used on
  318. the network. This value is network specific.
  319. NewSocket - Supplies a pointer where a pointer to a newly allocated
  320. socket structure will be returned. The caller is responsible for
  321. allocating the socket (and potentially a larger structure for its own
  322. context). The core network library will fill in the standard socket
  323. structure after this routine returns.
  324. Return Value:
  325. Status code.
  326. --*/
  327. {
  328. PNET_SOCKET NetSocket;
  329. PNET_PACKET_SIZE_INFORMATION PacketSizeInformation;
  330. PRAW_SOCKET RawSocket;
  331. KSTATUS Status;
  332. ASSERT(ProtocolEntry->Type == NetSocketRaw);
  333. ASSERT(ProtocolEntry->ParentProtocolNumber == SOCKET_INTERNET_PROTOCOL_RAW);
  334. NetSocket = NULL;
  335. RawSocket = MmAllocatePagedPool(sizeof(RAW_SOCKET),
  336. RAW_PROTOCOL_ALLOCATION_TAG);
  337. if (RawSocket == NULL) {
  338. Status = STATUS_INSUFFICIENT_RESOURCES;
  339. goto RawCreateSocketEnd;
  340. }
  341. RtlZeroMemory(RawSocket, sizeof(RAW_SOCKET));
  342. NetSocket = &(RawSocket->NetSocket);
  343. NetSocket->KernelSocket.Protocol = NetworkProtocol;
  344. NetSocket->KernelSocket.ReferenceCount = 1;
  345. INITIALIZE_LIST_HEAD(&(RawSocket->ReceivedPacketList));
  346. RawSocket->ReceiveTimeout = WAIT_TIME_INDEFINITE;
  347. RawSocket->ReceiveBufferTotalSize = RAW_DEFAULT_RECEIVE_BUFFER_SIZE;
  348. RawSocket->ReceiveBufferFreeSize = RawSocket->ReceiveBufferTotalSize;
  349. RawSocket->ReceiveMinimum = RAW_DEFAULT_RECEIVE_MINIMUM;
  350. RawSocket->MaxPacketSize = RAW_MAX_PACKET_SIZE;
  351. RawSocket->ReceiveLock = KeCreateQueuedLock();
  352. if (RawSocket->ReceiveLock == NULL) {
  353. Status = STATUS_INSUFFICIENT_RESOURCES;
  354. goto RawCreateSocketEnd;
  355. }
  356. //
  357. // Give the lower layers a chance to initialize. Start the maximum packet
  358. // size at the largest possible value.
  359. //
  360. ASSERT(RAW_MAX_PACKET_SIZE == MAX_ULONG);
  361. PacketSizeInformation = &(NetSocket->PacketSizeInformation);
  362. PacketSizeInformation->MaxPacketSize = RAW_MAX_PACKET_SIZE;
  363. Status = NetworkEntry->Interface.InitializeSocket(ProtocolEntry,
  364. NetworkEntry,
  365. NetworkProtocol,
  366. NetSocket);
  367. if (!KSUCCESS(Status)) {
  368. goto RawCreateSocketEnd;
  369. }
  370. Status = STATUS_SUCCESS;
  371. RawCreateSocketEnd:
  372. if (!KSUCCESS(Status)) {
  373. if (RawSocket != NULL) {
  374. if (RawSocket->ReceiveLock != NULL) {
  375. KeDestroyQueuedLock(RawSocket->ReceiveLock);
  376. }
  377. MmFreePagedPool(RawSocket);
  378. RawSocket = NULL;
  379. NetSocket = NULL;
  380. }
  381. }
  382. *NewSocket = NetSocket;
  383. return Status;
  384. }
  385. VOID
  386. NetpRawDestroySocket (
  387. PNET_SOCKET Socket
  388. )
  389. /*++
  390. Routine Description:
  391. This routine destroys resources associated with an open socket, officially
  392. marking the end of the kernel and core networking library's knowledge of
  393. this structure.
  394. Arguments:
  395. Socket - Supplies a pointer to the socket to destroy. The core networking
  396. library will have already destroyed any resources inside the common
  397. header, the protocol should not reach through any pointers inside the
  398. socket header except the protocol and network entries.
  399. Return Value:
  400. None. This routine is responsible for freeing the memory associated with
  401. the socket structure itself.
  402. --*/
  403. {
  404. PRAW_RECEIVED_PACKET Packet;
  405. PRAW_SOCKET RawSocket;
  406. RawSocket = (PRAW_SOCKET)Socket;
  407. //
  408. // Loop through and free any leftover packets.
  409. //
  410. KeAcquireQueuedLock(RawSocket->ReceiveLock);
  411. while (!LIST_EMPTY(&(RawSocket->ReceivedPacketList))) {
  412. Packet = LIST_VALUE(RawSocket->ReceivedPacketList.Next,
  413. RAW_RECEIVED_PACKET,
  414. ListEntry);
  415. LIST_REMOVE(&(Packet->ListEntry));
  416. RawSocket->ReceiveBufferFreeSize += Packet->Size;
  417. MmFreePagedPool(Packet);
  418. }
  419. ASSERT(RawSocket->ReceiveBufferFreeSize ==
  420. RawSocket->ReceiveBufferTotalSize);
  421. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  422. if (Socket->Network->Interface.DestroySocket != NULL) {
  423. Socket->Network->Interface.DestroySocket(Socket);
  424. }
  425. KeDestroyQueuedLock(RawSocket->ReceiveLock);
  426. MmFreePagedPool(RawSocket);
  427. return;
  428. }
  429. KSTATUS
  430. NetpRawBindToAddress (
  431. PNET_SOCKET Socket,
  432. PNET_LINK Link,
  433. PNETWORK_ADDRESS Address
  434. )
  435. /*++
  436. Routine Description:
  437. This routine binds the given socket to the specified network address.
  438. Usually this is a no-op for the protocol, it's simply responsible for
  439. passing the request down to the network layer.
  440. Arguments:
  441. Socket - Supplies a pointer to the socket to bind.
  442. Link - Supplies an optional pointer to a link to bind to.
  443. Address - Supplies a pointer to the address to bind the socket to.
  444. Return Value:
  445. Status code.
  446. --*/
  447. {
  448. ULONG OriginalPort;
  449. KSTATUS Status;
  450. //
  451. // Allow raw sockets to get bound multiple times, unless they are already
  452. // connected to a peer address. They get bound to the any address upon
  453. // creation.
  454. //
  455. if (Socket->RemoteAddress.Domain != NetDomainInvalid) {
  456. Status = STATUS_INVALID_PARAMETER;
  457. goto RawBindToAddressEnd;
  458. }
  459. //
  460. // Currently only IPv4 addresses are supported.
  461. //
  462. if (Address->Domain != NetDomainIp4) {
  463. Status = STATUS_NOT_SUPPORTED;
  464. goto RawBindToAddressEnd;
  465. }
  466. //
  467. // The port doesn't make a difference on raw sockets, so zero it out.
  468. //
  469. OriginalPort = Address->Port;
  470. Address->Port = 0;
  471. //
  472. // Pass the request down to the network layer.
  473. //
  474. Status = Socket->Network->Interface.BindToAddress(Socket, Link, Address);
  475. Address->Port = OriginalPort;
  476. if (!KSUCCESS(Status)) {
  477. goto RawBindToAddressEnd;
  478. }
  479. //
  480. // Begin listening immediately, as there is no explicit listen step for raw
  481. // sockets.
  482. //
  483. Status = Socket->Network->Interface.Listen(Socket);
  484. if (!KSUCCESS(Status)) {
  485. goto RawBindToAddressEnd;
  486. }
  487. IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
  488. RawBindToAddressEnd:
  489. return Status;
  490. }
  491. KSTATUS
  492. NetpRawListen (
  493. PNET_SOCKET Socket
  494. )
  495. /*++
  496. Routine Description:
  497. This routine adds a bound socket to the list of listening sockets,
  498. officially allowing clients to attempt to connect to it.
  499. Arguments:
  500. Socket - Supplies a pointer to the socket to mark as listning.
  501. Return Value:
  502. Status code.
  503. --*/
  504. {
  505. return STATUS_NOT_SUPPORTED;
  506. }
  507. KSTATUS
  508. NetpRawAccept (
  509. PNET_SOCKET Socket,
  510. PIO_HANDLE *NewConnectionSocket,
  511. PNETWORK_ADDRESS RemoteAddress
  512. )
  513. /*++
  514. Routine Description:
  515. This routine accepts an incoming connection on a listening connection-based
  516. socket.
  517. Arguments:
  518. Socket - Supplies a pointer to the socket to accept a connection from.
  519. NewConnectionSocket - Supplies a pointer where a new socket will be
  520. returned that represents the accepted connection with the remote
  521. host.
  522. RemoteAddress - Supplies a pointer where the address of the connected
  523. remote host will be returned.
  524. Return Value:
  525. Status code.
  526. --*/
  527. {
  528. return STATUS_NOT_SUPPORTED;
  529. }
  530. KSTATUS
  531. NetpRawConnect (
  532. PNET_SOCKET Socket,
  533. PNETWORK_ADDRESS Address
  534. )
  535. /*++
  536. Routine Description:
  537. This routine attempts to make an outgoing connection to a server.
  538. Arguments:
  539. Socket - Supplies a pointer to the socket to use for the connection.
  540. Address - Supplies a pointer to the address to connect to.
  541. Return Value:
  542. Status code.
  543. --*/
  544. {
  545. ULONG OriginalPort;
  546. KSTATUS Status;
  547. //
  548. // The port doesn't make a different on raw sockets, so zero it out.
  549. //
  550. OriginalPort = Address->Port;
  551. Address->Port = 0;
  552. //
  553. // Pass the request down to the network layer.
  554. //
  555. Status = Socket->Network->Interface.Connect(Socket, Address);
  556. Address->Port = OriginalPort;
  557. if (!KSUCCESS(Status)) {
  558. goto RawConnectEnd;
  559. }
  560. IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
  561. RawConnectEnd:
  562. return Status;
  563. }
  564. KSTATUS
  565. NetpRawClose (
  566. PNET_SOCKET Socket
  567. )
  568. /*++
  569. Routine Description:
  570. This routine closes a socket connection.
  571. Arguments:
  572. Socket - Supplies a pointer to the socket to shut down.
  573. Return Value:
  574. Status code.
  575. --*/
  576. {
  577. KSTATUS Status;
  578. //
  579. // Close it at the lower level and then release the reference taken on
  580. // create if the close was successful.
  581. //
  582. Status = Socket->Network->Interface.Close(Socket);
  583. if (!KSUCCESS(Status)) {
  584. goto RawCloseEnd;
  585. }
  586. IoSocketReleaseReference(&(Socket->KernelSocket));
  587. RawCloseEnd:
  588. return Status;
  589. }
  590. KSTATUS
  591. NetpRawShutdown (
  592. PNET_SOCKET Socket,
  593. ULONG ShutdownType
  594. )
  595. /*++
  596. Routine Description:
  597. This routine shuts down communication with a given socket.
  598. Arguments:
  599. Socket - Supplies a pointer to the socket.
  600. ShutdownType - Supplies the shutdown type to perform. See the
  601. SOCKET_SHUTDOWN_* definitions.
  602. Return Value:
  603. Status code.
  604. --*/
  605. {
  606. PRAW_SOCKET RawSocket;
  607. //
  608. // Shutdown is not supported unless the socket is connected.
  609. //
  610. if (Socket->RemoteAddress.Domain == NetDomainInvalid) {
  611. return STATUS_NOT_CONNECTED;
  612. }
  613. RawSocket = (PRAW_SOCKET)Socket;
  614. RtlAtomicOr32(&(RawSocket->ShutdownTypes), ShutdownType);
  615. //
  616. // Signal the read event if the read end was shut down.
  617. //
  618. if ((ShutdownType & SOCKET_SHUTDOWN_READ) != 0) {
  619. KeAcquireQueuedLock(RawSocket->ReceiveLock);
  620. IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_IN, TRUE);
  621. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  622. }
  623. if ((ShutdownType & SOCKET_SHUTDOWN_WRITE) != 0) {
  624. IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_OUT, TRUE);
  625. }
  626. return STATUS_SUCCESS;
  627. }
  628. KSTATUS
  629. NetpRawSend (
  630. BOOL FromKernelMode,
  631. PNET_SOCKET Socket,
  632. PSOCKET_IO_PARAMETERS Parameters,
  633. PIO_BUFFER IoBuffer
  634. )
  635. /*++
  636. Routine Description:
  637. This routine sends the given data buffer through the network using a
  638. specific protocol.
  639. Arguments:
  640. FromKernelMode - Supplies a boolean indicating whether the request is
  641. coming from kernel mode (TRUE) or user mode (FALSE).
  642. Socket - Supplies a pointer to the socket to send the data to.
  643. Parameters - Supplies a pointer to the socket I/O parameters. This will
  644. always be a kernel mode pointer.
  645. IoBuffer - Supplies a pointer to the I/O buffer containing the data to
  646. send.
  647. Return Value:
  648. Status code.
  649. --*/
  650. {
  651. UINTN BytesComplete;
  652. PNETWORK_ADDRESS Destination;
  653. NETWORK_ADDRESS DestinationLocal;
  654. ULONG Flags;
  655. ULONG FooterSize;
  656. ULONG HeaderSize;
  657. PNET_LINK Link;
  658. NET_LINK_LOCAL_ADDRESS LinkInformation;
  659. PNET_SOCKET_LINK_OVERRIDE LinkOverride;
  660. NET_SOCKET_LINK_OVERRIDE LinkOverrideBuffer;
  661. PNET_PACKET_BUFFER Packet;
  662. NET_PACKET_LIST PacketList;
  663. PRAW_SOCKET RawSocket;
  664. UINTN Size;
  665. KSTATUS Status;
  666. BytesComplete = 0;
  667. Flags = Parameters->SocketIoFlags;
  668. Parameters->SocketIoFlags = 0;
  669. Link = NULL;
  670. LinkInformation.Link = NULL;
  671. LinkOverride = NULL;
  672. NET_INITIALIZE_PACKET_LIST(&PacketList);
  673. Size = Parameters->Size;
  674. RawSocket = (PRAW_SOCKET)Socket;
  675. Destination = Parameters->NetworkAddress;
  676. if ((Destination != NULL) && (FromKernelMode == FALSE)) {
  677. Status = MmCopyFromUserMode(&DestinationLocal,
  678. Destination,
  679. sizeof(NETWORK_ADDRESS));
  680. Destination = &DestinationLocal;
  681. if (!KSUCCESS(Status)) {
  682. goto RawSendEnd;
  683. }
  684. }
  685. if ((Destination == NULL) ||
  686. (Destination->Domain == NetDomainInvalid)) {
  687. if (Socket->RemoteAddress.Port == 0) {
  688. Status = STATUS_NOT_CONFIGURED;
  689. goto RawSendEnd;
  690. }
  691. Destination = &(Socket->RemoteAddress);
  692. }
  693. //
  694. // Fail if the socket has already been closed for writing.
  695. //
  696. if ((RawSocket->ShutdownTypes & SOCKET_SHUTDOWN_WRITE) != 0) {
  697. if ((Flags & SOCKET_IO_NO_SIGNAL) != 0) {
  698. Status = STATUS_BROKEN_PIPE_SILENT;
  699. } else {
  700. Status = STATUS_BROKEN_PIPE;
  701. }
  702. goto RawSendEnd;
  703. }
  704. //
  705. // Fail if the socket's link went down.
  706. //
  707. if ((Socket->KernelSocket.IoState->Events & POLL_EVENT_DISCONNECTED) != 0) {
  708. Status = STATUS_NO_NETWORK_CONNECTION;
  709. goto RawSendEnd;
  710. }
  711. //
  712. // Fail if there's ancillary data.
  713. //
  714. if (Parameters->ControlDataSize != 0) {
  715. Status = STATUS_NOT_SUPPORTED;
  716. goto RawSendEnd;
  717. }
  718. //
  719. // If the socket has no link, then try to find a link that can service the
  720. // destination address.
  721. //
  722. if (Socket->Link == NULL) {
  723. Status = NetFindLinkForRemoteAddress(Destination, &LinkInformation);
  724. if (KSUCCESS(Status)) {
  725. //
  726. // Synchronously get the correct header, footer, and max packet
  727. // sizes.
  728. //
  729. Status = NetInitializeSocketLinkOverride(Socket,
  730. &LinkInformation,
  731. &LinkOverrideBuffer);
  732. if (KSUCCESS(Status)) {
  733. LinkOverride = &LinkOverrideBuffer;
  734. }
  735. }
  736. if (!KSUCCESS(Status) && (Status != STATUS_CONNECTION_EXISTS)) {
  737. goto RawSendEnd;
  738. }
  739. }
  740. //
  741. // Set the necessary local variables based on whether the socket's link or
  742. // an override link will be used to send the data.
  743. //
  744. if (LinkOverride != NULL) {
  745. ASSERT(LinkOverride == &LinkOverrideBuffer);
  746. Link = LinkOverrideBuffer.LinkInformation.Link;
  747. HeaderSize = LinkOverrideBuffer.PacketSizeInformation.HeaderSize;
  748. FooterSize = LinkOverrideBuffer.PacketSizeInformation.FooterSize;
  749. } else {
  750. ASSERT(Socket->Link != NULL);
  751. Link = Socket->Link;
  752. HeaderSize = Socket->PacketSizeInformation.HeaderSize;
  753. FooterSize = Socket->PacketSizeInformation.FooterSize;
  754. }
  755. //
  756. // Allocate a buffer for the packet.
  757. //
  758. Status = NetAllocateBuffer(HeaderSize,
  759. Size,
  760. FooterSize,
  761. Link,
  762. 0,
  763. &Packet);
  764. if (!KSUCCESS(Status)) {
  765. goto RawSendEnd;
  766. }
  767. NET_ADD_PACKET_TO_LIST(Packet, &PacketList);
  768. //
  769. // Copy the packet data.
  770. //
  771. Status = MmCopyIoBufferData(IoBuffer,
  772. Packet->Buffer + Packet->DataOffset,
  773. BytesComplete,
  774. Size - BytesComplete,
  775. FALSE);
  776. if (!KSUCCESS(Status)) {
  777. goto RawSendEnd;
  778. }
  779. //
  780. // Send the datagram down to the network layer, which may have to send it
  781. // in fragments.
  782. //
  783. Status = Socket->Network->Interface.Send(Socket,
  784. Destination,
  785. LinkOverride,
  786. &PacketList);
  787. if (!KSUCCESS(Status)) {
  788. goto RawSendEnd;
  789. }
  790. Packet = NULL;
  791. BytesComplete = Size;
  792. RawSendEnd:
  793. Parameters->BytesCompleted = BytesComplete;
  794. if (!KSUCCESS(Status)) {
  795. NetDestroyBufferList(&PacketList);
  796. }
  797. if (LinkInformation.Link != NULL) {
  798. NetLinkReleaseReference(LinkInformation.Link);
  799. }
  800. if (LinkOverride == &(LinkOverrideBuffer)) {
  801. ASSERT(LinkOverrideBuffer.LinkInformation.Link != NULL);
  802. NetLinkReleaseReference(LinkOverrideBuffer.LinkInformation.Link);
  803. }
  804. return Status;
  805. }
  806. VOID
  807. NetpRawProcessReceivedData (
  808. PNET_RECEIVE_CONTEXT ReceiveContext
  809. )
  810. /*++
  811. Routine Description:
  812. This routine is called to process a received packet.
  813. Arguments:
  814. ReceiveContext - Supplies a pointer to the receive context that stores the
  815. link, packet, network, protocol, and source and destination addresses.
  816. Return Value:
  817. None. When the function returns, the memory associated with the packet may
  818. be reclaimed and reused.
  819. --*/
  820. {
  821. //
  822. // No incoming packets should match the raw socket protocol. Raw sockets
  823. // receive data directly from net core and not their networking layer.
  824. //
  825. ASSERT(FALSE);
  826. return;
  827. }
  828. KSTATUS
  829. NetpRawProcessReceivedSocketData (
  830. PNET_SOCKET Socket,
  831. PNET_RECEIVE_CONTEXT ReceiveContext
  832. )
  833. /*++
  834. Routine Description:
  835. This routine is called for a particular socket to process a received packet
  836. that was sent to it.
  837. Arguments:
  838. Socket - Supplies a pointer to the socket that received the packet.
  839. ReceiveContext - Supplies a pointer to the receive context that stores the
  840. link, packet, network, protocol, and source and destination addresses.
  841. Return Value:
  842. Status code.
  843. --*/
  844. {
  845. ULONG AllocationSize;
  846. ULONG Length;
  847. PNET_PACKET_BUFFER Packet;
  848. PRAW_RECEIVED_PACKET RawPacket;
  849. PRAW_SOCKET RawSocket;
  850. KSTATUS Status;
  851. ASSERT(KeGetRunLevel() == RunLevelLow);
  852. ASSERT(Socket != NULL);
  853. RawSocket = (PRAW_SOCKET)Socket;
  854. Packet = ReceiveContext->Packet;
  855. Length = Packet->FooterOffset - Packet->DataOffset;
  856. //
  857. // Create a received packet entry for this data.
  858. //
  859. AllocationSize = sizeof(RAW_RECEIVED_PACKET) + Length;
  860. RawPacket = MmAllocatePagedPool(AllocationSize,
  861. RAW_PROTOCOL_ALLOCATION_TAG);
  862. if (RawPacket == NULL) {
  863. Status = STATUS_INSUFFICIENT_RESOURCES;
  864. goto ProcessReceivedKernelDataEnd;
  865. }
  866. RtlCopyMemory(&(RawPacket->Address),
  867. ReceiveContext->Source,
  868. sizeof(NETWORK_ADDRESS));
  869. RawPacket->DataBuffer = (PVOID)(RawPacket + 1);
  870. RawPacket->Size = Length;
  871. //
  872. // Copy the packet contents into the receive packet buffer.
  873. //
  874. RtlCopyMemory(RawPacket->DataBuffer,
  875. Packet->Buffer + Packet->DataOffset,
  876. Length);
  877. //
  878. // Work to insert the packet on the list of received packets.
  879. //
  880. KeAcquireQueuedLock(RawSocket->ReceiveLock);
  881. if (RawPacket->Size <= RawSocket->ReceiveBufferFreeSize) {
  882. INSERT_BEFORE(&(RawPacket->ListEntry),
  883. &(RawSocket->ReceivedPacketList));
  884. RawSocket->ReceiveBufferFreeSize -= RawPacket->Size;
  885. ASSERT(RawSocket->ReceiveBufferFreeSize <
  886. RawSocket->ReceiveBufferTotalSize);
  887. //
  888. // One packet is always enough to notify a waiting receiver.
  889. //
  890. IoSetIoObjectState(Socket->KernelSocket.IoState, POLL_EVENT_IN, TRUE);
  891. RawPacket = NULL;
  892. } else {
  893. RawSocket->DroppedPacketCount += 1;
  894. }
  895. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  896. //
  897. // If the packet wasn't nulled out, that's an indication it wasn't added to
  898. // the list, so free it up.
  899. //
  900. if (RawPacket != NULL) {
  901. MmFreePagedPool(RawPacket);
  902. }
  903. Status = STATUS_SUCCESS;
  904. ProcessReceivedKernelDataEnd:
  905. return Status;
  906. }
  907. KSTATUS
  908. NetpRawReceive (
  909. BOOL FromKernelMode,
  910. PNET_SOCKET Socket,
  911. PSOCKET_IO_PARAMETERS Parameters,
  912. PIO_BUFFER IoBuffer
  913. )
  914. /*++
  915. Routine Description:
  916. This routine is called by the user to receive data from the socket on a
  917. particular protocol.
  918. Arguments:
  919. FromKernelMode - Supplies a boolean indicating whether the request is
  920. coming from kernel mode (TRUE) or user mode (FALSE).
  921. Socket - Supplies a pointer to the socket to receive data from.
  922. Parameters - Supplies a pointer to the socket I/O parameters.
  923. IoBuffer - Supplies a pointer to the I/O buffer where the received data
  924. will be returned.
  925. Return Value:
  926. STATUS_SUCCESS if any bytes were read.
  927. STATUS_TIMEOUT if the request timed out.
  928. STATUS_BUFFER_TOO_SMALL if the incoming datagram was too large for the
  929. buffer. The remainder of the datagram is discarded in this case.
  930. Other error codes on other failures.
  931. --*/
  932. {
  933. UINTN BytesComplete;
  934. ULONG CopySize;
  935. ULONGLONG CurrentTime;
  936. ULONGLONG EndTime;
  937. ULONG Flags;
  938. BOOL LockHeld;
  939. PRAW_RECEIVED_PACKET Packet;
  940. PLIST_ENTRY PacketEntry;
  941. PRAW_SOCKET RawSocket;
  942. ULONG ReturnedEvents;
  943. ULONG ReturnSize;
  944. UINTN Size;
  945. KSTATUS Status;
  946. ULONGLONG TimeCounterFrequency;
  947. ULONG Timeout;
  948. ULONG WaitTime;
  949. ASSERT(KeGetRunLevel() == RunLevelLow);
  950. BytesComplete = 0;
  951. EndTime = 0;
  952. LockHeld = FALSE;
  953. Flags = Parameters->SocketIoFlags;
  954. Parameters->SocketIoFlags = 0;
  955. Size = Parameters->Size;
  956. TimeCounterFrequency = 0;
  957. Timeout = Parameters->TimeoutInMilliseconds;
  958. RawSocket = (PRAW_SOCKET)Socket;
  959. if ((Flags & SOCKET_IO_OUT_OF_BAND) != 0) {
  960. Status = STATUS_NOT_SUPPORTED;
  961. goto RawReceiveEnd;
  962. }
  963. //
  964. // Fail if there's ancillary data.
  965. //
  966. if (Parameters->ControlDataSize != 0) {
  967. Status = STATUS_NOT_SUPPORTED;
  968. goto RawReceiveEnd;
  969. }
  970. //
  971. // Set a timeout timer to give up on. The socket stores the maximum timeout.
  972. //
  973. if (Timeout > RawSocket->ReceiveTimeout) {
  974. Timeout = RawSocket->ReceiveTimeout;
  975. }
  976. if ((Timeout != 0) && (Timeout != WAIT_TIME_INDEFINITE)) {
  977. EndTime = KeGetRecentTimeCounter();
  978. EndTime += KeConvertMicrosecondsToTimeTicks(
  979. Timeout * MICROSECONDS_PER_MILLISECOND);
  980. TimeCounterFrequency = HlQueryTimeCounterFrequency();
  981. }
  982. //
  983. // Loop trying to get some data. This loop exits once one packet is read.
  984. //
  985. while (TRUE) {
  986. //
  987. // Wait for a packet to become available. Start by computing the wait
  988. // time.
  989. //
  990. if (Timeout == 0) {
  991. WaitTime = 0;
  992. } else if (Timeout != WAIT_TIME_INDEFINITE) {
  993. CurrentTime = KeGetRecentTimeCounter();
  994. WaitTime = (EndTime - CurrentTime) * MILLISECONDS_PER_SECOND /
  995. TimeCounterFrequency;
  996. } else {
  997. WaitTime = WAIT_TIME_INDEFINITE;
  998. }
  999. //
  1000. // Wait for something to maybe become available. If the wait fails due
  1001. // to a timeout, interruption, or something else, then fail out.
  1002. // Otherwise when the read event is signalled, there are at least one
  1003. // packet available.
  1004. //
  1005. Status = IoWaitForIoObjectState(Socket->KernelSocket.IoState,
  1006. POLL_EVENT_IN,
  1007. TRUE,
  1008. WaitTime,
  1009. &ReturnedEvents);
  1010. if (!KSUCCESS(Status)) {
  1011. goto RawReceiveEnd;
  1012. }
  1013. if ((ReturnedEvents & POLL_ERROR_EVENTS) != 0) {
  1014. if ((ReturnedEvents & POLL_EVENT_DISCONNECTED) != 0) {
  1015. Status = STATUS_NO_NETWORK_CONNECTION;
  1016. } else {
  1017. Status = NET_SOCKET_GET_LAST_ERROR(Socket);
  1018. if (KSUCCESS(Status)) {
  1019. Status = STATUS_DEVICE_IO_ERROR;
  1020. }
  1021. }
  1022. goto RawReceiveEnd;
  1023. }
  1024. KeAcquireQueuedLock(RawSocket->ReceiveLock);
  1025. LockHeld = TRUE;
  1026. //
  1027. // Fail with EOF if the socket has already been closed for reading.
  1028. //
  1029. if ((RawSocket->ShutdownTypes & SOCKET_SHUTDOWN_READ) != 0) {
  1030. Status = STATUS_END_OF_FILE;
  1031. goto RawReceiveEnd;
  1032. }
  1033. //
  1034. // If another thread beat this one to the punch, try again.
  1035. //
  1036. if (LIST_EMPTY(&(RawSocket->ReceivedPacketList)) != FALSE) {
  1037. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  1038. LockHeld = FALSE;
  1039. continue;
  1040. }
  1041. //
  1042. // This should be the first packet being read.
  1043. //
  1044. ASSERT(BytesComplete == 0);
  1045. PacketEntry = RawSocket->ReceivedPacketList.Next;
  1046. Packet = LIST_VALUE(PacketEntry, RAW_RECEIVED_PACKET, ListEntry);
  1047. ReturnSize = Packet->Size;
  1048. CopySize = ReturnSize;
  1049. if (CopySize > Size) {
  1050. Parameters->SocketIoFlags |= SOCKET_IO_DATA_TRUNCATED;
  1051. CopySize = Size;
  1052. //
  1053. // The real packet size is only returned to the user on truncation
  1054. // if the truncated flag was supplied to this routine. Default to
  1055. // returning the truncated size.
  1056. //
  1057. if ((Flags & SOCKET_IO_DATA_TRUNCATED) == 0) {
  1058. ReturnSize = CopySize;
  1059. }
  1060. }
  1061. Status = MmCopyIoBufferData(IoBuffer,
  1062. Packet->DataBuffer,
  1063. 0,
  1064. CopySize,
  1065. TRUE);
  1066. if (!KSUCCESS(Status)) {
  1067. goto RawReceiveEnd;
  1068. }
  1069. //
  1070. // Copy the packet address out to the caller if requested.
  1071. //
  1072. if (Parameters->NetworkAddress != NULL) {
  1073. if (FromKernelMode != FALSE) {
  1074. RtlCopyMemory(Parameters->NetworkAddress,
  1075. &(Packet->Address),
  1076. sizeof(NETWORK_ADDRESS));
  1077. } else {
  1078. Status = MmCopyToUserMode(Parameters->NetworkAddress,
  1079. &(Packet->Address),
  1080. sizeof(NETWORK_ADDRESS));
  1081. if (!KSUCCESS(Status)) {
  1082. goto RawReceiveEnd;
  1083. }
  1084. }
  1085. }
  1086. BytesComplete = ReturnSize;
  1087. //
  1088. // Remove the packet if not peeking.
  1089. //
  1090. if ((Flags & SOCKET_IO_PEEK) == 0) {
  1091. LIST_REMOVE(&(Packet->ListEntry));
  1092. RawSocket->ReceiveBufferFreeSize += Packet->Size;
  1093. //
  1094. // The total receive buffer size may have been decreased. Don't
  1095. // increment the free size above the total.
  1096. //
  1097. if (RawSocket->ReceiveBufferFreeSize >
  1098. RawSocket->ReceiveBufferTotalSize) {
  1099. RawSocket->ReceiveBufferFreeSize =
  1100. RawSocket->ReceiveBufferTotalSize;
  1101. }
  1102. MmFreePagedPool(Packet);
  1103. //
  1104. // Unsignal the IN event if there are no more packets.
  1105. //
  1106. if (LIST_EMPTY(&(RawSocket->ReceivedPacketList)) != FALSE) {
  1107. IoSetIoObjectState(Socket->KernelSocket.IoState,
  1108. POLL_EVENT_IN,
  1109. FALSE);
  1110. }
  1111. }
  1112. //
  1113. // Wait-all does not apply to raw sockets. Break out.
  1114. //
  1115. Status = STATUS_SUCCESS;
  1116. break;
  1117. }
  1118. RawReceiveEnd:
  1119. if (LockHeld != FALSE) {
  1120. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  1121. }
  1122. Parameters->BytesCompleted = BytesComplete;
  1123. return Status;
  1124. }
  1125. KSTATUS
  1126. NetpRawGetSetInformation (
  1127. PNET_SOCKET Socket,
  1128. SOCKET_INFORMATION_TYPE InformationType,
  1129. UINTN Option,
  1130. PVOID Data,
  1131. PUINTN DataSize,
  1132. BOOL Set
  1133. )
  1134. /*++
  1135. Routine Description:
  1136. This routine gets or sets properties of the given socket.
  1137. Arguments:
  1138. Socket - Supplies a pointer to the socket to get or set information for.
  1139. InformationType - Supplies the socket information type category to which
  1140. specified option belongs.
  1141. Option - Supplies the option to get or set, which is specific to the
  1142. information type. The type of this value is generally
  1143. SOCKET_<information_type>_OPTION.
  1144. Data - Supplies a pointer to the data buffer where the data is either
  1145. returned for a get operation or given for a set operation.
  1146. DataSize - Supplies a pointer that on input constains the size of the data
  1147. buffer. On output, this contains the required size of the data buffer.
  1148. Set - Supplies a boolean indicating if this is a get operation (FALSE) or
  1149. a set operation (TRUE).
  1150. Return Value:
  1151. STATUS_SUCCESS on success.
  1152. STATUS_INVALID_PARAMETER if the information type is incorrect.
  1153. STATUS_BUFFER_TOO_SMALL if the data buffer is too small to receive the
  1154. requested option.
  1155. STATUS_NOT_SUPPORTED_BY_PROTOCOL if the socket option is not supported by
  1156. the socket.
  1157. STATUS_NOT_HANDLED if the protocol does not override the default behavior
  1158. for a basic socket option.
  1159. --*/
  1160. {
  1161. ULONG Count;
  1162. ULONG Index;
  1163. LONGLONG Milliseconds;
  1164. PRAW_SOCKET_OPTION RawOption;
  1165. PRAW_SOCKET RawSocket;
  1166. PNET_PACKET_SIZE_INFORMATION SizeInformation;
  1167. ULONG SizeOption;
  1168. PSOCKET_TIME SocketTime;
  1169. SOCKET_TIME SocketTimeBuffer;
  1170. PVOID Source;
  1171. KSTATUS Status;
  1172. RawSocket = (PRAW_SOCKET)Socket;
  1173. if ((InformationType != SocketInformationBasic) &&
  1174. (InformationType != SocketInformationUdp)) {
  1175. Status = STATUS_NOT_SUPPORTED;
  1176. goto RawGetSetInformationEnd;
  1177. }
  1178. //
  1179. // Search to see if the socket option is supported by the raw protocol.
  1180. //
  1181. Count = sizeof(NetRawSocketOptions) / sizeof(NetRawSocketOptions[0]);
  1182. for (Index = 0; Index < Count; Index += 1) {
  1183. RawOption = &(NetRawSocketOptions[Index]);
  1184. if ((RawOption->InformationType == InformationType) &&
  1185. (RawOption->Option == Option)) {
  1186. break;
  1187. }
  1188. }
  1189. if (Index == Count) {
  1190. if (InformationType == SocketInformationBasic) {
  1191. Status = STATUS_NOT_HANDLED;
  1192. } else {
  1193. Status = STATUS_NOT_SUPPORTED_BY_PROTOCOL;
  1194. }
  1195. goto RawGetSetInformationEnd;
  1196. }
  1197. //
  1198. // Handle failure cases common to all options.
  1199. //
  1200. if (Set != FALSE) {
  1201. if (RawOption->SetAllowed == FALSE) {
  1202. Status = STATUS_NOT_SUPPORTED_BY_PROTOCOL;
  1203. goto RawGetSetInformationEnd;
  1204. }
  1205. if (*DataSize < RawOption->Size) {
  1206. *DataSize = RawOption->Size;
  1207. Status = STATUS_BUFFER_TOO_SMALL;
  1208. goto RawGetSetInformationEnd;
  1209. }
  1210. }
  1211. //
  1212. // There are currently no raw protocol options.
  1213. //
  1214. ASSERT(InformationType != SocketInformationRaw);
  1215. //
  1216. // Parse the basic socket option, getting the information from the raw
  1217. // socket or setting the new state in the raw socket.
  1218. //
  1219. Source = NULL;
  1220. Status = STATUS_SUCCESS;
  1221. switch ((SOCKET_BASIC_OPTION)Option) {
  1222. case SocketBasicOptionSendBufferSize:
  1223. if (Set != FALSE) {
  1224. SizeOption = *((PULONG)Data);
  1225. if (SizeOption > SOCKET_OPTION_MAX_ULONG) {
  1226. SizeOption = SOCKET_OPTION_MAX_ULONG;
  1227. }
  1228. SizeInformation = &(Socket->PacketSizeInformation);
  1229. if (SizeOption > RAW_MAX_PACKET_SIZE) {
  1230. SizeOption = RAW_MAX_PACKET_SIZE;
  1231. } else if (SizeOption < SizeInformation->MaxPacketSize) {
  1232. SizeOption = SizeInformation->MaxPacketSize;
  1233. }
  1234. RawSocket->MaxPacketSize = SizeOption;
  1235. } else {
  1236. Source = &SizeOption;
  1237. SizeOption = RawSocket->MaxPacketSize;
  1238. }
  1239. break;
  1240. case SocketBasicOptionSendMinimum:
  1241. ASSERT(Set == FALSE);
  1242. Source = &SizeOption;
  1243. SizeOption = RAW_SEND_MINIMUM;
  1244. break;
  1245. case SocketBasicOptionReceiveBufferSize:
  1246. if (Set != FALSE) {
  1247. SizeOption = *((PULONG)Data);
  1248. if (SizeOption < RAW_MIN_RECEIVE_BUFFER_SIZE) {
  1249. SizeOption = RAW_MIN_RECEIVE_BUFFER_SIZE;
  1250. } else if (SizeOption > SOCKET_OPTION_MAX_ULONG) {
  1251. SizeOption = SOCKET_OPTION_MAX_ULONG;
  1252. }
  1253. //
  1254. // Set the receive buffer size and truncate the available free
  1255. // space if necessary. Do not remove any packets that have already
  1256. // been received. This is not meant to be a truncate call.
  1257. //
  1258. KeAcquireQueuedLock(RawSocket->ReceiveLock);
  1259. RawSocket->ReceiveBufferTotalSize = SizeOption;
  1260. if (RawSocket->ReceiveBufferFreeSize > SizeOption) {
  1261. RawSocket->ReceiveBufferFreeSize = SizeOption;
  1262. }
  1263. KeReleaseQueuedLock(RawSocket->ReceiveLock);
  1264. } else {
  1265. Source = &SizeOption;
  1266. SizeOption = RawSocket->ReceiveBufferTotalSize;
  1267. }
  1268. break;
  1269. case SocketBasicOptionReceiveMinimum:
  1270. if (Set != FALSE) {
  1271. SizeOption = *((PULONG)Data);
  1272. if (SizeOption > SOCKET_OPTION_MAX_ULONG) {
  1273. SizeOption = SOCKET_OPTION_MAX_ULONG;
  1274. }
  1275. RawSocket->ReceiveMinimum = SizeOption;
  1276. } else {
  1277. Source = &SizeOption;
  1278. SizeOption = RawSocket->ReceiveMinimum;
  1279. }
  1280. break;
  1281. case SocketBasicOptionReceiveTimeout:
  1282. if (Set != FALSE) {
  1283. SocketTime = (PSOCKET_TIME)Data;
  1284. if (SocketTime->Seconds < 0) {
  1285. Status = STATUS_DOMAIN_ERROR;
  1286. break;
  1287. }
  1288. Milliseconds = SocketTime->Seconds * MILLISECONDS_PER_SECOND;
  1289. if (Milliseconds < SocketTime->Seconds) {
  1290. Status = STATUS_DOMAIN_ERROR;
  1291. break;
  1292. }
  1293. Milliseconds += SocketTime->Microseconds /
  1294. MICROSECONDS_PER_MILLISECOND;
  1295. if ((Milliseconds < 0) || (Milliseconds > MAX_LONG)) {
  1296. Status = STATUS_DOMAIN_ERROR;
  1297. break;
  1298. }
  1299. RawSocket->ReceiveTimeout = (ULONG)(LONG)Milliseconds;
  1300. } else {
  1301. Source = &SocketTimeBuffer;
  1302. if (RawSocket->ReceiveTimeout == WAIT_TIME_INDEFINITE) {
  1303. SocketTimeBuffer.Seconds = 0;
  1304. SocketTimeBuffer.Microseconds = 0;
  1305. } else {
  1306. SocketTimeBuffer.Seconds = RawSocket->ReceiveTimeout /
  1307. MILLISECONDS_PER_SECOND;
  1308. SocketTimeBuffer.Microseconds = (RawSocket->ReceiveTimeout %
  1309. MILLISECONDS_PER_SECOND) *
  1310. MICROSECONDS_PER_MILLISECOND;
  1311. }
  1312. }
  1313. break;
  1314. default:
  1315. ASSERT(FALSE);
  1316. Status = STATUS_NOT_SUPPORTED;
  1317. break;
  1318. }
  1319. if (!KSUCCESS(Status)) {
  1320. goto RawGetSetInformationEnd;
  1321. }
  1322. //
  1323. // Truncate all copies for get requests down to the required size and only
  1324. // return the required size on set requests.
  1325. //
  1326. if (*DataSize > RawOption->Size) {
  1327. *DataSize = RawOption->Size;
  1328. }
  1329. //
  1330. // For get requests, copy the gathered information to the supplied data
  1331. // buffer.
  1332. //
  1333. if (Set == FALSE) {
  1334. ASSERT(Source != NULL);
  1335. RtlCopyMemory(Data, Source, *DataSize);
  1336. //
  1337. // If the copy truncated the data, report that the given buffer was too
  1338. // small. The caller can choose to ignore this if the truncated data is
  1339. // enough.
  1340. //
  1341. if (*DataSize < RawOption->Size) {
  1342. *DataSize = RawOption->Size;
  1343. Status = STATUS_BUFFER_TOO_SMALL;
  1344. goto RawGetSetInformationEnd;
  1345. }
  1346. }
  1347. RawGetSetInformationEnd:
  1348. return Status;
  1349. }
  1350. KSTATUS
  1351. NetpRawUserControl (
  1352. PNET_SOCKET Socket,
  1353. ULONG CodeNumber,
  1354. BOOL FromKernelMode,
  1355. PVOID ContextBuffer,
  1356. UINTN ContextBufferSize
  1357. )
  1358. /*++
  1359. Routine Description:
  1360. This routine handles user control requests destined for a socket.
  1361. Arguments:
  1362. Socket - Supplies a pointer to the socket.
  1363. CodeNumber - Supplies the minor code of the request.
  1364. FromKernelMode - Supplies a boolean indicating whether or not this request
  1365. (and the buffer associated with it) originates from user mode (FALSE)
  1366. or kernel mode (TRUE).
  1367. ContextBuffer - Supplies a pointer to the context buffer allocated by the
  1368. caller for the request.
  1369. ContextBufferSize - Supplies the size of the supplied context buffer.
  1370. Return Value:
  1371. Status code.
  1372. --*/
  1373. {
  1374. return STATUS_NOT_SUPPORTED;
  1375. }
  1376. //
  1377. // --------------------------------------------------------- Internal Functions
  1378. //