netlabel.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. /*
  97. * LSM security attributes
  98. */
  99. /**
  100. * struct netlbl_lsm_cache - NetLabel LSM security attribute cache
  101. * @refcount: atomic reference counter
  102. * @free: LSM supplied function to free the cache data
  103. * @data: LSM supplied cache data
  104. *
  105. * Description:
  106. * This structure is provided for LSMs which wish to make use of the NetLabel
  107. * caching mechanism to store LSM specific data/attributes in the NetLabel
  108. * cache. If the LSM has to perform a lot of translation from the NetLabel
  109. * security attributes into it's own internal representation then the cache
  110. * mechanism can provide a way to eliminate some or all of that translation
  111. * overhead on a cache hit.
  112. *
  113. */
  114. struct netlbl_lsm_cache {
  115. atomic_t refcount;
  116. void (*free) (const void *data);
  117. void *data;
  118. };
  119. /**
  120. * struct netlbl_lsm_secattr_catmap - NetLabel LSM secattr category bitmap
  121. * @startbit: the value of the lowest order bit in the bitmap
  122. * @bitmap: the category bitmap
  123. * @next: pointer to the next bitmap "node" or NULL
  124. *
  125. * Description:
  126. * This structure is used to represent category bitmaps. Due to the large
  127. * number of categories supported by most labeling protocols it is not
  128. * practical to transfer a full bitmap internally so NetLabel adopts a sparse
  129. * bitmap structure modeled after SELinux's ebitmap structure.
  130. * The catmap bitmap field MUST be a power of two in length and large
  131. * enough to hold at least 240 bits. Special care (i.e. check the code!)
  132. * should be used when changing these values as the LSM implementation
  133. * probably has functions which rely on the sizes of these types to speed
  134. * processing.
  135. *
  136. */
  137. #define NETLBL_CATMAP_MAPTYPE u64
  138. #define NETLBL_CATMAP_MAPCNT 4
  139. #define NETLBL_CATMAP_MAPSIZE (sizeof(NETLBL_CATMAP_MAPTYPE) * 8)
  140. #define NETLBL_CATMAP_SIZE (NETLBL_CATMAP_MAPSIZE * \
  141. NETLBL_CATMAP_MAPCNT)
  142. #define NETLBL_CATMAP_BIT (NETLBL_CATMAP_MAPTYPE)0x01
  143. struct netlbl_lsm_secattr_catmap {
  144. u32 startbit;
  145. NETLBL_CATMAP_MAPTYPE bitmap[NETLBL_CATMAP_MAPCNT];
  146. struct netlbl_lsm_secattr_catmap *next;
  147. };
  148. /**
  149. * struct netlbl_lsm_secattr - NetLabel LSM security attributes
  150. * @flags: indicate which attributes are contained in this structure
  151. * @type: indicate the NLTYPE of the attributes
  152. * @domain: the NetLabel LSM domain
  153. * @cache: NetLabel LSM specific cache
  154. * @attr.mls: MLS sensitivity label
  155. * @attr.mls.cat: MLS category bitmap
  156. * @attr.mls.lvl: MLS sensitivity level
  157. * @attr.secid: LSM specific secid token
  158. *
  159. * Description:
  160. * This structure is used to pass security attributes between NetLabel and the
  161. * LSM modules. The flags field is used to specify which fields within the
  162. * struct are valid and valid values can be created by bitwise OR'ing the
  163. * NETLBL_SECATTR_* defines. The domain field is typically set by the LSM to
  164. * specify domain specific configuration settings and is not usually used by
  165. * NetLabel itself when returning security attributes to the LSM.
  166. *
  167. */
  168. #define NETLBL_SECATTR_NONE 0x00000000
  169. #define NETLBL_SECATTR_DOMAIN 0x00000001
  170. #define NETLBL_SECATTR_CACHE 0x00000002
  171. #define NETLBL_SECATTR_MLS_LVL 0x00000004
  172. #define NETLBL_SECATTR_MLS_CAT 0x00000008
  173. #define NETLBL_SECATTR_SECID 0x00000010
  174. #define NETLBL_SECATTR_CACHEABLE (NETLBL_SECATTR_MLS_LVL | \
  175. NETLBL_SECATTR_MLS_CAT | \
  176. NETLBL_SECATTR_SECID)
  177. struct netlbl_lsm_secattr {
  178. u32 flags;
  179. u32 type;
  180. char *domain;
  181. struct netlbl_lsm_cache *cache;
  182. union {
  183. struct {
  184. struct netlbl_lsm_secattr_catmap *cat;
  185. u32 lvl;
  186. } mls;
  187. u32 secid;
  188. } attr;
  189. };
  190. /*
  191. * LSM security attribute operations (inline)
  192. */
  193. /**
  194. * netlbl_secattr_cache_alloc - Allocate and initialize a secattr cache
  195. * @flags: the memory allocation flags
  196. *
  197. * Description:
  198. * Allocate and initialize a netlbl_lsm_cache structure. Returns a pointer
  199. * on success, NULL on failure.
  200. *
  201. */
  202. static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(gfp_t flags)
  203. {
  204. struct netlbl_lsm_cache *cache;
  205. cache = kzalloc(sizeof(*cache), flags);
  206. if (cache)
  207. atomic_set(&cache->refcount, 1);
  208. return cache;
  209. }
  210. /**
  211. * netlbl_secattr_cache_free - Frees a netlbl_lsm_cache struct
  212. * @cache: the struct to free
  213. *
  214. * Description:
  215. * Frees @secattr including all of the internal buffers.
  216. *
  217. */
  218. static inline void netlbl_secattr_cache_free(struct netlbl_lsm_cache *cache)
  219. {
  220. if (!atomic_dec_and_test(&cache->refcount))
  221. return;
  222. if (cache->free)
  223. cache->free(cache->data);
  224. kfree(cache);
  225. }
  226. /**
  227. * netlbl_secattr_catmap_alloc - Allocate a LSM secattr catmap
  228. * @flags: memory allocation flags
  229. *
  230. * Description:
  231. * Allocate memory for a LSM secattr catmap, returns a pointer on success, NULL
  232. * on failure.
  233. *
  234. */
  235. static inline struct netlbl_lsm_secattr_catmap *netlbl_secattr_catmap_alloc(
  236. gfp_t flags)
  237. {
  238. return kzalloc(sizeof(struct netlbl_lsm_secattr_catmap), flags);
  239. }
  240. /**
  241. * netlbl_secattr_catmap_free - Free a LSM secattr catmap
  242. * @catmap: the category bitmap
  243. *
  244. * Description:
  245. * Free a LSM secattr catmap.
  246. *
  247. */
  248. static inline void netlbl_secattr_catmap_free(
  249. struct netlbl_lsm_secattr_catmap *catmap)
  250. {
  251. struct netlbl_lsm_secattr_catmap *iter;
  252. do {
  253. iter = catmap;
  254. catmap = catmap->next;
  255. kfree(iter);
  256. } while (catmap);
  257. }
  258. /**
  259. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  260. * @secattr: the struct to initialize
  261. *
  262. * Description:
  263. * Initialize an already allocated netlbl_lsm_secattr struct.
  264. *
  265. */
  266. static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  267. {
  268. memset(secattr, 0, sizeof(*secattr));
  269. }
  270. /**
  271. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  272. * @secattr: the struct to clear
  273. *
  274. * Description:
  275. * Destroys the @secattr struct, including freeing all of the internal buffers.
  276. * The struct must be reset with a call to netlbl_secattr_init() before reuse.
  277. *
  278. */
  279. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
  280. {
  281. kfree(secattr->domain);
  282. if (secattr->flags & NETLBL_SECATTR_CACHE)
  283. netlbl_secattr_cache_free(secattr->cache);
  284. if (secattr->flags & NETLBL_SECATTR_MLS_CAT)
  285. netlbl_secattr_catmap_free(secattr->attr.mls.cat);
  286. }
  287. /**
  288. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  289. * @flags: the memory allocation flags
  290. *
  291. * Description:
  292. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  293. * pointer on success, or NULL on failure.
  294. *
  295. */
  296. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
  297. {
  298. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  299. }
  300. /**
  301. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  302. * @secattr: the struct to free
  303. *
  304. * Description:
  305. * Frees @secattr including all of the internal buffers.
  306. *
  307. */
  308. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
  309. {
  310. netlbl_secattr_destroy(secattr);
  311. kfree(secattr);
  312. }
  313. #ifdef CONFIG_NETLABEL
  314. /*
  315. * LSM security attribute operations
  316. */
  317. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  318. u32 offset);
  319. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  320. u32 offset);
  321. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  322. u32 bit,
  323. gfp_t flags);
  324. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  325. u32 start,
  326. u32 end,
  327. gfp_t flags);
  328. /*
  329. * LSM protocol operations (NetLabel LSM/kernel API)
  330. */
  331. int netlbl_enabled(void);
  332. int netlbl_sock_setattr(struct sock *sk,
  333. const struct netlbl_lsm_secattr *secattr);
  334. int netlbl_sock_getattr(struct sock *sk,
  335. struct netlbl_lsm_secattr *secattr);
  336. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  337. u16 family,
  338. struct netlbl_lsm_secattr *secattr);
  339. void netlbl_skbuff_err(struct sk_buff *skb, int error);
  340. /*
  341. * LSM label mapping cache operations
  342. */
  343. void netlbl_cache_invalidate(void);
  344. int netlbl_cache_add(const struct sk_buff *skb,
  345. const struct netlbl_lsm_secattr *secattr);
  346. #else
  347. static inline int netlbl_secattr_catmap_walk(
  348. struct netlbl_lsm_secattr_catmap *catmap,
  349. u32 offset)
  350. {
  351. return -ENOENT;
  352. }
  353. static inline int netlbl_secattr_catmap_walk_rng(
  354. struct netlbl_lsm_secattr_catmap *catmap,
  355. u32 offset)
  356. {
  357. return -ENOENT;
  358. }
  359. static inline int netlbl_secattr_catmap_setbit(
  360. struct netlbl_lsm_secattr_catmap *catmap,
  361. u32 bit,
  362. gfp_t flags)
  363. {
  364. return 0;
  365. }
  366. static inline int netlbl_secattr_catmap_setrng(
  367. struct netlbl_lsm_secattr_catmap *catmap,
  368. u32 start,
  369. u32 end,
  370. gfp_t flags)
  371. {
  372. return 0;
  373. }
  374. static inline int netlbl_enabled(void)
  375. {
  376. return 0;
  377. }
  378. static inline int netlbl_sock_setattr(struct sock *sk,
  379. const struct netlbl_lsm_secattr *secattr)
  380. {
  381. return -ENOSYS;
  382. }
  383. static inline int netlbl_sock_getattr(struct sock *sk,
  384. struct netlbl_lsm_secattr *secattr)
  385. {
  386. return -ENOSYS;
  387. }
  388. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  389. u16 family,
  390. struct netlbl_lsm_secattr *secattr)
  391. {
  392. return -ENOSYS;
  393. }
  394. static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
  395. {
  396. return;
  397. }
  398. static inline void netlbl_cache_invalidate(void)
  399. {
  400. return;
  401. }
  402. static inline int netlbl_cache_add(const struct sk_buff *skb,
  403. const struct netlbl_lsm_secattr *secattr)
  404. {
  405. return 0;
  406. }
  407. #endif /* CONFIG_NETLABEL */
  408. #endif /* _NETLABEL_H */