netlabel.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/net.h>
  32. #include <linux/skbuff.h>
  33. #include <net/netlink.h>
  34. /*
  35. * NetLabel - A management interface for maintaining network packet label
  36. * mapping tables for explicit packet labling protocols.
  37. *
  38. * Network protocols such as CIPSO and RIPSO require a label translation layer
  39. * to convert the label on the packet into something meaningful on the host
  40. * machine. In the current Linux implementation these mapping tables live
  41. * inside the kernel; NetLabel provides a mechanism for user space applications
  42. * to manage these mapping tables.
  43. *
  44. * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
  45. * send messages between kernel and user space. The general format of a
  46. * NetLabel message is shown below:
  47. *
  48. * +-----------------+-------------------+--------- --- -- -
  49. * | struct nlmsghdr | struct genlmsghdr | payload
  50. * +-----------------+-------------------+--------- --- -- -
  51. *
  52. * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
  53. * The payload is dependent on the subsystem specified in the
  54. * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
  55. * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
  56. * file. All of the fields in the NetLabel payload are NETLINK attributes, see
  57. * the include/net/netlink.h file 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. /*
  77. * NetLabel - Kernel API for accessing the network packet label mappings.
  78. *
  79. * The following functions are provided for use by other kernel modules,
  80. * specifically kernel LSM modules, to provide a consistent, transparent API
  81. * for dealing with explicit packet labeling protocols such as CIPSO and
  82. * RIPSO. The functions defined here are implemented in the
  83. * net/netlabel/netlabel_kapi.c file.
  84. *
  85. */
  86. /* Domain mapping definition struct */
  87. struct netlbl_dom_map;
  88. /* Domain mapping operations */
  89. int netlbl_domhsh_remove(const char *domain);
  90. /* LSM security attributes */
  91. struct netlbl_lsm_cache {
  92. void (*free) (const void *data);
  93. void *data;
  94. };
  95. struct netlbl_lsm_secattr {
  96. char *domain;
  97. u32 mls_lvl;
  98. u32 mls_lvl_vld;
  99. unsigned char *mls_cat;
  100. size_t mls_cat_len;
  101. struct netlbl_lsm_cache cache;
  102. };
  103. /*
  104. * LSM security attribute operations
  105. */
  106. /**
  107. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  108. * @secattr: the struct to initialize
  109. *
  110. * Description:
  111. * Initialize an already allocated netlbl_lsm_secattr struct. Returns zero on
  112. * success, negative values on error.
  113. *
  114. */
  115. static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  116. {
  117. memset(secattr, 0, sizeof(*secattr));
  118. return 0;
  119. }
  120. /**
  121. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  122. * @secattr: the struct to clear
  123. * @clear_cache: cache clear flag
  124. *
  125. * Description:
  126. * Destroys the @secattr struct, including freeing all of the internal buffers.
  127. * If @clear_cache is true then free the cache fields, otherwise leave them
  128. * intact. The struct must be reset with a call to netlbl_secattr_init()
  129. * before reuse.
  130. *
  131. */
  132. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr,
  133. u32 clear_cache)
  134. {
  135. if (clear_cache && secattr->cache.data != NULL && secattr->cache.free)
  136. secattr->cache.free(secattr->cache.data);
  137. kfree(secattr->domain);
  138. kfree(secattr->mls_cat);
  139. }
  140. /**
  141. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  142. * @flags: the memory allocation flags
  143. *
  144. * Description:
  145. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  146. * pointer on success, or NULL on failure.
  147. *
  148. */
  149. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
  150. {
  151. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  152. }
  153. /**
  154. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  155. * @secattr: the struct to free
  156. * @clear_cache: cache clear flag
  157. *
  158. * Description:
  159. * Frees @secattr including all of the internal buffers. If @clear_cache is
  160. * true then free the cache fields, otherwise leave them intact.
  161. *
  162. */
  163. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr,
  164. u32 clear_cache)
  165. {
  166. netlbl_secattr_destroy(secattr, clear_cache);
  167. kfree(secattr);
  168. }
  169. /*
  170. * LSM protocol operations
  171. */
  172. #ifdef CONFIG_NETLABEL
  173. int netlbl_socket_setattr(const struct socket *sock,
  174. const struct netlbl_lsm_secattr *secattr);
  175. int netlbl_sock_getattr(struct sock *sk,
  176. struct netlbl_lsm_secattr *secattr);
  177. int netlbl_socket_getattr(const struct socket *sock,
  178. struct netlbl_lsm_secattr *secattr);
  179. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  180. struct netlbl_lsm_secattr *secattr);
  181. void netlbl_skbuff_err(struct sk_buff *skb, int error);
  182. #else
  183. static inline int netlbl_socket_setattr(const struct socket *sock,
  184. const struct netlbl_lsm_secattr *secattr)
  185. {
  186. return -ENOSYS;
  187. }
  188. static inline int netlbl_sock_getattr(struct sock *sk,
  189. struct netlbl_lsm_secattr *secattr)
  190. {
  191. return -ENOSYS;
  192. }
  193. static inline int netlbl_socket_getattr(const struct socket *sock,
  194. struct netlbl_lsm_secattr *secattr)
  195. {
  196. return -ENOSYS;
  197. }
  198. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  199. struct netlbl_lsm_secattr *secattr)
  200. {
  201. return -ENOSYS;
  202. }
  203. static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
  204. {
  205. return;
  206. }
  207. #endif /* CONFIG_NETLABEL */
  208. /*
  209. * LSM label mapping cache operations
  210. */
  211. #ifdef CONFIG_NETLABEL
  212. void netlbl_cache_invalidate(void);
  213. int netlbl_cache_add(const struct sk_buff *skb,
  214. const struct netlbl_lsm_secattr *secattr);
  215. #else
  216. static inline void netlbl_cache_invalidate(void)
  217. {
  218. return;
  219. }
  220. static inline int netlbl_cache_add(const struct sk_buff *skb,
  221. const struct netlbl_lsm_secattr *secattr)
  222. {
  223. return 0;
  224. }
  225. #endif /* CONFIG_NETLABEL */
  226. #endif /* _NETLABEL_H */