raw.c 46 KB

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