netlink.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_NETLINK_H
  3. #define __LINUX_NETLINK_H
  4. #include <linux/capability.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/export.h>
  7. #include <net/scm.h>
  8. #include <uapi/linux/netlink.h>
  9. struct net;
  10. static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
  11. {
  12. return (struct nlmsghdr *)skb->data;
  13. }
  14. enum netlink_skb_flags {
  15. NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
  16. };
  17. struct netlink_skb_parms {
  18. struct scm_creds creds; /* Skb credentials */
  19. __u32 portid;
  20. __u32 dst_group;
  21. __u32 flags;
  22. struct sock *sk;
  23. bool nsid_is_set;
  24. int nsid;
  25. };
  26. #define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
  27. #define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
  28. void netlink_table_grab(void);
  29. void netlink_table_ungrab(void);
  30. #define NL_CFG_F_NONROOT_RECV (1 << 0)
  31. #define NL_CFG_F_NONROOT_SEND (1 << 1)
  32. /* optional Netlink kernel configuration parameters */
  33. struct netlink_kernel_cfg {
  34. unsigned int groups;
  35. unsigned int flags;
  36. void (*input)(struct sk_buff *skb);
  37. struct mutex *cb_mutex;
  38. int (*bind)(struct net *net, int group);
  39. void (*unbind)(struct net *net, int group);
  40. bool (*compare)(struct net *net, struct sock *sk);
  41. };
  42. struct sock *__netlink_kernel_create(struct net *net, int unit,
  43. struct module *module,
  44. struct netlink_kernel_cfg *cfg);
  45. static inline struct sock *
  46. netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
  47. {
  48. return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
  49. }
  50. /* this can be increased when necessary - don't expose to userland */
  51. #define NETLINK_MAX_COOKIE_LEN 20
  52. /**
  53. * struct netlink_ext_ack - netlink extended ACK report struct
  54. * @_msg: message string to report - don't access directly, use
  55. * %NL_SET_ERR_MSG
  56. * @bad_attr: attribute with error
  57. * @policy: policy for a bad attribute
  58. * @cookie: cookie data to return to userspace (for success)
  59. * @cookie_len: actual cookie data length
  60. */
  61. struct netlink_ext_ack {
  62. const char *_msg;
  63. const struct nlattr *bad_attr;
  64. const struct nla_policy *policy;
  65. u8 cookie[NETLINK_MAX_COOKIE_LEN];
  66. u8 cookie_len;
  67. };
  68. /* Always use this macro, this allows later putting the
  69. * message into a separate section or such for things
  70. * like translation or listing all possible messages.
  71. * Currently string formatting is not supported (due
  72. * to the lack of an output buffer.)
  73. */
  74. #define NL_SET_ERR_MSG(extack, msg) do { \
  75. static const char __msg[] = msg; \
  76. struct netlink_ext_ack *__extack = (extack); \
  77. \
  78. if (__extack) \
  79. __extack->_msg = __msg; \
  80. } while (0)
  81. #define NL_SET_ERR_MSG_MOD(extack, msg) \
  82. NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
  83. #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
  84. if ((extack)) { \
  85. (extack)->bad_attr = (attr); \
  86. (extack)->policy = (pol); \
  87. } \
  88. } while (0)
  89. #define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
  90. #define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do { \
  91. static const char __msg[] = msg; \
  92. struct netlink_ext_ack *__extack = (extack); \
  93. \
  94. if (__extack) { \
  95. __extack->_msg = __msg; \
  96. __extack->bad_attr = (attr); \
  97. __extack->policy = (pol); \
  98. } \
  99. } while (0)
  100. #define NL_SET_ERR_MSG_ATTR(extack, attr, msg) \
  101. NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
  102. static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
  103. u64 cookie)
  104. {
  105. u64 __cookie = cookie;
  106. if (!extack)
  107. return;
  108. memcpy(extack->cookie, &__cookie, sizeof(__cookie));
  109. extack->cookie_len = sizeof(__cookie);
  110. }
  111. static inline void nl_set_extack_cookie_u32(struct netlink_ext_ack *extack,
  112. u32 cookie)
  113. {
  114. u32 __cookie = cookie;
  115. if (!extack)
  116. return;
  117. memcpy(extack->cookie, &__cookie, sizeof(__cookie));
  118. extack->cookie_len = sizeof(__cookie);
  119. }
  120. void netlink_kernel_release(struct sock *sk);
  121. int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
  122. int netlink_change_ngroups(struct sock *sk, unsigned int groups);
  123. void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  124. void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
  125. const struct netlink_ext_ack *extack);
  126. int netlink_has_listeners(struct sock *sk, unsigned int group);
  127. bool netlink_strict_get_check(struct sk_buff *skb);
  128. int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
  129. int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
  130. __u32 group, gfp_t allocation);
  131. int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
  132. __u32 portid, __u32 group, gfp_t allocation,
  133. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  134. void *filter_data);
  135. int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
  136. int netlink_register_notifier(struct notifier_block *nb);
  137. int netlink_unregister_notifier(struct notifier_block *nb);
  138. /* finegrained unicast helpers: */
  139. struct sock *netlink_getsockbyfilp(struct file *filp);
  140. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  141. long *timeo, struct sock *ssk);
  142. void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
  143. int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
  144. static inline struct sk_buff *
  145. netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  146. {
  147. struct sk_buff *nskb;
  148. nskb = skb_clone(skb, gfp_mask);
  149. if (!nskb)
  150. return NULL;
  151. /* This is a large skb, set destructor callback to release head */
  152. if (is_vmalloc_addr(skb->head))
  153. nskb->destructor = skb->destructor;
  154. return nskb;
  155. }
  156. /*
  157. * skb should fit one page. This choice is good for headerless malloc.
  158. * But we should limit to 8K so that userspace does not have to
  159. * use enormous buffer sizes on recvmsg() calls just to avoid
  160. * MSG_TRUNC when PAGE_SIZE is very large.
  161. */
  162. #if PAGE_SIZE < 8192UL
  163. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
  164. #else
  165. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
  166. #endif
  167. #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
  168. struct netlink_callback {
  169. struct sk_buff *skb;
  170. const struct nlmsghdr *nlh;
  171. int (*dump)(struct sk_buff * skb,
  172. struct netlink_callback *cb);
  173. int (*done)(struct netlink_callback *cb);
  174. void *data;
  175. /* the module that dump function belong to */
  176. struct module *module;
  177. struct netlink_ext_ack *extack;
  178. u16 family;
  179. u16 answer_flags;
  180. u32 min_dump_alloc;
  181. unsigned int prev_seq, seq;
  182. bool strict_check;
  183. union {
  184. u8 ctx[48];
  185. /* args is deprecated. Cast a struct over ctx instead
  186. * for proper type safety.
  187. */
  188. long args[6];
  189. };
  190. };
  191. struct netlink_notify {
  192. struct net *net;
  193. u32 portid;
  194. int protocol;
  195. };
  196. struct nlmsghdr *
  197. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
  198. struct netlink_dump_control {
  199. int (*start)(struct netlink_callback *);
  200. int (*dump)(struct sk_buff *skb, struct netlink_callback *);
  201. int (*done)(struct netlink_callback *);
  202. void *data;
  203. struct module *module;
  204. u32 min_dump_alloc;
  205. };
  206. int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  207. const struct nlmsghdr *nlh,
  208. struct netlink_dump_control *control);
  209. static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  210. const struct nlmsghdr *nlh,
  211. struct netlink_dump_control *control)
  212. {
  213. if (!control->module)
  214. control->module = THIS_MODULE;
  215. return __netlink_dump_start(ssk, skb, nlh, control);
  216. }
  217. struct netlink_tap {
  218. struct net_device *dev;
  219. struct module *module;
  220. struct list_head list;
  221. };
  222. int netlink_add_tap(struct netlink_tap *nt);
  223. int netlink_remove_tap(struct netlink_tap *nt);
  224. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  225. struct user_namespace *ns, int cap);
  226. bool netlink_ns_capable(const struct sk_buff *skb,
  227. struct user_namespace *ns, int cap);
  228. bool netlink_capable(const struct sk_buff *skb, int cap);
  229. bool netlink_net_capable(const struct sk_buff *skb, int cap);
  230. #endif /* __LINUX_NETLINK_H */