netlink.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <bits/types.h>
  2. #include <bits/socket/netlink.h>
  3. /* Structures common for all(?) netlink protocols except UDEV.
  4. None of the supported ones actually sends raw nlmsg-s,
  5. all of them have some sort of extra header fields.
  6. Pretty much any request may be replied to with a nlerr packet.
  7. "errno" there is a kernel return code like -EINVAL or -ENOENT.
  8. Value 0 means ACK of a successful request, and only gets sent
  9. if there was NLM_F_ACK in the request packet.
  10. All userspace-to-kernel packets must carry NLM_F_REQUEST flag.
  11. "Set-something" requests without NLM_F_ACK produce no reply
  12. on success and nlerr on error, messing up request-reply
  13. sequences. Such requests must always be sent with ACK enabled.
  14. "Get-something" requests return their respective structures
  15. on success or nlerr on error. Setting NLM_F_ACK results in
  16. *both* ACK and the result struct being sent on success,
  17. messing up request-reply sequences again. Such requests
  18. must always be send without ACK.
  19. Multipart replies have NLM_F_MULTI in each data packet,
  20. and those are followed by a single empty NLMSG_DONE packet. */
  21. /* nlmsg.type */
  22. #define NLMSG_NOOP 1
  23. #define NLMSG_ERROR 2
  24. #define NLMSG_DONE 3
  25. #define NLMSG_OVERRUN 4
  26. /* nlmsg.flags */
  27. #define NLM_F_REQUEST (1<<0)
  28. #define NLM_F_MULTI (1<<1)
  29. #define NLM_F_ACK (1<<2)
  30. #define NLM_F_ECHO (1<<3)
  31. #define NLM_F_DUMP_INTR (1<<4)
  32. #define NLM_F_DUMP_FILTERED (1<<5)
  33. #define NLM_F_ROOT (1<<8)
  34. #define NLM_F_MATCH (1<<9)
  35. #define NLM_F_ATOMIC (1<<10)
  36. #define NLM_F_DUMP (NLM_F_ROOT|NLM_F_MATCH)
  37. #define NLM_F_REPLACE (1<<8)
  38. #define NLM_F_EXCL (1<<9)
  39. #define NLM_F_CREATE (1<<10)
  40. #define NLM_F_APPEND (1<<11)
  41. struct nlmsg {
  42. uint32_t len;
  43. uint16_t type;
  44. uint16_t flags;
  45. uint32_t seq;
  46. uint32_t pid;
  47. char payload[];
  48. } __attribute__((packed));
  49. struct nlerr {
  50. struct nlmsg nlm;
  51. int32_t errno;
  52. /* original request header */
  53. uint32_t len;
  54. uint16_t type;
  55. uint16_t flags;
  56. uint32_t seq;
  57. uint32_t pid;
  58. } __attribute__((packed));
  59. struct nlattr {
  60. uint16_t len;
  61. uint16_t type;
  62. char payload[];
  63. } __attribute__((packed));
  64. /* socket.protocol = NETLINK_GENERIC,
  65. nlmsg.type = family-id
  66. Family is a service we're talking to, like "nl80211" or "batman".
  67. The only one with a fixed id is "ctrl", its id is GENL_ID_CTRL.
  68. IDs for other families must be queried by sending CTRL_CMD_GETFAMILY
  69. request to "ctrl" service.
  70. Message header (nlgen) is common for all requests.
  71. The actual parameters are passed as attributes.
  72. Attribute IDs *are* specific to each family, and so is
  73. expected attribute content. */
  74. struct nlgen {
  75. struct nlmsg nlm;
  76. uint8_t cmd;
  77. uint8_t version;
  78. uint16_t unused;
  79. char payload[];
  80. } __attribute__((packed));
  81. struct nlmsg* nl_msg(void* buf, int len);
  82. struct nlerr* nl_err(struct nlmsg* msg);
  83. struct nlgen* nl_gen(struct nlmsg* msg);
  84. void* nl_cast(struct nlmsg* msg, unsigned size);
  85. static inline int nl_len(struct nlmsg* msg) { return msg->len; }