netlink.h 16 KB

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