netlink.h 4.6 KB

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