netlabel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include <asm/atomic.h>
  35. /*
  36. * NetLabel - A management interface for maintaining network packet label
  37. * mapping tables for explicit packet labling protocols.
  38. *
  39. * Network protocols such as CIPSO and RIPSO require a label translation layer
  40. * to convert the label on the packet into something meaningful on the host
  41. * machine. In the current Linux implementation these mapping tables live
  42. * inside the kernel; NetLabel provides a mechanism for user space applications
  43. * to manage these mapping tables.
  44. *
  45. * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
  46. * send messages between kernel and user space. The general format of a
  47. * NetLabel message is shown below:
  48. *
  49. * +-----------------+-------------------+--------- --- -- -
  50. * | struct nlmsghdr | struct genlmsghdr | payload
  51. * +-----------------+-------------------+--------- --- -- -
  52. *
  53. * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
  54. * The payload is dependent on the subsystem specified in the
  55. * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
  56. * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
  57. * file. All of the fields in the NetLabel payload are NETLINK attributes, see
  58. * the include/net/netlink.h file for more information on NETLINK attributes.
  59. *
  60. */
  61. /*
  62. * NetLabel NETLINK protocol
  63. */
  64. #define NETLBL_PROTO_VERSION 1
  65. /* NetLabel NETLINK types/families */
  66. #define NETLBL_NLTYPE_NONE 0
  67. #define NETLBL_NLTYPE_MGMT 1
  68. #define NETLBL_NLTYPE_MGMT_NAME "NLBL_MGMT"
  69. #define NETLBL_NLTYPE_RIPSO 2
  70. #define NETLBL_NLTYPE_RIPSO_NAME "NLBL_RIPSO"
  71. #define NETLBL_NLTYPE_CIPSOV4 3
  72. #define NETLBL_NLTYPE_CIPSOV4_NAME "NLBL_CIPSOv4"
  73. #define NETLBL_NLTYPE_CIPSOV6 4
  74. #define NETLBL_NLTYPE_CIPSOV6_NAME "NLBL_CIPSOv6"
  75. #define NETLBL_NLTYPE_UNLABELED 5
  76. #define NETLBL_NLTYPE_UNLABELED_NAME "NLBL_UNLBL"
  77. /*
  78. * NetLabel - Kernel API for accessing the network packet label mappings.
  79. *
  80. * The following functions are provided for use by other kernel modules,
  81. * specifically kernel LSM modules, to provide a consistent, transparent API
  82. * for dealing with explicit packet labeling protocols such as CIPSO and
  83. * RIPSO. The functions defined here are implemented in the
  84. * net/netlabel/netlabel_kapi.c file.
  85. *
  86. */
  87. /* NetLabel audit information */
  88. struct netlbl_audit {
  89. u32 secid;
  90. uid_t loginuid;
  91. };
  92. /* Domain mapping definition struct */
  93. struct netlbl_dom_map;
  94. /* Domain mapping operations */
  95. int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info);
  96. /* LSM security attributes */
  97. struct netlbl_lsm_cache {
  98. atomic_t refcount;
  99. void (*free) (const void *data);
  100. void *data;
  101. };
  102. /* The catmap bitmap field MUST be a power of two in length and large
  103. * enough to hold at least 240 bits. Special care (i.e. check the code!)
  104. * should be used when changing these values as the LSM implementation
  105. * probably has functions which rely on the sizes of these types to speed
  106. * processing. */
  107. #define NETLBL_CATMAP_MAPTYPE u64
  108. #define NETLBL_CATMAP_MAPCNT 4
  109. #define NETLBL_CATMAP_MAPSIZE (sizeof(NETLBL_CATMAP_MAPTYPE) * 8)
  110. #define NETLBL_CATMAP_SIZE (NETLBL_CATMAP_MAPSIZE * \
  111. NETLBL_CATMAP_MAPCNT)
  112. #define NETLBL_CATMAP_BIT (NETLBL_CATMAP_MAPTYPE)0x01
  113. struct netlbl_lsm_secattr_catmap {
  114. u32 startbit;
  115. NETLBL_CATMAP_MAPTYPE bitmap[NETLBL_CATMAP_MAPCNT];
  116. struct netlbl_lsm_secattr_catmap *next;
  117. };
  118. #define NETLBL_SECATTR_NONE 0x00000000
  119. #define NETLBL_SECATTR_DOMAIN 0x00000001
  120. #define NETLBL_SECATTR_CACHE 0x00000002
  121. #define NETLBL_SECATTR_MLS_LVL 0x00000004
  122. #define NETLBL_SECATTR_MLS_CAT 0x00000008
  123. #define NETLBL_SECATTR_CACHEABLE (NETLBL_SECATTR_MLS_LVL | \
  124. NETLBL_SECATTR_MLS_CAT)
  125. struct netlbl_lsm_secattr {
  126. u32 flags;
  127. char *domain;
  128. u32 mls_lvl;
  129. struct netlbl_lsm_secattr_catmap *mls_cat;
  130. struct netlbl_lsm_cache *cache;
  131. };
  132. /*
  133. * LSM security attribute operations (inline)
  134. */
  135. /**
  136. * netlbl_secattr_cache_alloc - Allocate and initialize a secattr cache
  137. * @flags: the memory allocation flags
  138. *
  139. * Description:
  140. * Allocate and initialize a netlbl_lsm_cache structure. Returns a pointer
  141. * on success, NULL on failure.
  142. *
  143. */
  144. static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(gfp_t flags)
  145. {
  146. struct netlbl_lsm_cache *cache;
  147. cache = kzalloc(sizeof(*cache), flags);
  148. if (cache)
  149. atomic_set(&cache->refcount, 1);
  150. return cache;
  151. }
  152. /**
  153. * netlbl_secattr_cache_free - Frees a netlbl_lsm_cache struct
  154. * @cache: the struct to free
  155. *
  156. * Description:
  157. * Frees @secattr including all of the internal buffers.
  158. *
  159. */
  160. static inline void netlbl_secattr_cache_free(struct netlbl_lsm_cache *cache)
  161. {
  162. if (!atomic_dec_and_test(&cache->refcount))
  163. return;
  164. if (cache->free)
  165. cache->free(cache->data);
  166. kfree(cache);
  167. }
  168. /**
  169. * netlbl_secattr_catmap_alloc - Allocate a LSM secattr catmap
  170. * @flags: memory allocation flags
  171. *
  172. * Description:
  173. * Allocate memory for a LSM secattr catmap, returns a pointer on success, NULL
  174. * on failure.
  175. *
  176. */
  177. static inline struct netlbl_lsm_secattr_catmap *netlbl_secattr_catmap_alloc(
  178. gfp_t flags)
  179. {
  180. return kzalloc(sizeof(struct netlbl_lsm_secattr_catmap), flags);
  181. }
  182. /**
  183. * netlbl_secattr_catmap_free - Free a LSM secattr catmap
  184. * @catmap: the category bitmap
  185. *
  186. * Description:
  187. * Free a LSM secattr catmap.
  188. *
  189. */
  190. static inline void netlbl_secattr_catmap_free(
  191. struct netlbl_lsm_secattr_catmap *catmap)
  192. {
  193. struct netlbl_lsm_secattr_catmap *iter;
  194. do {
  195. iter = catmap;
  196. catmap = catmap->next;
  197. kfree(iter);
  198. } while (catmap);
  199. }
  200. /**
  201. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  202. * @secattr: the struct to initialize
  203. *
  204. * Description:
  205. * Initialize an already allocated netlbl_lsm_secattr struct.
  206. *
  207. */
  208. static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  209. {
  210. secattr->flags = 0;
  211. secattr->domain = NULL;
  212. secattr->mls_cat = NULL;
  213. secattr->cache = NULL;
  214. }
  215. /**
  216. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  217. * @secattr: the struct to clear
  218. *
  219. * Description:
  220. * Destroys the @secattr struct, including freeing all of the internal buffers.
  221. * The struct must be reset with a call to netlbl_secattr_init() before reuse.
  222. *
  223. */
  224. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
  225. {
  226. if (secattr->cache)
  227. netlbl_secattr_cache_free(secattr->cache);
  228. kfree(secattr->domain);
  229. if (secattr->mls_cat)
  230. netlbl_secattr_catmap_free(secattr->mls_cat);
  231. }
  232. /**
  233. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  234. * @flags: the memory allocation flags
  235. *
  236. * Description:
  237. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  238. * pointer on success, or NULL on failure.
  239. *
  240. */
  241. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
  242. {
  243. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  244. }
  245. /**
  246. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  247. * @secattr: the struct to free
  248. *
  249. * Description:
  250. * Frees @secattr including all of the internal buffers.
  251. *
  252. */
  253. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
  254. {
  255. netlbl_secattr_destroy(secattr);
  256. kfree(secattr);
  257. }
  258. #ifdef CONFIG_NETLABEL
  259. /*
  260. * LSM security attribute operations
  261. */
  262. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  263. u32 offset);
  264. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  265. u32 offset);
  266. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  267. u32 bit,
  268. gfp_t flags);
  269. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  270. u32 start,
  271. u32 end,
  272. gfp_t flags);
  273. /*
  274. * LSM protocol operations
  275. */
  276. int netlbl_enabled(void);
  277. int netlbl_sock_setattr(struct sock *sk,
  278. const struct netlbl_lsm_secattr *secattr);
  279. int netlbl_sock_getattr(struct sock *sk,
  280. struct netlbl_lsm_secattr *secattr);
  281. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  282. struct netlbl_lsm_secattr *secattr);
  283. void netlbl_skbuff_err(struct sk_buff *skb, int error);
  284. /*
  285. * LSM label mapping cache operations
  286. */
  287. void netlbl_cache_invalidate(void);
  288. int netlbl_cache_add(const struct sk_buff *skb,
  289. const struct netlbl_lsm_secattr *secattr);
  290. #else
  291. static inline int netlbl_secattr_catmap_walk(
  292. struct netlbl_lsm_secattr_catmap *catmap,
  293. u32 offset)
  294. {
  295. return -ENOENT;
  296. }
  297. static inline int netlbl_secattr_catmap_walk_rng(
  298. struct netlbl_lsm_secattr_catmap *catmap,
  299. u32 offset)
  300. {
  301. return -ENOENT;
  302. }
  303. static inline int netlbl_secattr_catmap_setbit(
  304. struct netlbl_lsm_secattr_catmap *catmap,
  305. u32 bit,
  306. gfp_t flags)
  307. {
  308. return 0;
  309. }
  310. static inline int netlbl_secattr_catmap_setrng(
  311. struct netlbl_lsm_secattr_catmap *catmap,
  312. u32 start,
  313. u32 end,
  314. gfp_t flags)
  315. {
  316. return 0;
  317. }
  318. static inline int netlbl_enabled(void)
  319. {
  320. return 0;
  321. }
  322. static inline int netlbl_sock_setattr(struct sock *sk,
  323. const struct netlbl_lsm_secattr *secattr)
  324. {
  325. return -ENOSYS;
  326. }
  327. static inline int netlbl_sock_getattr(struct sock *sk,
  328. struct netlbl_lsm_secattr *secattr)
  329. {
  330. return -ENOSYS;
  331. }
  332. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  333. struct netlbl_lsm_secattr *secattr)
  334. {
  335. return -ENOSYS;
  336. }
  337. static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
  338. {
  339. return;
  340. }
  341. static inline void netlbl_cache_invalidate(void)
  342. {
  343. return;
  344. }
  345. static inline int netlbl_cache_add(const struct sk_buff *skb,
  346. const struct netlbl_lsm_secattr *secattr)
  347. {
  348. return 0;
  349. }
  350. #endif /* CONFIG_NETLABEL */
  351. #endif /* _NETLABEL_H */