netlink.h 5.9 KB

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