netlink.h 8.1 KB

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