netlink.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. struct netlink_skb_parms {
  14. struct scm_creds creds; /* Skb credentials */
  15. __u32 portid;
  16. __u32 dst_group;
  17. struct sock *sk;
  18. };
  19. #define NETLINK_CB(skb) (*(struct netlink_skb_parms*)&((skb)->cb))
  20. #define NETLINK_CREDS(skb) (&NETLINK_CB((skb)).creds)
  21. extern void netlink_table_grab(void);
  22. extern void netlink_table_ungrab(void);
  23. #define NL_CFG_F_NONROOT_RECV (1 << 0)
  24. #define NL_CFG_F_NONROOT_SEND (1 << 1)
  25. /* optional Netlink kernel configuration parameters */
  26. struct netlink_kernel_cfg {
  27. unsigned int groups;
  28. unsigned int flags;
  29. void (*input)(struct sk_buff *skb);
  30. struct mutex *cb_mutex;
  31. void (*bind)(int group);
  32. };
  33. extern struct sock *__netlink_kernel_create(struct net *net, int unit,
  34. struct module *module,
  35. struct netlink_kernel_cfg *cfg);
  36. static inline struct sock *
  37. netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
  38. {
  39. return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
  40. }
  41. extern void netlink_kernel_release(struct sock *sk);
  42. extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
  43. extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
  44. extern void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  45. extern void netlink_clear_multicast_users(struct sock *sk, unsigned int group);
  46. extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err);
  47. extern int netlink_has_listeners(struct sock *sk, unsigned int group);
  48. extern int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
  49. extern int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
  50. __u32 group, gfp_t allocation);
  51. extern int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb,
  52. __u32 portid, __u32 group, gfp_t allocation,
  53. int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
  54. void *filter_data);
  55. extern int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
  56. extern int netlink_register_notifier(struct notifier_block *nb);
  57. extern int netlink_unregister_notifier(struct notifier_block *nb);
  58. /* finegrained unicast helpers: */
  59. struct sock *netlink_getsockbyfilp(struct file *filp);
  60. int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
  61. long *timeo, struct sock *ssk);
  62. void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
  63. int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
  64. /*
  65. * skb should fit one page. This choice is good for headerless malloc.
  66. * But we should limit to 8K so that userspace does not have to
  67. * use enormous buffer sizes on recvmsg() calls just to avoid
  68. * MSG_TRUNC when PAGE_SIZE is very large.
  69. */
  70. #if PAGE_SIZE < 8192UL
  71. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(PAGE_SIZE)
  72. #else
  73. #define NLMSG_GOODSIZE SKB_WITH_OVERHEAD(8192UL)
  74. #endif
  75. #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
  76. struct netlink_callback {
  77. struct sk_buff *skb;
  78. const struct nlmsghdr *nlh;
  79. int (*dump)(struct sk_buff * skb,
  80. struct netlink_callback *cb);
  81. int (*done)(struct netlink_callback *cb);
  82. void *data;
  83. /* the module that dump function belong to */
  84. struct module *module;
  85. u16 family;
  86. u16 min_dump_alloc;
  87. unsigned int prev_seq, seq;
  88. long args[6];
  89. };
  90. struct netlink_notify {
  91. struct net *net;
  92. int portid;
  93. int protocol;
  94. };
  95. struct nlmsghdr *
  96. __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
  97. struct netlink_dump_control {
  98. int (*dump)(struct sk_buff *skb, struct netlink_callback *);
  99. int (*done)(struct netlink_callback *);
  100. void *data;
  101. struct module *module;
  102. u16 min_dump_alloc;
  103. };
  104. extern int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  105. const struct nlmsghdr *nlh,
  106. struct netlink_dump_control *control);
  107. static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
  108. const struct nlmsghdr *nlh,
  109. struct netlink_dump_control *control)
  110. {
  111. if (!control->module)
  112. control->module = THIS_MODULE;
  113. return __netlink_dump_start(ssk, skb, nlh, control);
  114. }
  115. #endif /* __LINUX_NETLINK_H */