netlink.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. extern void netlink_table_grab(void);
  29. extern 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. extern 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. * @cookie: cookie data to return to userspace (for success)
  58. * @cookie_len: actual cookie data length
  59. */
  60. struct netlink_ext_ack {
  61. const char *_msg;
  62. const struct nlattr *bad_attr;
  63. u8 cookie[NETLINK_MAX_COOKIE_LEN];
  64. u8 cookie_len;
  65. };
  66. /* Always use this macro, this allows later putting the
  67. * message into a separate section or such for things
  68. * like translation or listing all possible messages.
  69. * Currently string formatting is not supported (due
  70. * to the lack of an output buffer.)
  71. */
  72. #define NL_SET_ERR_MSG(extack, msg) do { \
  73. static const char __msg[] = msg; \
  74. struct netlink_ext_ack *__extack = (extack); \
  75. \
  76. if (__extack) \
  77. __extack->_msg = __msg; \
  78. } while (0)
  79. #define NL_SET_ERR_MSG_MOD(extack, msg) \
  80. NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
  81. #define NL_SET_BAD_ATTR(extack, attr) do { \
  82. if ((extack)) \
  83. (extack)->bad_attr = (attr); \
  84. } while (0)
  85. #define NL_SET_ERR_MSG_ATTR(extack, attr, msg) do { \
  86. static const char __msg[] = msg; \
  87. struct netlink_ext_ack *__extack = (extack); \
  88. \
  89. if (__extack) { \
  90. __extack->_msg = __msg; \
  91. __extack->bad_attr = (attr); \
  92. } \
  93. } while (0)
  94. extern void netlink_kernel_release(struct sock *sk);
  95. extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
  96. extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
  97. extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  98. extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
  99. const struct netlink_ext_ack *extack);
  100. extern int netlink_has_listeners(struct sock *sk, unsigned int group);
  101. extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
  102. extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
  103. __u32 group, gfp_t allocation);
  104. extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
  105. __u32 portid, __u32 group, gfp_t allocation,
  106. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  107. void *filter_data);
  108. extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
  109. extern int netlink_register_notifier(struct notifier_block *nb);
  110. extern int netlink_unregister_notifier(struct notifier_block *nb);
  111. /* finegrained unicast helpers: */
  112. struct sock *netlink_getsockbyfilp(struct file *filp);
  113. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  114. long *timeo, struct sock *ssk);
  115. void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
  116. int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
  117. static inline struct sk_buff *
  118. netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  119. {
  120. struct sk_buff *nskb;
  121. nskb = skb_clone(skb, gfp_mask);
  122. if (!nskb)
  123. return NULL;
  124. /* This is a large skb, set destructor callback to release head */
  125. if (is_vmalloc_addr(skb->head))
  126. nskb->destructor = skb->destructor;
  127. return nskb;
  128. }
  129. /*
  130. * skb should fit one page. This choice is good for headerless malloc.
  131. * But we should limit to 8K so that userspace does not have to
  132. * use enormous buffer sizes on recvmsg() calls just to avoid
  133. * MSG_TRUNC when PAGE_SIZE is very large.
  134. */
  135. #if PAGE_SIZE < 8192UL
  136. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
  137. #else
  138. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
  139. #endif
  140. #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
  141. struct netlink_callback {
  142. struct sk_buff *skb;
  143. const struct nlmsghdr *nlh;
  144. int (*start)(struct netlink_callback *);
  145. int (*dump)(struct sk_buff * skb,
  146. struct netlink_callback *cb);
  147. int (*done)(struct netlink_callback *cb);
  148. void *data;
  149. /* the module that dump function belong to */
  150. struct module *module;
  151. u16 family;
  152. u16 min_dump_alloc;
  153. unsigned int prev_seq, seq;
  154. long args[6];
  155. };
  156. struct netlink_notify {
  157. struct net *net;
  158. u32 portid;
  159. int protocol;
  160. };
  161. struct nlmsghdr *
  162. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
  163. struct netlink_dump_control {
  164. int (*start)(struct netlink_callback *);
  165. int (*dump)(struct sk_buff *skb, struct netlink_callback *);
  166. int (*done)(struct netlink_callback *);
  167. void *data;
  168. struct module *module;
  169. u16 min_dump_alloc;
  170. };
  171. extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  172. const struct nlmsghdr *nlh,
  173. struct netlink_dump_control *control);
  174. static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  175. const struct nlmsghdr *nlh,
  176. struct netlink_dump_control *control)
  177. {
  178. if (!control->module)
  179. control->module = THIS_MODULE;
  180. return __netlink_dump_start(ssk, skb, nlh, control);
  181. }
  182. struct netlink_tap {
  183. struct net_device *dev;
  184. struct module *module;
  185. struct list_head list;
  186. };
  187. extern int netlink_add_tap(struct netlink_tap *nt);
  188. extern int netlink_remove_tap(struct netlink_tap *nt);
  189. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  190. struct user_namespace *ns, int cap);
  191. bool netlink_ns_capable(const struct sk_buff *skb,
  192. struct user_namespace *ns, int cap);
  193. bool netlink_capable(const struct sk_buff *skb, int cap);
  194. bool netlink_net_capable(const struct sk_buff *skb, int cap);
  195. #endif /* __LINUX_NETLINK_H */