netlink.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifndef __LINUX_NETLINK_H
  2. #define __LINUX_NETLINK_H
  3. #include <linux/capability.h>
  4. #include <linux/skbuff.h>
  5. #include <linux/export.h>
  6. #include <net/scm.h>
  7. #include <uapi/linux/netlink.h>
  8. struct net;
  9. static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
  10. {
  11. return (struct nlmsghdr *)skb->data;
  12. }
  13. enum netlink_skb_flags {
  14. NETLINK_SKB_MMAPED = 0x1, /* Packet data is mmaped */
  15. NETLINK_SKB_TX = 0x2, /* Packet was sent by userspace */
  16. NETLINK_SKB_DELIVERED = 0x4, /* Packet was delivered */
  17. NETLINK_SKB_DST = 0x8, /* Dst set in sendto or sendmsg */
  18. };
  19. struct netlink_skb_parms {
  20. struct scm_creds creds; /* Skb credentials */
  21. __u32 portid;
  22. __u32 dst_group;
  23. __u32 flags;
  24. struct sock *sk;
  25. bool nsid_is_set;
  26. int nsid;
  27. };
  28. #define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
  29. #define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
  30. extern void netlink_table_grab(void);
  31. extern void netlink_table_ungrab(void);
  32. #define NL_CFG_F_NONROOT_RECV (1 << 0)
  33. #define NL_CFG_F_NONROOT_SEND (1 << 1)
  34. /* optional Netlink kernel configuration parameters */
  35. struct netlink_kernel_cfg {
  36. unsigned int groups;
  37. unsigned int flags;
  38. void (*input)(struct sk_buff *skb);
  39. struct mutex *cb_mutex;
  40. int (*bind)(struct net *net, int group);
  41. void (*unbind)(struct net *net, int group);
  42. bool (*compare)(struct net *net, struct sock *sk);
  43. };
  44. extern struct sock *__netlink_kernel_create(struct net *net, int unit,
  45. struct module *module,
  46. struct netlink_kernel_cfg *cfg);
  47. static inline struct sock *
  48. netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
  49. {
  50. return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
  51. }
  52. /* this can be increased when necessary - don't expose to userland */
  53. #define NETLINK_MAX_COOKIE_LEN 20
  54. /**
  55. * struct netlink_ext_ack - netlink extended ACK report struct
  56. * @_msg: message string to report - don't access directly, use
  57. * %NL_SET_ERR_MSG
  58. * @bad_attr: attribute with error
  59. * @cookie: cookie data to return to userspace (for success)
  60. * @cookie_len: actual cookie data length
  61. */
  62. struct netlink_ext_ack {
  63. const char *_msg;
  64. const struct nlattr *bad_attr;
  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(extack, attr) do { \
  84. if ((extack)) \
  85. (extack)->bad_attr = (attr); \
  86. } while (0)
  87. extern void netlink_kernel_release(struct sock *sk);
  88. extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
  89. extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
  90. extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  91. extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
  92. const struct netlink_ext_ack *extack);
  93. extern int netlink_has_listeners(struct sock *sk, unsigned int group);
  94. extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
  95. extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
  96. __u32 group, gfp_t allocation);
  97. extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
  98. __u32 portid, __u32 group, gfp_t allocation,
  99. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  100. void *filter_data);
  101. extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
  102. extern int netlink_register_notifier(struct notifier_block *nb);
  103. extern int netlink_unregister_notifier(struct notifier_block *nb);
  104. /* finegrained unicast helpers: */
  105. struct sock *netlink_getsockbyfilp(struct file *filp);
  106. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  107. long *timeo, struct sock *ssk);
  108. void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
  109. int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
  110. static inline struct sk_buff *
  111. netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  112. {
  113. struct sk_buff *nskb;
  114. nskb = skb_clone(skb, gfp_mask);
  115. if (!nskb)
  116. return NULL;
  117. /* This is a large skb, set destructor callback to release head */
  118. if (is_vmalloc_addr(skb->head))
  119. nskb->destructor = skb->destructor;
  120. return nskb;
  121. }
  122. /*
  123. * skb should fit one page. This choice is good for headerless malloc.
  124. * But we should limit to 8K so that userspace does not have to
  125. * use enormous buffer sizes on recvmsg() calls just to avoid
  126. * MSG_TRUNC when PAGE_SIZE is very large.
  127. */
  128. #if PAGE_SIZE < 8192UL
  129. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
  130. #else
  131. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
  132. #endif
  133. #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
  134. struct netlink_callback {
  135. struct sk_buff *skb;
  136. const struct nlmsghdr *nlh;
  137. int (*start)(struct netlink_callback *);
  138. int (*dump)(struct sk_buff * skb,
  139. struct netlink_callback *cb);
  140. int (*done)(struct netlink_callback *cb);
  141. void *data;
  142. /* the module that dump function belong to */
  143. struct module *module;
  144. u16 family;
  145. u16 min_dump_alloc;
  146. unsigned int prev_seq, seq;
  147. long args[6];
  148. };
  149. struct netlink_notify {
  150. struct net *net;
  151. u32 portid;
  152. int protocol;
  153. };
  154. struct nlmsghdr *
  155. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
  156. struct netlink_dump_control {
  157. int (*start)(struct netlink_callback *);
  158. int (*dump)(struct sk_buff *skb, struct netlink_callback *);
  159. int (*done)(struct netlink_callback *);
  160. void *data;
  161. struct module *module;
  162. u16 min_dump_alloc;
  163. };
  164. extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  165. const struct nlmsghdr *nlh,
  166. struct netlink_dump_control *control);
  167. static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  168. const struct nlmsghdr *nlh,
  169. struct netlink_dump_control *control)
  170. {
  171. if (!control->module)
  172. control->module = THIS_MODULE;
  173. return __netlink_dump_start(ssk, skb, nlh, control);
  174. }
  175. struct netlink_tap {
  176. struct net_device *dev;
  177. struct module *module;
  178. struct list_head list;
  179. };
  180. extern int netlink_add_tap(struct netlink_tap *nt);
  181. extern int netlink_remove_tap(struct netlink_tap *nt);
  182. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  183. struct user_namespace *ns, int cap);
  184. bool netlink_ns_capable(const struct sk_buff *skb,
  185. struct user_namespace *ns, int cap);
  186. bool netlink_capable(const struct sk_buff *skb, int cap);
  187. bool netlink_net_capable(const struct sk_buff *skb, int cap);
  188. #endif /* __LINUX_NETLINK_H */