netlink.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*++
  2. Copyright (c) 2016 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. netlink.c
  5. Abstract:
  6. This module implements the netlink library functions.
  7. Author:
  8. Chris Stevens 24-Mar-2016
  9. Environment:
  10. User Mode
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include "netlinkp.h"
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. //
  23. // This macro returns the aligned size of the netlink message buffer structure.
  24. //
  25. #define NL_MESSAGE_BUFFER_SIZE NETLINK_ALIGN(sizeof(NL_MESSAGE_BUFFER))
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. //
  30. // ------------------------------------------------------ Data Type Definitions
  31. //
  32. //
  33. // ----------------------------------------------- Internal Function Prototypes
  34. //
  35. KSTATUS
  36. NetlinkpConvertToNetworkAddress (
  37. const struct sockaddr *Address,
  38. socklen_t AddressLength,
  39. PNETWORK_ADDRESS NetworkAddress
  40. );
  41. /*++
  42. Routine Description:
  43. This routine converts a sockaddr address structure into a network address
  44. structure.
  45. Arguments:
  46. Address - Supplies a pointer to the address structure.
  47. AddressLength - Supplies the length of the address structure in bytes.
  48. NetworkAddress - Supplies a pointer where the corresponding network address
  49. will be returned.
  50. Return Value:
  51. STATUS_SUCCESS on success.
  52. STATUS_INVALID_ADDRESS on failure.
  53. --*/
  54. KSTATUS
  55. NetlinkpConvertFromNetworkAddress (
  56. PNETWORK_ADDRESS NetworkAddress,
  57. struct sockaddr *Address,
  58. socklen_t *AddressLength
  59. );
  60. /*++
  61. Routine Description:
  62. This routine converts a network address structure into a sockaddr structure.
  63. Arguments:
  64. NetworkAddress - Supplies a pointer to the network address to convert.
  65. Address - Supplies a pointer where the address structure will be returned.
  66. AddressLength - Supplies a pointer that on input contains the length of the
  67. specified Address structure, and on output returns the length of the
  68. returned address. If the supplied buffer is not big enough to hold the
  69. address, the address is truncated, and the larger needed buffer size
  70. will be returned here.
  71. Return Value:
  72. STATUS_SUCCESS on success.
  73. STATUS_BUFFER_TOO_SMALL if the address buffer is not big enough.
  74. STATUS_INVALID_ADDRESS on failure.
  75. --*/
  76. //
  77. // -------------------------------------------------------------------- Globals
  78. //
  79. CL_NETWORK_CONVERSION_INTERFACE NetlinkAddressConversionInterface = {
  80. CL_NETWORK_CONVERSION_INTERFACE_VERSION,
  81. AF_NETLINK,
  82. NetDomainNetlink,
  83. NetlinkpConvertToNetworkAddress,
  84. NetlinkpConvertFromNetworkAddress
  85. };
  86. //
  87. // ------------------------------------------------------------------ Functions
  88. //
  89. LIBNETLINK_API
  90. VOID
  91. NlInitialize (
  92. PVOID Environment
  93. )
  94. /*++
  95. Routine Description:
  96. This routine initializes the Minoca Netlink Library. This routine is
  97. normally called by statically linked assembly within a program, and unless
  98. developing outside the usual paradigm should not need to call this routine
  99. directly.
  100. Arguments:
  101. Environment - Supplies a pointer to the environment information.
  102. Return Value:
  103. None.
  104. --*/
  105. {
  106. ClRegisterTypeConversionInterface(ClConversionNetworkAddress,
  107. &NetlinkAddressConversionInterface,
  108. TRUE);
  109. return;
  110. }
  111. LIBNETLINK_API
  112. INT
  113. NlCreateSocket (
  114. ULONG Protocol,
  115. ULONG PortId,
  116. ULONG Flags,
  117. PNL_SOCKET *NewSocket
  118. )
  119. /*++
  120. Routine Description:
  121. This routine creates a netlink socket with the given protocol and port ID.
  122. Arguments:
  123. Protocol - Supplies the netlink protocol to use for the socket.
  124. PortId - Supplies a specific port ID to use for the socket, if available.
  125. Supply NL_ANY_PORT_ID to have the socket dynamically bind to an
  126. available port ID.
  127. Flags - Supplies a bitmask of netlink socket flags. See NL_SOCKET_FLAG_*
  128. for definitions.
  129. NewSocket - Supplies a pointer that receives a pointer to the newly created
  130. socket.
  131. Return Value:
  132. 0 on success.
  133. -1 on error, and the errno variable will be set to contain more information.
  134. --*/
  135. {
  136. socklen_t AddressLength;
  137. PNL_SOCKET Socket;
  138. INT Status;
  139. Socket = malloc(sizeof(NL_SOCKET));
  140. if (Socket == NULL) {
  141. errno = ENOMEM;
  142. return -1;
  143. }
  144. memset(Socket, 0, sizeof(NL_SOCKET));
  145. Status = NlAllocateBuffer(NETLINK_SCRATCH_BUFFER_SIZE,
  146. &(Socket->ReceiveBuffer));
  147. if (Status != 0) {
  148. goto CreateSocketEnd;
  149. }
  150. Socket->Protocol = Protocol;
  151. Socket->Flags = Flags;
  152. Socket->Socket = socket(AF_NETLINK, SOCK_DGRAM, Protocol);
  153. if (Socket->Socket == -1) {
  154. Status = -1;
  155. goto CreateSocketEnd;
  156. }
  157. //
  158. // Bind the socket. If the supplied port ID is NL_ANY_PORT_ID, then an
  159. // ephemeral port will be assign. Otherwise the socket will be bound to the
  160. // given port if it's available.
  161. //
  162. AddressLength = sizeof(struct sockaddr_nl);
  163. Socket->LocalAddress.nl_family = AF_NETLINK;
  164. Socket->LocalAddress.nl_pid = PortId;
  165. Status = bind(Socket->Socket,
  166. (struct sockaddr *)&(Socket->LocalAddress),
  167. AddressLength);
  168. if (Status != 0) {
  169. goto CreateSocketEnd;
  170. }
  171. Status = getsockname(Socket->Socket,
  172. (struct sockaddr *)&(Socket->LocalAddress),
  173. &AddressLength);
  174. if (Status != 0) {
  175. goto CreateSocketEnd;
  176. }
  177. ASSERT((Socket->LocalAddress.nl_pid != NL_ANY_PORT_ID) &&
  178. (Socket->LocalAddress.nl_pid != NETLINK_KERNEL_PORT_ID));
  179. CreateSocketEnd:
  180. if (Status != 0) {
  181. if (Socket != NULL) {
  182. NlDestroySocket(Socket);
  183. Socket = NULL;
  184. }
  185. }
  186. *NewSocket = Socket;
  187. return Status;
  188. }
  189. LIBNETLINK_API
  190. VOID
  191. NlDestroySocket (
  192. PNL_SOCKET Socket
  193. )
  194. /*++
  195. Routine Description:
  196. This routine destroys a netlink socket and all its resources.
  197. Arguments:
  198. Socket - Supplies a pointer to the netlink socket to destroy.
  199. Return Value:
  200. None.
  201. --*/
  202. {
  203. if (Socket->Socket != -1) {
  204. close(Socket->Socket);
  205. }
  206. if (Socket->ReceiveBuffer != NULL) {
  207. NlFreeBuffer(Socket->ReceiveBuffer);
  208. }
  209. free(Socket);
  210. return;
  211. }
  212. LIBNETLINK_API
  213. INT
  214. NlAllocateBuffer (
  215. ULONG Size,
  216. PNL_MESSAGE_BUFFER *NewBuffer
  217. )
  218. /*++
  219. Routine Description:
  220. This routine allocates a netlink message buffer. It always adds on space
  221. for the base netlink message header.
  222. Arguments:
  223. Size - Supplies the number of data bytes needed.
  224. NewBuffer - Supplies a pointer where a pointer to the new allocation will
  225. be returned on success.
  226. Return Value:
  227. 0 on success.
  228. -1 on error, and the errno variable will be set to contain more information.
  229. --*/
  230. {
  231. PNL_MESSAGE_BUFFER Buffer;
  232. INT Status;
  233. ULONG TotalSize;
  234. Buffer = NULL;
  235. Status = -1;
  236. if (Size != NETLINK_ALIGN(Size)) {
  237. errno = EINVAL;
  238. goto AllocateBufferEnd;
  239. }
  240. Size = NETLINK_ALIGN(Size);
  241. TotalSize = NETLINK_HEADER_LENGTH + Size;
  242. if (TotalSize < Size) {
  243. errno = EINVAL;
  244. goto AllocateBufferEnd;
  245. }
  246. Buffer = malloc(TotalSize + NL_MESSAGE_BUFFER_SIZE);
  247. if (Buffer == NULL) {
  248. errno = ENOMEM;
  249. goto AllocateBufferEnd;
  250. }
  251. Buffer->Buffer = (PVOID)Buffer + NL_MESSAGE_BUFFER_SIZE;
  252. Buffer->BufferSize = TotalSize;
  253. Buffer->DataSize = 0;
  254. Buffer->CurrentOffset = 0;
  255. Status = 0;
  256. AllocateBufferEnd:
  257. if (Status != 0) {
  258. if (Buffer != NULL) {
  259. free(Buffer);
  260. Buffer = NULL;
  261. }
  262. }
  263. *NewBuffer = Buffer;
  264. return Status;
  265. }
  266. LIBNETLINK_API
  267. VOID
  268. NlFreeBuffer (
  269. PNL_MESSAGE_BUFFER Buffer
  270. )
  271. /*++
  272. Routine Description:
  273. This routine frees a previously allocated netlink message buffer.
  274. Arguments:
  275. Buffer - Supplies a pointer to the buffer to be released.
  276. Return Value:
  277. None.
  278. --*/
  279. {
  280. free(Buffer);
  281. return;
  282. }
  283. LIBNETLINK_API
  284. INT
  285. NlAppendHeader (
  286. PNL_SOCKET Socket,
  287. PNL_MESSAGE_BUFFER Message,
  288. ULONG PayloadLength,
  289. ULONG SequenceNumber,
  290. USHORT Type,
  291. USHORT Flags
  292. )
  293. /*++
  294. Routine Description:
  295. This routine appends a base netlink header to the message. It will make
  296. sure there is enough room left in the supplied message buffer, add the
  297. header before at the current offset and update the offset and valid data
  298. size when complete. It always adds the ACK and REQUEST flags.
  299. Arguments:
  300. Socket - Supplies a pointer to the netlink socket that is sending the
  301. message.
  302. Message - Supplies a pointer to the netlink message buffer to which the
  303. header should be appended.
  304. PayloadLength - Supplies the length of the message data payload, in bytes.
  305. This does not include the base netlink header length.
  306. SequenceNumber - Supplies the sequence number for the message. This value
  307. is ignored unless NL_SOCKET_FLAG_NO_AUTO_SEQUENCE is set in the socket.
  308. Type - Supplies the netlink message type.
  309. Flags - Supplies a bitmask of netlink message flags. See
  310. NETLINK_HEADER_FLAG_* for definitions.
  311. Return Value:
  312. 0 on success.
  313. -1 on error, and the errno variable will be set to contain more information.
  314. --*/
  315. {
  316. PNETLINK_HEADER Header;
  317. ULONG MessageLength;
  318. PayloadLength += NETLINK_HEADER_LENGTH;
  319. MessageLength = Message->BufferSize - Message->CurrentOffset;
  320. if (MessageLength < PayloadLength) {
  321. errno = ENOBUFS;
  322. return -1;
  323. }
  324. Flags |= NETLINK_HEADER_FLAG_ACK | NETLINK_HEADER_FLAG_REQUEST;
  325. Header = Message->Buffer + Message->CurrentOffset;
  326. Header->Length = PayloadLength;
  327. Header->Type = Type;
  328. Header->Flags = Flags;
  329. if ((Socket->Flags & NL_SOCKET_FLAG_NO_AUTO_SEQUENCE) != 0) {
  330. Header->SequenceNumber = SequenceNumber;
  331. } else {
  332. Header->SequenceNumber = RtlAtomicAdd32(&(Socket->SendNextSequence), 1);
  333. }
  334. Header->PortId = Socket->LocalAddress.nl_pid;
  335. //
  336. // Move the offset and data size to the first byte after the header.
  337. //
  338. Message->CurrentOffset += NETLINK_HEADER_LENGTH;
  339. Message->DataSize += NETLINK_HEADER_LENGTH;
  340. return 0;
  341. }
  342. LIBNETLINK_API
  343. INT
  344. NlSendMessage (
  345. PNL_SOCKET Socket,
  346. PNL_MESSAGE_BUFFER Message,
  347. ULONG PortId,
  348. ULONG GroupMask,
  349. PULONG BytesSent
  350. )
  351. /*++
  352. Routine Description:
  353. This routine sends a netlink message for the given socket.
  354. Arguments:
  355. Socket - Supplies a pointer to the netlink socket over which to send the
  356. message.
  357. Message - Supplies a pointer to the message to be sent. The routine will
  358. attempt to send the entire message between the data offset and footer
  359. offset.
  360. PortId - Supplies the port ID of the recipient of the message.
  361. GroupMask - Supplies the group mask of the message recipients.
  362. BytesSent - Supplies an optional pointer the receives the number of bytes
  363. sent on success.
  364. Return Value:
  365. 0 on success.
  366. -1 on error, and the errno variable will be set to contain more information.
  367. --*/
  368. {
  369. struct sockaddr_nl Address;
  370. ssize_t LocalBytesSent;
  371. INT Status;
  372. memset(&Address, 0, sizeof(struct sockaddr_nl));
  373. Address.nl_family = AF_NETLINK;
  374. Address.nl_pid = PortId;
  375. Address.nl_groups = GroupMask;
  376. LocalBytesSent = sendto(Socket->Socket,
  377. Message->Buffer,
  378. Message->DataSize,
  379. 0,
  380. (struct sockaddr *)&Address,
  381. sizeof(struct sockaddr_nl));
  382. if (LocalBytesSent < 0) {
  383. Status = -1;
  384. } else {
  385. if (BytesSent != NULL) {
  386. *BytesSent = LocalBytesSent;
  387. }
  388. Status = 0;
  389. }
  390. return Status;
  391. }
  392. LIBNETLINK_API
  393. INT
  394. NlReceiveMessage (
  395. PNL_SOCKET Socket,
  396. PNL_RECEIVE_PARAMETERS Parameters
  397. )
  398. /*++
  399. Routine Description:
  400. This routine receives one or more netlink messages, does some simple
  401. validation, handles the default netlink message types, and calls the given
  402. receive routine callback for each protocol layer message.
  403. Arguments:
  404. Socket - Supplies a pointer to the netlink socket over which to receive the
  405. message.
  406. Parameters - Supplies a pointer to the receive message parameters.
  407. Return Value:
  408. 0 on success.
  409. -1 on error, and the errno variable will be set to contain more information.
  410. --*/
  411. {
  412. struct sockaddr_nl Address;
  413. socklen_t AddressLength;
  414. INTN BytesRemaining;
  415. PINT Error;
  416. ULONG ErrorLength;
  417. KSTATUS ErrorStatus;
  418. INT ErrorValue;
  419. ULONG Flags;
  420. BOOL GroupMatch;
  421. PNETLINK_HEADER Header;
  422. PNL_MESSAGE_BUFFER Message;
  423. BOOL Multipart;
  424. BOOL PortMatch;
  425. BOOL ReceiveMore;
  426. INT Status;
  427. Flags = Parameters->Flags;
  428. Parameters->Flags = 0;
  429. Message = Socket->ReceiveBuffer;
  430. Multipart = FALSE;
  431. ReceiveMore = TRUE;
  432. while ((ReceiveMore != FALSE) || (Multipart != FALSE)) {
  433. Message->CurrentOffset = 0;
  434. Message->DataSize = 0;
  435. AddressLength = sizeof(struct sockaddr_nl);
  436. BytesRemaining = recvfrom(Socket->Socket,
  437. Message->Buffer,
  438. Message->BufferSize,
  439. 0,
  440. (struct sockaddr *)&Address,
  441. &AddressLength);
  442. if (BytesRemaining < 0) {
  443. Status = -1;
  444. goto ReceiveMessageEnd;
  445. }
  446. Message->DataSize = BytesRemaining;
  447. if ((AddressLength != sizeof(struct sockaddr_nl)) ||
  448. (Address.nl_family != AF_NETLINK)) {
  449. errno = EAFNOSUPPORT;
  450. Status = -1;
  451. goto ReceiveMessageEnd;
  452. }
  453. //
  454. // If supplied, validate the port and/or group. Skipping any messages
  455. // that do not match at least one of them.
  456. //
  457. PortMatch = TRUE;
  458. GroupMatch = TRUE;
  459. if (((Flags & NL_RECEIVE_FLAG_PORT_ID) != 0) &&
  460. (Address.nl_pid != Parameters->PortId)) {
  461. PortMatch = FALSE;
  462. }
  463. if (((Flags & NL_RECEIVE_FLAG_GROUP_MASK) != 0) &&
  464. ((Address.nl_groups & Parameters->GroupMask) == 0)) {
  465. GroupMatch = FALSE;
  466. }
  467. if ((PortMatch == FALSE) && (GroupMatch == FALSE)) {
  468. continue;
  469. }
  470. //
  471. // If the caller is not expecting an ACK, then do not wait for one.
  472. //
  473. if ((Flags & NL_RECEIVE_FLAG_NO_ACK_WAIT) != 0) {
  474. ReceiveMore = FALSE;
  475. }
  476. //
  477. // Process each message in the buffer.
  478. //
  479. while (BytesRemaining > 0) {
  480. Header = Message->Buffer + Message->CurrentOffset;
  481. if ((BytesRemaining < NETLINK_HEADER_LENGTH) ||
  482. (BytesRemaining < Header->Length)) {
  483. break;
  484. }
  485. //
  486. // If there is no multi-part flag, then there shouldn't be a reason
  487. // to read another message.
  488. //
  489. if ((Header->Flags & NETLINK_HEADER_FLAG_MULTIPART) != 0) {
  490. Multipart = TRUE;
  491. }
  492. //
  493. // Validate the sequence number, but skip this on multicast
  494. // messages. Sequence numbers don't make much sense for out-of-band
  495. // multicast messages.
  496. //
  497. if (((Socket->Flags & NL_SOCKET_FLAG_NO_AUTO_SEQUENCE) == 0) &&
  498. (Address.nl_pid == Socket->LocalAddress.nl_pid) &&
  499. (Header->SequenceNumber != Socket->ReceiveNextSequence)) {
  500. errno = EILSEQ;
  501. Status = -1;
  502. goto ReceiveMessageEnd;
  503. }
  504. //
  505. // If an error is received, stop receiving. If an ACK is received,
  506. // finish processing this set of messages, but don't read any more.
  507. //
  508. if (Header->Type == NETLINK_MESSAGE_TYPE_ERROR) {
  509. if (((Socket->Flags & NL_SOCKET_FLAG_NO_AUTO_SEQUENCE) == 0) &&
  510. (Address.nl_pid == Socket->LocalAddress.nl_pid)) {
  511. RtlAtomicAdd32(&(Socket->ReceiveNextSequence), 1);
  512. }
  513. ErrorLength = NETLINK_HEADER_LENGTH +
  514. sizeof(NETLINK_ERROR_MESSAGE);
  515. if (Header->Length < ErrorLength) {
  516. errno = ENOMSG;
  517. Status = -1;
  518. goto ReceiveMessageEnd;
  519. }
  520. //
  521. // Convert from KSTATUS to the C library value for errno.
  522. //
  523. Error = NETLINK_DATA(Header);
  524. ErrorStatus = (KSTATUS)*Error;
  525. ErrorValue = ClConvertKstatusToErrorNumber(ErrorStatus);
  526. //
  527. // If the library consumer did not specifically ask for KSTATUS
  528. // errors, then all error messages need to be converted.
  529. //
  530. if ((Socket->Flags & NL_SOCKET_FLAG_REPORT_KSTATUS) == 0) {
  531. *Error = ErrorValue;
  532. }
  533. if (!KSUCCESS(ErrorStatus)) {
  534. errno = ErrorValue;
  535. Status = -1;
  536. goto ReceiveMessageEnd;
  537. }
  538. //
  539. // Receives should not exit until an ACK has been seen, unless
  540. // the caller specifically requested to not wait.
  541. //
  542. if ((Flags & NL_RECEIVE_FLAG_NO_ACK_WAIT) == 0) {
  543. ReceiveMore = FALSE;
  544. }
  545. Parameters->Flags |= NL_RECEIVE_FLAG_ACK_RECEIVED;
  546. //
  547. // If this is the last message in a multi-part message, then stop
  548. // receiving more data.
  549. //
  550. } else if (Header->Type == NETLINK_MESSAGE_TYPE_DONE) {
  551. Multipart = FALSE;
  552. //
  553. // For all protocol layer messages, invoke the given callback.
  554. //
  555. } else if ((Parameters->ReceiveRoutine != NULL) &&
  556. (Header->Type >=
  557. NETLINK_MESSAGE_TYPE_PROTOCOL_MINIMUM)) {
  558. Parameters->ReceiveRoutine(Socket,
  559. &(Parameters->ReceiveContext),
  560. Header);
  561. }
  562. //
  563. // Skip along to the next message.
  564. //
  565. Message->CurrentOffset += NETLINK_ALIGN(Header->Length);
  566. BytesRemaining -= NETLINK_ALIGN(Header->Length);
  567. }
  568. }
  569. Status = 0;
  570. ReceiveMessageEnd:
  571. return Status;
  572. }
  573. LIBNETLINK_API
  574. INT
  575. NlAppendAttribute (
  576. PNL_MESSAGE_BUFFER Message,
  577. USHORT Type,
  578. PVOID Data,
  579. USHORT DataLength
  580. )
  581. /*++
  582. Routine Description:
  583. This routine appends a netlink attribute to the given message. It validates
  584. that there is enough space for the attribute and moves the message buffer's
  585. offset to the first byte after the attribute. It also updates the buffer's
  586. valid data size. The exception to this rule is if a NULL data buffer is
  587. supplied; the buffer's data offset and size will only be updated for the
  588. attribute's header.
  589. Arguments:
  590. Message - Supplies a pointer to the netlink message buffer to which the
  591. attribute will be added.
  592. Type - Supplies the netlink attribute type.
  593. Data - Supplies an optional pointer to the attribute data to be stored in
  594. the buffer. Even if no data buffer is supplied, a data length may be
  595. supplied for the case of child attributes that are yet to be appended.
  596. DataLength - Supplies the length of the data, in bytes.
  597. Return Value:
  598. 0 on success.
  599. -1 on error, and the errno variable will be set to contain more information.
  600. --*/
  601. {
  602. PNETLINK_ATTRIBUTE Attribute;
  603. ULONG AttributeLength;
  604. ULONG MessageLength;
  605. AttributeLength = NETLINK_ATTRIBUTE_SIZE(DataLength);
  606. MessageLength = Message->BufferSize - Message->CurrentOffset;
  607. if (MessageLength < AttributeLength) {
  608. return ENOBUFS;
  609. }
  610. Attribute = Message->Buffer + Message->CurrentOffset;
  611. Attribute->Length = NETLINK_ATTRIBUTE_LENGTH(DataLength);
  612. Attribute->Type = Type;
  613. if (Data != NULL) {
  614. RtlCopyMemory(NETLINK_ATTRIBUTE_DATA(Attribute), Data, DataLength);
  615. Message->CurrentOffset += AttributeLength;
  616. Message->DataSize += AttributeLength;
  617. } else {
  618. Message->CurrentOffset += NETLINK_ATTRIBUTE_HEADER_LENGTH;
  619. Message->DataSize += NETLINK_ATTRIBUTE_HEADER_LENGTH;
  620. }
  621. return 0;
  622. }
  623. LIBNETLINK_API
  624. INT
  625. NlGetAttribute (
  626. PVOID Attributes,
  627. ULONG AttributesLength,
  628. USHORT Type,
  629. PVOID *Data,
  630. PUSHORT DataLength
  631. )
  632. /*++
  633. Routine Description:
  634. This routine parses the given attributes buffer and returns a pointer to
  635. the desired attribute.
  636. Arguments:
  637. Attributes - Supplies a pointer to the start of the generic command
  638. attributes.
  639. AttributesLength - Supplies the length of the attributes buffer, in bytes.
  640. Type - Supplies the netlink generic attribute type.
  641. Data - Supplies a pointer that receives a pointer to the data for the
  642. requested attribute type.
  643. DataLength - Supplies a pointer that receives the length of the requested
  644. attribute data.
  645. Return Value:
  646. 0 on success.
  647. -1 on error, and the errno variable will be set to contain more information.
  648. --*/
  649. {
  650. PNETLINK_ATTRIBUTE Attribute;
  651. ULONG AttributeSize;
  652. Attribute = (PNETLINK_ATTRIBUTE)Attributes;
  653. while (AttributesLength != 0) {
  654. if ((AttributesLength < NETLINK_ATTRIBUTE_HEADER_LENGTH) ||
  655. (AttributesLength < Attribute->Length)) {
  656. break;
  657. }
  658. if (Attribute->Type == Type) {
  659. *DataLength = Attribute->Length - NETLINK_ATTRIBUTE_HEADER_LENGTH;
  660. *Data = NETLINK_ATTRIBUTE_DATA(Attribute);
  661. return 0;
  662. }
  663. AttributeSize = NETLINK_ALIGN(Attribute->Length);
  664. Attribute = (PVOID)Attribute + AttributeSize;
  665. AttributesLength -= AttributeSize;
  666. }
  667. errno = ENOENT;
  668. return -1;
  669. }
  670. //
  671. // --------------------------------------------------------- Internal Functions
  672. //
  673. KSTATUS
  674. NetlinkpConvertToNetworkAddress (
  675. const struct sockaddr *Address,
  676. socklen_t AddressLength,
  677. PNETWORK_ADDRESS NetworkAddress
  678. )
  679. /*++
  680. Routine Description:
  681. This routine converts a sockaddr address structure into a network address
  682. structure.
  683. Arguments:
  684. Address - Supplies a pointer to the address structure.
  685. AddressLength - Supplies the length of the address structure in bytes.
  686. NetworkAddress - Supplies a pointer where the corresponding network address
  687. will be returned.
  688. Return Value:
  689. STATUS_SUCCESS on success.
  690. STATUS_INVALID_ADDRESS on failure.
  691. --*/
  692. {
  693. struct sockaddr_nl *NetlinkAddress;
  694. if ((AddressLength < sizeof(struct sockaddr_nl)) ||
  695. (Address->sa_family != AF_NETLINK)) {
  696. return STATUS_INVALID_ADDRESS;
  697. }
  698. NetlinkAddress = (struct sockaddr_nl *)Address;
  699. NetworkAddress->Domain = NetDomainNetlink;
  700. NetworkAddress->Port = NetlinkAddress->nl_pid;
  701. NetworkAddress->Address[0] = (UINTN)NetlinkAddress->nl_groups;
  702. return STATUS_SUCCESS;
  703. }
  704. KSTATUS
  705. NetlinkpConvertFromNetworkAddress (
  706. PNETWORK_ADDRESS NetworkAddress,
  707. struct sockaddr *Address,
  708. socklen_t *AddressLength
  709. )
  710. /*++
  711. Routine Description:
  712. This routine converts a network address structure into a sockaddr structure.
  713. Arguments:
  714. NetworkAddress - Supplies a pointer to the network address to convert.
  715. Address - Supplies a pointer where the address structure will be returned.
  716. AddressLength - Supplies a pointer that on input contains the length of the
  717. specified Address structure, and on output returns the length of the
  718. returned address. If the supplied buffer is not big enough to hold the
  719. address, the address is truncated, and the larger needed buffer size
  720. will be returned here.
  721. Return Value:
  722. STATUS_SUCCESS on success.
  723. STATUS_BUFFER_TOO_SMALL if the address buffer is not big enough.
  724. STATUS_INVALID_ADDRESS on failure.
  725. --*/
  726. {
  727. UINTN CopySize;
  728. struct sockaddr_nl NetlinkAddress;
  729. KSTATUS Status;
  730. if (NetworkAddress->Domain != NetDomainNetlink) {
  731. return STATUS_INVALID_ADDRESS;
  732. }
  733. Status = STATUS_SUCCESS;
  734. NetlinkAddress.nl_family = AF_NETLINK;
  735. NetlinkAddress.nl_pad = 0;
  736. NetlinkAddress.nl_pid = NetworkAddress->Port;
  737. NetlinkAddress.nl_groups = (ULONG)NetworkAddress->Address[0];
  738. CopySize = sizeof(NetlinkAddress);
  739. if (CopySize > *AddressLength) {
  740. CopySize = *AddressLength;
  741. Status = STATUS_BUFFER_TOO_SMALL;
  742. }
  743. RtlCopyMemory(Address, &NetlinkAddress, CopySize);
  744. *AddressLength = sizeof(NetlinkAddress);
  745. return Status;
  746. }