netlink.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*++
  2. Copyright (c) 2016 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. netlink.h
  9. Abstract:
  10. This header contains definitions for the Minoca Netlink Library.
  11. Author:
  12. Chris Stevens 24-Mar-2016
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #include <sys/socket.h>
  18. //
  19. // ---------------------------------------------------------------- Definitions
  20. //
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #ifndef LIBNETLINK_API
  25. #define LIBNETLINK_API __DLLIMPORT
  26. #endif
  27. //
  28. // Define the netlink address family.
  29. //
  30. #define AF_NETLINK 4
  31. //
  32. // Define the level number for the get/setsockopt that applies to all netlink
  33. // sockets.
  34. //
  35. #define SOL_NETLINK 256
  36. //
  37. // Define the netlink socket options.
  38. //
  39. #define NETLINK_ADD_MEMBERSHIP 1
  40. #define NETLINK_DROP_MEMBERSHIP 2
  41. //
  42. // Define the netlink socket protocols.
  43. //
  44. #define NETLINK_GENERIC 257
  45. //
  46. // Define the port ID value to supply on socket creation if the port does not
  47. // matter.
  48. //
  49. #define NL_ANY_PORT_ID 0
  50. //
  51. // Set this flag in the netlink socket to receive KSTATUS error codes in
  52. // netlink error messages. The default is to receive errno values.
  53. //
  54. #define NL_SOCKET_FLAG_REPORT_KSTATUS 0x00000001
  55. //
  56. // Set this flag in the netlink socket to disable the automatic setting of a
  57. // sequence number on send and automatic validation of sequence numbers upon
  58. // message reception.
  59. //
  60. #define NL_SOCKET_FLAG_NO_AUTO_SEQUENCE 0x00000002
  61. //
  62. // Supply this flag to the message reception routine to prevent it from waiting
  63. // for an ACK before returning.
  64. //
  65. #define NL_RECEIVE_FLAG_NO_ACK_WAIT 0x00000001
  66. //
  67. // Supply these flags to the message reception routine to make sure the
  68. // received messages come from either a given port ID or a multicast group.
  69. //
  70. #define NL_RECEIVE_FLAG_PORT_ID 0x00000002
  71. #define NL_RECEIVE_FLAG_GROUP_MASK 0x00000004
  72. //
  73. // This flag is returned by the receive routine if an ACK was processed.
  74. //
  75. #define NL_RECEIVE_FLAG_ACK_RECEIVED 0x00000008
  76. //
  77. // ------------------------------------------------------ Data Type Definitions
  78. //
  79. /*++
  80. Structure Description:
  81. This structure defines a netlink family socket address.
  82. Members:
  83. nl_family - Stores the address family, which is always AF_NETLINK for
  84. netlink addresses.
  85. nl_pad - Stores two bytes of padding.
  86. nl_pid - Stores the port ID for the address.
  87. nl_groups - Stores the multicast group information for the address.
  88. --*/
  89. struct sockaddr_nl {
  90. sa_family_t nl_family;
  91. unsigned short nl_pad;
  92. pid_t nl_pid;
  93. uint32_t nl_groups;
  94. };
  95. /*++
  96. Structure Description:
  97. This structure defines information about a netlink message buffer.
  98. Members:
  99. Buffer - Stores the address of the netlink buffer.
  100. BufferSize - Stores the size of the buffer, in bytes.
  101. DataSize - Stores the size of the valid data in the buffer, in bytes.
  102. CurrentOffset - Stores the byte offset into the buffer indicating where the
  103. next set of data will be appended.
  104. --*/
  105. typedef struct _NL_MESSAGE_BUFFER {
  106. PVOID Buffer;
  107. ULONG BufferSize;
  108. ULONG DataSize;
  109. ULONG CurrentOffset;
  110. } NL_MESSAGE_BUFFER, *PNL_MESSAGE_BUFFER;
  111. /*++
  112. Structure Description:
  113. This structure defines a socket for the netlink library.
  114. Members:
  115. Socket - Stores the file descriptor for the associated C library socket.
  116. Protocol - Stores the netlink protocol over which the socket communicates.
  117. Flags - Stores a bitmask of netlink socket flags. See NL_SOCKET_FLAG_* for
  118. definitions.
  119. SendNextSequence - Stores the next sequence number to use in a netlink
  120. message header being sent.
  121. ReceiveNextSequence - Stores the next sequence number that is expected to
  122. be received.
  123. LocalAddress - Stores the local address for the socket.
  124. ReceiveBuffer - Stores a pointer to a scratch buffer that the socket can
  125. use to receive messages.
  126. --*/
  127. typedef struct _NL_SOCKET {
  128. INT Socket;
  129. ULONG Protocol;
  130. ULONG Flags;
  131. volatile ULONG SendNextSequence;
  132. volatile ULONG ReceiveNextSequence;
  133. struct sockaddr_nl LocalAddress;
  134. PNL_MESSAGE_BUFFER ReceiveBuffer;
  135. } NL_SOCKET, *PNL_SOCKET;
  136. /*++
  137. Structure Description:
  138. This structure defines the context supplied to each invocation of the
  139. receive callback routine during receive message processing.
  140. Members:
  141. Status - Stores the status from the receive callback routine.
  142. Type - Stores an optional message type that the receive callback routine
  143. can use to validate the message.
  144. PrivateContext - Stores an optional pointer to a private context to be used
  145. by the caller of the message receive handler on each invocation of the
  146. receive callback routine.
  147. --*/
  148. typedef struct _NL_RECEIVE_CONTEXT {
  149. INT Status;
  150. USHORT Type;
  151. PVOID PrivateContext;
  152. } NL_RECEIVE_CONTEXT, *PNL_RECEIVE_CONTEXT;
  153. typedef
  154. VOID
  155. (*PNL_RECEIVE_ROUTINE) (
  156. PNL_SOCKET Socket,
  157. PNL_RECEIVE_CONTEXT Context,
  158. PVOID Message
  159. );
  160. /*++
  161. Routine Description:
  162. This routine processes a protocol-layer netlink message.
  163. Arguments:
  164. Socket - Supplies a pointer to the netlink socket that received the message.
  165. Context - Supplies a pointer to the receive context given to the receive
  166. message handler.
  167. Message - Supplies a pointer to the beginning of the netlink message. The
  168. length of which can be obtained from the header; it was already
  169. validated.
  170. Return Value:
  171. None.
  172. --*/
  173. /*++
  174. Structure Description:
  175. This structure defines the parameters passed to receive a netlink message.
  176. Members:
  177. ReceiveRoutine - Stores an optional pointer to a routine that will be
  178. called for each protocol layer message that is received.
  179. ReceiveContext - Stores a receive context that will be supplied to each
  180. invocation of the receive routine.
  181. Flags - Stores a bitmask of receive flags. See NL_RECEIVE_FLAG_* for
  182. definitions.
  183. PortId - Stores an optional port ID. If valid via the
  184. NL_RECEIVE_FLAG_PORT_ID, the receive message handler will skip any
  185. messages received from non-matching ports.
  186. GroupMask - Stores an optional multicast group mask. If valid via the
  187. NL_RECEIVE_FLAG_GROUP_MASK flag being set, the receive message handler
  188. will skip any messages received from non-matching groups.
  189. --*/
  190. typedef struct _NL_RECEIVE_PARAMETERS {
  191. PNL_RECEIVE_ROUTINE ReceiveRoutine;
  192. NL_RECEIVE_CONTEXT ReceiveContext;
  193. ULONG Flags;
  194. ULONG PortId;
  195. ULONG GroupMask;
  196. } NL_RECEIVE_PARAMETERS, *PNL_RECEIVE_PARAMETERS;
  197. //
  198. // -------------------------------------------------------------------- Globals
  199. //
  200. //
  201. // -------------------------------------------------------- Function Prototypes
  202. //
  203. LIBNETLINK_API
  204. VOID
  205. NlInitialize (
  206. PVOID Environment
  207. );
  208. /*++
  209. Routine Description:
  210. This routine initializes the Minoca Netlink Library. This routine is
  211. normally called by statically linked assembly within a program, and unless
  212. developing outside the usual paradigm should not need to call this routine
  213. directly.
  214. Arguments:
  215. Environment - Supplies a pointer to the environment information.
  216. Return Value:
  217. None.
  218. --*/
  219. LIBNETLINK_API
  220. INT
  221. NlCreateSocket (
  222. ULONG Protocol,
  223. ULONG PortId,
  224. ULONG Flags,
  225. PNL_SOCKET *NewSocket
  226. );
  227. /*++
  228. Routine Description:
  229. This routine creates a netlink socket with the given protocol and port ID.
  230. Arguments:
  231. Protocol - Supplies the netlink protocol to use for the socket.
  232. PortId - Supplies a specific port ID to use for the socket, if available.
  233. Supply NL_ANY_PORT_ID to have the socket dynamically bind to an
  234. available port ID.
  235. Flags - Supplies a bitmask of netlink socket flags. See NL_SOCKET_FLAG_*
  236. for definitions.
  237. NewSocket - Supplies a pointer that receives a pointer to the newly created
  238. socket.
  239. Return Value:
  240. 0 on success.
  241. -1 on error, and the errno variable will be set to contain more information.
  242. --*/
  243. LIBNETLINK_API
  244. VOID
  245. NlDestroySocket (
  246. PNL_SOCKET Socket
  247. );
  248. /*++
  249. Routine Description:
  250. This routine destroys a netlink socket and all its resources.
  251. Arguments:
  252. Socket - Supplies a pointer to the netlink socket to destroy.
  253. Return Value:
  254. None.
  255. --*/
  256. LIBNETLINK_API
  257. INT
  258. NlAllocateBuffer (
  259. ULONG Size,
  260. PNL_MESSAGE_BUFFER *NewBuffer
  261. );
  262. /*++
  263. Routine Description:
  264. This routine allocates a netlink message buffer. It always adds on space
  265. for the base netlink message header.
  266. Arguments:
  267. Size - Supplies the number of data bytes needed.
  268. NewBuffer - Supplies a pointer where a pointer to the new allocation will
  269. be returned on success.
  270. Return Value:
  271. 0 on success.
  272. -1 on error, and the errno variable will be set to contain more information.
  273. --*/
  274. LIBNETLINK_API
  275. VOID
  276. NlFreeBuffer (
  277. PNL_MESSAGE_BUFFER Buffer
  278. );
  279. /*++
  280. Routine Description:
  281. This routine frees a previously allocated netlink message buffer.
  282. Arguments:
  283. Buffer - Supplies a pointer to the buffer to be released.
  284. Return Value:
  285. None.
  286. --*/
  287. LIBNETLINK_API
  288. INT
  289. NlAppendHeader (
  290. PNL_SOCKET Socket,
  291. PNL_MESSAGE_BUFFER Message,
  292. ULONG PayloadLength,
  293. ULONG SequenceNumber,
  294. USHORT Type,
  295. USHORT Flags
  296. );
  297. /*++
  298. Routine Description:
  299. This routine appends a base netlink header to the message. It will make
  300. sure there is enough room left in the supplied message buffer, add the
  301. header before at the current offset and update the offset and valid data
  302. size when complete. It always adds the ACK and REQUEST flags.
  303. Arguments:
  304. Socket - Supplies a pointer to the netlink socket that is sending the
  305. message.
  306. Message - Supplies a pointer to the netlink message buffer to which the
  307. header should be appended.
  308. PayloadLength - Supplies the length of the message data payload, in bytes.
  309. This does not include the base netlink header length.
  310. SequenceNumber - Supplies the sequence number for the message. This value
  311. is ignored unless NL_SOCKET_FLAG_NO_AUTO_SEQUENCE is set in the socket.
  312. Type - Supplies the netlink message type.
  313. Flags - Supplies a bitmask of netlink message flags. See
  314. NETLINK_HEADER_FLAG_* for definitions.
  315. Return Value:
  316. 0 on success.
  317. -1 on error, and the errno variable will be set to contain more information.
  318. --*/
  319. LIBNETLINK_API
  320. INT
  321. NlSendMessage (
  322. PNL_SOCKET Socket,
  323. PNL_MESSAGE_BUFFER Message,
  324. ULONG PortId,
  325. ULONG GroupMask,
  326. PULONG BytesSent
  327. );
  328. /*++
  329. Routine Description:
  330. This routine sends a netlink message for the given socket.
  331. Arguments:
  332. Socket - Supplies a pointer to the netlink socket over which to send the
  333. message.
  334. Message - Supplies a pointer to the message to be sent. The routine will
  335. attempt to send the entire message between the data offset and footer
  336. offset.
  337. PortId - Supplies the port ID of the recipient of the message.
  338. GroupMask - Supplies the group mask of the message recipients.
  339. BytesSent - Supplies an optional pointer the receives the number of bytes
  340. sent on success.
  341. Return Value:
  342. 0 on success.
  343. -1 on error, and the errno variable will be set to contain more information.
  344. --*/
  345. LIBNETLINK_API
  346. INT
  347. NlReceiveMessage (
  348. PNL_SOCKET Socket,
  349. PNL_RECEIVE_PARAMETERS Parameters
  350. );
  351. /*++
  352. Routine Description:
  353. This routine receives one or more netlink messages, does some simple
  354. validation, handles the default netlink message types, and calls the given
  355. receive routine callback for each protocol layer message.
  356. Arguments:
  357. Socket - Supplies a pointer to the netlink socket over which to receive the
  358. message.
  359. Parameters - Supplies a pointer to the receive message parameters.
  360. Return Value:
  361. 0 on success.
  362. -1 on error, and the errno variable will be set to contain more information.
  363. --*/
  364. LIBNETLINK_API
  365. INT
  366. NlAppendAttribute (
  367. PNL_MESSAGE_BUFFER Message,
  368. USHORT Type,
  369. PVOID Data,
  370. USHORT DataLength
  371. );
  372. /*++
  373. Routine Description:
  374. This routine appends a netlink attribute to the given message. It validates
  375. that there is enough space for the attribute and moves the message buffer's
  376. offset to the first byte after the attribute. It also updates the buffer's
  377. valid data size. The exception to this rule is if a NULL data buffer is
  378. supplied; the buffer's data offset and size will only be updated for the
  379. attribute's header.
  380. Arguments:
  381. Message - Supplies a pointer to the netlink message buffer to which the
  382. attribute will be added.
  383. Type - Supplies the netlink attribute type.
  384. Data - Supplies an optional pointer to the attribute data to be stored in
  385. the buffer. Even if no data buffer is supplied, a data length may be
  386. supplied for the case of child attributes that are yet to be appended.
  387. DataLength - Supplies the length of the data, in bytes.
  388. Return Value:
  389. 0 on success.
  390. -1 on error, and the errno variable will be set to contain more information.
  391. --*/
  392. LIBNETLINK_API
  393. INT
  394. NlGetAttribute (
  395. PVOID Attributes,
  396. ULONG AttributesLength,
  397. USHORT Type,
  398. PVOID *Data,
  399. PUSHORT DataLength
  400. );
  401. /*++
  402. Routine Description:
  403. This routine parses the given attributes buffer and returns a pointer to
  404. the desired attribute.
  405. Arguments:
  406. Attributes - Supplies a pointer to the start of the generic command
  407. attributes.
  408. AttributesLength - Supplies the length of the attributes buffer, in bytes.
  409. Type - Supplies the netlink generic attribute type.
  410. Data - Supplies a pointer that receives a pointer to the data for the
  411. requested attribute type.
  412. DataLength - Supplies a pointer that receives the length of the requested
  413. attribute data.
  414. Return Value:
  415. 0 on success.
  416. -1 on error, and the errno variable will be set to contain more information.
  417. --*/
  418. LIBNETLINK_API
  419. INT
  420. NlGenericAppendHeaders (
  421. PNL_SOCKET Socket,
  422. PNL_MESSAGE_BUFFER Message,
  423. ULONG PayloadLength,
  424. ULONG SequenceNumber,
  425. USHORT Type,
  426. USHORT Flags,
  427. UCHAR Command,
  428. UCHAR Version
  429. );
  430. /*++
  431. Routine Description:
  432. This routine appends the base and generic netlink headers to the given
  433. message, validating that there is enough space remaining in the buffer.
  434. Once the headers are appended, it moves the buffer's offset to the first
  435. byte after the headers and updates the valid data size.
  436. Arguments:
  437. Socket - Supplies a pointer to the netlink socket over which the message
  438. will be sent.
  439. Message - Supplies a pointer to the netlink message buffer to which the
  440. headers will be appended.
  441. PayloadLength - Supplies the length of the message data payload, in bytes.
  442. This does not include the header lengths.
  443. SequenceNumber - Supplies the sequence number for the message. This value
  444. is ignored unless NL_SOCKET_FLAG_NO_AUTO_SEQUENCE is set in the socket.
  445. Type - Supplies the netlink message type.
  446. Flags - Supplies a bitmask of netlink message flags. See
  447. NETLINK_HEADER_FLAG_* for definitions.
  448. Command - Supplies the generic message command.
  449. Version - Supplies the version of the generic message command.
  450. Return Value:
  451. 0 on success.
  452. -1 on error, and the errno variable will be set to contain more information.
  453. --*/
  454. LIBNETLINK_API
  455. INT
  456. NlGenericGetFamilyId (
  457. PNL_SOCKET Socket,
  458. PSTR FamilyName,
  459. PUSHORT FamilyId
  460. );
  461. /*++
  462. Routine Description:
  463. This routine queries the system for a message family ID, which is dynamic,
  464. using a well-known messsage family name.
  465. Arguments:
  466. Socket - Supplies a pointer to the netlink socket to use to send the
  467. generic message.
  468. FamilyName - Supplies the family name string to use for looking up the type.
  469. FamilyId - Supplies a pointer that receives the message family ID to use as
  470. the netlink message header type.
  471. Return Value:
  472. 0 on success.
  473. -1 on error, and the errno variable will be set to contain more information.
  474. --*/
  475. LIBNETLINK_API
  476. INT
  477. NlGenericJoinMulticastGroup (
  478. PNL_SOCKET Socket,
  479. USHORT FamilyId,
  480. PSTR GroupName
  481. );
  482. /*++
  483. Routine Description:
  484. This routine joins the given socket to the multicast group specified by the
  485. family ID and group name.
  486. Arguments:
  487. Socket - Supplies a pointer to the netlink socket requesting to join a
  488. multicast group.
  489. FamilyId - Supplies the ID of the family to which the multicast group
  490. belongs.
  491. GroupName - Supplies the name of the multicast group to join.
  492. Return Value:
  493. 0 on success.
  494. -1 on error, and the errno variable will be set to contain more information.
  495. --*/
  496. #ifdef __cplusplus
  497. }
  498. #endif