netlink.h 5.5 KB

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