netlabel.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * NetLabel System
  3. *
  4. * The NetLabel system manages static and dynamic label mappings for network
  5. * protocols such as CIPSO and RIPSO.
  6. *
  7. * Author: Paul Moore <paul.moore@hp.com>
  8. *
  9. */
  10. /*
  11. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  21. * the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. */
  28. #ifndef _NETLABEL_H
  29. #define _NETLABEL_H
  30. #include <linux/types.h>
  31. #include <linux/skbuff.h>
  32. #include <net/netlink.h>
  33. /*
  34. * NetLabel - A management interface for maintaining network packet label
  35. * mapping tables for explicit packet labling protocols.
  36. *
  37. * Network protocols such as CIPSO and RIPSO require a label translation layer
  38. * to convert the label on the packet into something meaningful on the host
  39. * machine. In the current Linux implementation these mapping tables live
  40. * inside the kernel; NetLabel provides a mechanism for user space applications
  41. * to manage these mapping tables.
  42. *
  43. * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
  44. * send messages between kernel and user space. The general format of a
  45. * NetLabel message is shown below:
  46. *
  47. * +-----------------+-------------------+--------- --- -- -
  48. * | struct nlmsghdr | struct genlmsghdr | payload
  49. * +-----------------+-------------------+--------- --- -- -
  50. *
  51. * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
  52. * The payload is dependent on the subsystem specified in the
  53. * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
  54. * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
  55. * file. All of the fields in the NetLabel payload are NETLINK attributes, the
  56. * length of each field is the length of the NETLINK attribute payload, see
  57. * include/net/netlink.h for more information on NETLINK attributes.
  58. *
  59. */
  60. /*
  61. * NetLabel NETLINK protocol
  62. */
  63. #define NETLBL_PROTO_VERSION 1
  64. /* NetLabel NETLINK types/families */
  65. #define NETLBL_NLTYPE_NONE 0
  66. #define NETLBL_NLTYPE_MGMT 1
  67. #define NETLBL_NLTYPE_MGMT_NAME "NLBL_MGMT"
  68. #define NETLBL_NLTYPE_RIPSO 2
  69. #define NETLBL_NLTYPE_RIPSO_NAME "NLBL_RIPSO"
  70. #define NETLBL_NLTYPE_CIPSOV4 3
  71. #define NETLBL_NLTYPE_CIPSOV4_NAME "NLBL_CIPSOv4"
  72. #define NETLBL_NLTYPE_CIPSOV6 4
  73. #define NETLBL_NLTYPE_CIPSOV6_NAME "NLBL_CIPSOv6"
  74. #define NETLBL_NLTYPE_UNLABELED 5
  75. #define NETLBL_NLTYPE_UNLABELED_NAME "NLBL_UNLBL"
  76. /* NetLabel return codes */
  77. #define NETLBL_E_OK 0
  78. /*
  79. * Helper functions
  80. */
  81. #define NETLBL_LEN_U8 nla_total_size(sizeof(u8))
  82. #define NETLBL_LEN_U16 nla_total_size(sizeof(u16))
  83. #define NETLBL_LEN_U32 nla_total_size(sizeof(u32))
  84. /**
  85. * netlbl_netlink_alloc_skb - Allocate a NETLINK message buffer
  86. * @head: the amount of headroom in bytes
  87. * @body: the desired size (minus headroom) in bytes
  88. * @gfp_flags: the alloc flags to pass to alloc_skb()
  89. *
  90. * Description:
  91. * Allocate a NETLINK message buffer based on the sizes given in @head and
  92. * @body. If @head is greater than zero skb_reserve() is called to reserve
  93. * @head bytes at the start of the buffer. Returns a valid sk_buff pointer on
  94. * success, NULL on failure.
  95. *
  96. */
  97. static inline struct sk_buff *netlbl_netlink_alloc_skb(size_t head,
  98. size_t body,
  99. int gfp_flags)
  100. {
  101. struct sk_buff *skb;
  102. skb = alloc_skb(NLMSG_ALIGN(head + body), gfp_flags);
  103. if (skb == NULL)
  104. return NULL;
  105. if (head > 0) {
  106. skb_reserve(skb, head);
  107. if (skb_tailroom(skb) < body) {
  108. kfree_skb(skb);
  109. return NULL;
  110. }
  111. }
  112. return skb;
  113. }
  114. /*
  115. * NetLabel - Kernel API for accessing the network packet label mappings.
  116. *
  117. * The following functions are provided for use by other kernel modules,
  118. * specifically kernel LSM modules, to provide a consistent, transparent API
  119. * for dealing with explicit packet labeling protocols such as CIPSO and
  120. * RIPSO. The functions defined here are implemented in the
  121. * net/netlabel/netlabel_kapi.c file.
  122. *
  123. */
  124. /* Domain mapping definition struct */
  125. struct netlbl_dom_map;
  126. /* Domain mapping operations */
  127. int netlbl_domhsh_remove(const char *domain);
  128. /* LSM security attributes */
  129. struct netlbl_lsm_cache {
  130. void (*free) (const void *data);
  131. void *data;
  132. };
  133. struct netlbl_lsm_secattr {
  134. char *domain;
  135. u32 mls_lvl;
  136. u32 mls_lvl_vld;
  137. unsigned char *mls_cat;
  138. size_t mls_cat_len;
  139. struct netlbl_lsm_cache cache;
  140. };
  141. /*
  142. * LSM security attribute operations
  143. */
  144. /**
  145. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  146. * @secattr: the struct to initialize
  147. *
  148. * Description:
  149. * Initialize an already allocated netlbl_lsm_secattr struct. Returns zero on
  150. * success, negative values on error.
  151. *
  152. */
  153. static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  154. {
  155. memset(secattr, 0, sizeof(*secattr));
  156. return 0;
  157. }
  158. /**
  159. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  160. * @secattr: the struct to clear
  161. * @clear_cache: cache clear flag
  162. *
  163. * Description:
  164. * Destroys the @secattr struct, including freeing all of the internal buffers.
  165. * If @clear_cache is true then free the cache fields, otherwise leave them
  166. * intact. The struct must be reset with a call to netlbl_secattr_init()
  167. * before reuse.
  168. *
  169. */
  170. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr,
  171. u32 clear_cache)
  172. {
  173. if (clear_cache && secattr->cache.data != NULL && secattr->cache.free)
  174. secattr->cache.free(secattr->cache.data);
  175. kfree(secattr->domain);
  176. kfree(secattr->mls_cat);
  177. }
  178. /**
  179. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  180. * @flags: the memory allocation flags
  181. *
  182. * Description:
  183. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  184. * pointer on success, or NULL on failure.
  185. *
  186. */
  187. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
  188. {
  189. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  190. }
  191. /**
  192. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  193. * @secattr: the struct to free
  194. * @clear_cache: cache clear flag
  195. *
  196. * Description:
  197. * Frees @secattr including all of the internal buffers. If @clear_cache is
  198. * true then free the cache fields, otherwise leave them intact.
  199. *
  200. */
  201. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr,
  202. u32 clear_cache)
  203. {
  204. netlbl_secattr_destroy(secattr, clear_cache);
  205. kfree(secattr);
  206. }
  207. /*
  208. * LSM protocol operations
  209. */
  210. #ifdef CONFIG_NETLABEL
  211. int netlbl_socket_setattr(const struct socket *sock,
  212. const struct netlbl_lsm_secattr *secattr);
  213. int netlbl_socket_getattr(const struct socket *sock,
  214. struct netlbl_lsm_secattr *secattr);
  215. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  216. struct netlbl_lsm_secattr *secattr);
  217. void netlbl_skbuff_err(struct sk_buff *skb, int error);
  218. #else
  219. static inline int netlbl_socket_setattr(const struct socket *sock,
  220. const struct netlbl_lsm_secattr *secattr)
  221. {
  222. return -ENOSYS;
  223. }
  224. static inline int netlbl_socket_getattr(const struct socket *sock,
  225. struct netlbl_lsm_secattr *secattr)
  226. {
  227. return -ENOSYS;
  228. }
  229. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  230. struct netlbl_lsm_secattr *secattr)
  231. {
  232. return -ENOSYS;
  233. }
  234. static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
  235. {
  236. return;
  237. }
  238. #endif /* CONFIG_NETLABEL */
  239. /*
  240. * LSM label mapping cache operations
  241. */
  242. #ifdef CONFIG_NETLABEL
  243. void netlbl_cache_invalidate(void);
  244. int netlbl_cache_add(const struct sk_buff *skb,
  245. const struct netlbl_lsm_secattr *secattr);
  246. #else
  247. static inline void netlbl_cache_invalidate(void)
  248. {
  249. return;
  250. }
  251. static inline int netlbl_cache_add(const struct sk_buff *skb,
  252. const struct netlbl_lsm_secattr *secattr)
  253. {
  254. return 0;
  255. }
  256. #endif /* CONFIG_NETLABEL */
  257. #endif /* _NETLABEL_H */