netlabel_user.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * NetLabel NETLINK Interface
  3. *
  4. * This file defines the NETLINK interface for the NetLabel system. The
  5. * NetLabel system manages static and dynamic label mappings for network
  6. * protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #ifndef _NETLABEL_USER_H
  30. #define _NETLABEL_USER_H
  31. #include <linux/skbuff.h>
  32. #include <linux/capability.h>
  33. #include <linux/genetlink.h>
  34. #include <net/netlabel.h>
  35. #include <net/genetlink.h>
  36. /* NetLabel NETLINK helper functions */
  37. /**
  38. * netlbl_netlink_cap_check - Check the NETLINK msg capabilities
  39. * @skb: the NETLINK buffer
  40. * @req_cap: the required capability
  41. *
  42. * Description:
  43. * Check the NETLINK buffer's capabilities against the required capabilities.
  44. * Returns zero on success, negative values on failure.
  45. *
  46. */
  47. static inline int netlbl_netlink_cap_check(const struct sk_buff *skb,
  48. kernel_cap_t req_cap)
  49. {
  50. if (cap_raised(NETLINK_CB(skb).eff_cap, req_cap))
  51. return 0;
  52. return -EPERM;
  53. }
  54. /**
  55. * netlbl_getinc_u8 - Read a u8 value from a nlattr stream and move on
  56. * @nla: the attribute
  57. * @rem_len: remaining length
  58. *
  59. * Description:
  60. * Return a u8 value pointed to by @nla and advance it to the next attribute.
  61. *
  62. */
  63. static inline u8 netlbl_getinc_u8(struct nlattr **nla, int *rem_len)
  64. {
  65. u8 val = nla_get_u8(*nla);
  66. *nla = nla_next(*nla, rem_len);
  67. return val;
  68. }
  69. /**
  70. * netlbl_getinc_u16 - Read a u16 value from a nlattr stream and move on
  71. * @nla: the attribute
  72. * @rem_len: remaining length
  73. *
  74. * Description:
  75. * Return a u16 value pointed to by @nla and advance it to the next attribute.
  76. *
  77. */
  78. static inline u16 netlbl_getinc_u16(struct nlattr **nla, int *rem_len)
  79. {
  80. u16 val = nla_get_u16(*nla);
  81. *nla = nla_next(*nla, rem_len);
  82. return val;
  83. }
  84. /**
  85. * netlbl_getinc_u32 - Read a u32 value from a nlattr stream and move on
  86. * @nla: the attribute
  87. * @rem_len: remaining length
  88. *
  89. * Description:
  90. * Return a u32 value pointed to by @nla and advance it to the next attribute.
  91. *
  92. */
  93. static inline u32 netlbl_getinc_u32(struct nlattr **nla, int *rem_len)
  94. {
  95. u32 val = nla_get_u32(*nla);
  96. *nla = nla_next(*nla, rem_len);
  97. return val;
  98. }
  99. /**
  100. * netlbl_netlink_hdr_put - Write the NETLINK buffers into a sk_buff
  101. * @skb: the packet
  102. * @pid: the PID of the receipient
  103. * @seq: the sequence number
  104. * @type: the generic NETLINK message family type
  105. * @cmd: command
  106. *
  107. * Description:
  108. * Write both a NETLINK nlmsghdr structure and a Generic NETLINK genlmsghdr
  109. * struct to the packet. Returns a pointer to the start of the payload buffer
  110. * on success or NULL on failure.
  111. *
  112. */
  113. static inline void *netlbl_netlink_hdr_put(struct sk_buff *skb,
  114. u32 pid,
  115. u32 seq,
  116. int type,
  117. u8 cmd)
  118. {
  119. return genlmsg_put(skb,
  120. pid,
  121. seq,
  122. type,
  123. 0,
  124. 0,
  125. cmd,
  126. NETLBL_PROTO_VERSION);
  127. }
  128. /**
  129. * netlbl_netlink_hdr_push - Write the NETLINK buffers into a sk_buff
  130. * @skb: the packet
  131. * @pid: the PID of the receipient
  132. * @seq: the sequence number
  133. * @type: the generic NETLINK message family type
  134. * @cmd: command
  135. *
  136. * Description:
  137. * Write both a NETLINK nlmsghdr structure and a Generic NETLINK genlmsghdr
  138. * struct to the packet.
  139. *
  140. */
  141. static inline void netlbl_netlink_hdr_push(struct sk_buff *skb,
  142. u32 pid,
  143. u32 seq,
  144. int type,
  145. u8 cmd)
  146. {
  147. struct nlmsghdr *nlh;
  148. struct genlmsghdr *hdr;
  149. nlh = (struct nlmsghdr *)skb_push(skb, NLMSG_SPACE(GENL_HDRLEN));
  150. nlh->nlmsg_type = type;
  151. nlh->nlmsg_len = skb->len;
  152. nlh->nlmsg_flags = 0;
  153. nlh->nlmsg_pid = pid;
  154. nlh->nlmsg_seq = seq;
  155. hdr = nlmsg_data(nlh);
  156. hdr->cmd = cmd;
  157. hdr->version = NETLBL_PROTO_VERSION;
  158. hdr->reserved = 0;
  159. }
  160. /**
  161. * netlbl_netlink_payload_len - Return the length of the payload
  162. * @skb: the NETLINK buffer
  163. *
  164. * Description:
  165. * This function returns the length of the NetLabel payload.
  166. *
  167. */
  168. static inline u32 netlbl_netlink_payload_len(const struct sk_buff *skb)
  169. {
  170. return nlmsg_len((struct nlmsghdr *)skb->data) - GENL_HDRLEN;
  171. }
  172. /**
  173. * netlbl_netlink_payload_data - Returns a pointer to the start of the payload
  174. * @skb: the NETLINK buffer
  175. *
  176. * Description:
  177. * This function returns a pointer to the start of the NetLabel payload.
  178. *
  179. */
  180. static inline void *netlbl_netlink_payload_data(const struct sk_buff *skb)
  181. {
  182. return (unsigned char *)nlmsg_data((struct nlmsghdr *)skb->data) +
  183. GENL_HDRLEN;
  184. }
  185. /* NetLabel common protocol functions */
  186. void netlbl_netlink_send_ack(const struct genl_info *info,
  187. u32 genl_family,
  188. u8 ack_cmd,
  189. u32 ret_code);
  190. /* NetLabel NETLINK I/O functions */
  191. int netlbl_netlink_init(void);
  192. int netlbl_netlink_snd(struct sk_buff *skb, u32 pid);
  193. int netlbl_netlink_snd_multicast(struct sk_buff *skb, u32 pid, u32 group);
  194. #endif