netlink.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. extern void netlink_kernel_release(struct sock *sk);
  84. extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
  85. extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
  86. extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  87. extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
  88. const struct netlink_ext_ack *extack);
  89. extern int netlink_has_listeners(struct sock *sk, unsigned int group);
  90. extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
  91. extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
  92. __u32 group, gfp_t allocation);
  93. extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
  94. __u32 portid, __u32 group, gfp_t allocation,
  95. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  96. void *filter_data);
  97. extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
  98. extern int netlink_register_notifier(struct notifier_block *nb);
  99. extern int netlink_unregister_notifier(struct notifier_block *nb);
  100. /* finegrained unicast helpers: */
  101. struct sock *netlink_getsockbyfilp(struct file *filp);
  102. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  103. long *timeo, struct sock *ssk);
  104. void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
  105. int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
  106. static inline struct sk_buff *
  107. netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
  108. {
  109. struct sk_buff *nskb;
  110. nskb = skb_clone(skb, gfp_mask);
  111. if (!nskb)
  112. return NULL;
  113. /* This is a large skb, set destructor callback to release head */
  114. if (is_vmalloc_addr(skb->head))
  115. nskb->destructor = skb->destructor;
  116. return nskb;
  117. }
  118. /*
  119. * skb should fit one page. This choice is good for headerless malloc.
  120. * But we should limit to 8K so that userspace does not have to
  121. * use enormous buffer sizes on recvmsg() calls just to avoid
  122. * MSG_TRUNC when PAGE_SIZE is very large.
  123. */
  124. #if PAGE_SIZE < 8192UL
  125. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
  126. #else
  127. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
  128. #endif
  129. #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
  130. struct netlink_callback {
  131. struct sk_buff *skb;
  132. const struct nlmsghdr *nlh;
  133. int (*start)(struct netlink_callback *);
  134. int (*dump)(struct sk_buff * skb,
  135. struct netlink_callback *cb);
  136. int (*done)(struct netlink_callback *cb);
  137. void *data;
  138. /* the module that dump function belong to */
  139. struct module *module;
  140. u16 family;
  141. u16 min_dump_alloc;
  142. unsigned int prev_seq, seq;
  143. long args[6];
  144. };
  145. struct netlink_notify {
  146. struct net *net;
  147. u32 portid;
  148. int protocol;
  149. };
  150. struct nlmsghdr *
  151. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
  152. struct netlink_dump_control {
  153. int (*start)(struct netlink_callback *);
  154. int (*dump)(struct sk_buff *skb, struct netlink_callback *);
  155. int (*done)(struct netlink_callback *);
  156. void *data;
  157. struct module *module;
  158. u16 min_dump_alloc;
  159. };
  160. extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  161. const struct nlmsghdr *nlh,
  162. struct netlink_dump_control *control);
  163. static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  164. const struct nlmsghdr *nlh,
  165. struct netlink_dump_control *control)
  166. {
  167. if (!control->module)
  168. control->module = THIS_MODULE;
  169. return __netlink_dump_start(ssk, skb, nlh, control);
  170. }
  171. struct netlink_tap {
  172. struct net_device *dev;
  173. struct module *module;
  174. struct list_head list;
  175. };
  176. extern int netlink_add_tap(struct netlink_tap *nt);
  177. extern int netlink_remove_tap(struct netlink_tap *nt);
  178. bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
  179. struct user_namespace *ns, int cap);
  180. bool netlink_ns_capable(const struct sk_buff *skb,
  181. struct user_namespace *ns, int cap);
  182. bool netlink_capable(const struct sk_buff *skb, int cap);
  183. bool netlink_net_capable(const struct sk_buff *skb, int cap);
  184. #endif /* __LINUX_NETLINK_H */