netlabel.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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, 2008
  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. struct cipso_v4_doi;
  36. /*
  37. * NetLabel - A management interface for maintaining network packet label
  38. * mapping tables for explicit packet labling protocols.
  39. *
  40. * Network protocols such as CIPSO and RIPSO require a label translation layer
  41. * to convert the label on the packet into something meaningful on the host
  42. * machine. In the current Linux implementation these mapping tables live
  43. * inside the kernel; NetLabel provides a mechanism for user space applications
  44. * to manage these mapping tables.
  45. *
  46. * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
  47. * send messages between kernel and user space. The general format of a
  48. * NetLabel message is shown below:
  49. *
  50. * +-----------------+-------------------+--------- --- -- -
  51. * | struct nlmsghdr | struct genlmsghdr | payload
  52. * +-----------------+-------------------+--------- --- -- -
  53. *
  54. * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
  55. * The payload is dependent on the subsystem specified in the
  56. * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
  57. * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
  58. * file. All of the fields in the NetLabel payload are NETLINK attributes, see
  59. * the include/net/netlink.h file for more information on NETLINK attributes.
  60. *
  61. */
  62. /*
  63. * NetLabel NETLINK protocol
  64. */
  65. /* NetLabel NETLINK protocol version
  66. * 1: initial version
  67. * 2: added static labels for unlabeled connections
  68. * 3: network selectors added to the NetLabel/LSM domain mapping and the
  69. * CIPSO_V4_MAP_LOCAL CIPSO mapping was added
  70. */
  71. #define NETLBL_PROTO_VERSION 3
  72. /* NetLabel NETLINK types/families */
  73. #define NETLBL_NLTYPE_NONE 0
  74. #define NETLBL_NLTYPE_MGMT 1
  75. #define NETLBL_NLTYPE_MGMT_NAME "NLBL_MGMT"
  76. #define NETLBL_NLTYPE_RIPSO 2
  77. #define NETLBL_NLTYPE_RIPSO_NAME "NLBL_RIPSO"
  78. #define NETLBL_NLTYPE_CIPSOV4 3
  79. #define NETLBL_NLTYPE_CIPSOV4_NAME "NLBL_CIPSOv4"
  80. #define NETLBL_NLTYPE_CIPSOV6 4
  81. #define NETLBL_NLTYPE_CIPSOV6_NAME "NLBL_CIPSOv6"
  82. #define NETLBL_NLTYPE_UNLABELED 5
  83. #define NETLBL_NLTYPE_UNLABELED_NAME "NLBL_UNLBL"
  84. #define NETLBL_NLTYPE_ADDRSELECT 6
  85. #define NETLBL_NLTYPE_ADDRSELECT_NAME "NLBL_ADRSEL"
  86. /*
  87. * NetLabel - Kernel API for accessing the network packet label mappings.
  88. *
  89. * The following functions are provided for use by other kernel modules,
  90. * specifically kernel LSM modules, to provide a consistent, transparent API
  91. * for dealing with explicit packet labeling protocols such as CIPSO and
  92. * RIPSO. The functions defined here are implemented in the
  93. * net/netlabel/netlabel_kapi.c file.
  94. *
  95. */
  96. /* NetLabel audit information */
  97. struct netlbl_audit {
  98. u32 secid;
  99. uid_t loginuid;
  100. u32 sessionid;
  101. };
  102. /*
  103. * LSM security attributes
  104. */
  105. /**
  106. * struct netlbl_lsm_cache - NetLabel LSM security attribute cache
  107. * @refcount: atomic reference counter
  108. * @free: LSM supplied function to free the cache data
  109. * @data: LSM supplied cache data
  110. *
  111. * Description:
  112. * This structure is provided for LSMs which wish to make use of the NetLabel
  113. * caching mechanism to store LSM specific data/attributes in the NetLabel
  114. * cache. If the LSM has to perform a lot of translation from the NetLabel
  115. * security attributes into it's own internal representation then the cache
  116. * mechanism can provide a way to eliminate some or all of that translation
  117. * overhead on a cache hit.
  118. *
  119. */
  120. struct netlbl_lsm_cache {
  121. atomic_t refcount;
  122. void (*free) (const void *data);
  123. void *data;
  124. };
  125. /**
  126. * struct netlbl_lsm_secattr_catmap - NetLabel LSM secattr category bitmap
  127. * @startbit: the value of the lowest order bit in the bitmap
  128. * @bitmap: the category bitmap
  129. * @next: pointer to the next bitmap "node" or NULL
  130. *
  131. * Description:
  132. * This structure is used to represent category bitmaps. Due to the large
  133. * number of categories supported by most labeling protocols it is not
  134. * practical to transfer a full bitmap internally so NetLabel adopts a sparse
  135. * bitmap structure modeled after SELinux's ebitmap structure.
  136. * The catmap bitmap field MUST be a power of two in length and large
  137. * enough to hold at least 240 bits. Special care (i.e. check the code!)
  138. * should be used when changing these values as the LSM implementation
  139. * probably has functions which rely on the sizes of these types to speed
  140. * processing.
  141. *
  142. */
  143. #define NETLBL_CATMAP_MAPTYPE u64
  144. #define NETLBL_CATMAP_MAPCNT 4
  145. #define NETLBL_CATMAP_MAPSIZE (sizeof(NETLBL_CATMAP_MAPTYPE) * 8)
  146. #define NETLBL_CATMAP_SIZE (NETLBL_CATMAP_MAPSIZE * \
  147. NETLBL_CATMAP_MAPCNT)
  148. #define NETLBL_CATMAP_BIT (NETLBL_CATMAP_MAPTYPE)0x01
  149. struct netlbl_lsm_secattr_catmap {
  150. u32 startbit;
  151. NETLBL_CATMAP_MAPTYPE bitmap[NETLBL_CATMAP_MAPCNT];
  152. struct netlbl_lsm_secattr_catmap *next;
  153. };
  154. /**
  155. * struct netlbl_lsm_secattr - NetLabel LSM security attributes
  156. * @flags: indicate structure attributes, see NETLBL_SECATTR_*
  157. * @type: indicate the NLTYPE of the attributes
  158. * @domain: the NetLabel LSM domain
  159. * @cache: NetLabel LSM specific cache
  160. * @attr.mls: MLS sensitivity label
  161. * @attr.mls.cat: MLS category bitmap
  162. * @attr.mls.lvl: MLS sensitivity level
  163. * @attr.secid: LSM specific secid token
  164. *
  165. * Description:
  166. * This structure is used to pass security attributes between NetLabel and the
  167. * LSM modules. The flags field is used to specify which fields within the
  168. * struct are valid and valid values can be created by bitwise OR'ing the
  169. * NETLBL_SECATTR_* defines. The domain field is typically set by the LSM to
  170. * specify domain specific configuration settings and is not usually used by
  171. * NetLabel itself when returning security attributes to the LSM.
  172. *
  173. */
  174. struct netlbl_lsm_secattr {
  175. u32 flags;
  176. /* bitmap values for 'flags' */
  177. #define NETLBL_SECATTR_NONE 0x00000000
  178. #define NETLBL_SECATTR_DOMAIN 0x00000001
  179. #define NETLBL_SECATTR_DOMAIN_CPY (NETLBL_SECATTR_DOMAIN | \
  180. NETLBL_SECATTR_FREE_DOMAIN)
  181. #define NETLBL_SECATTR_CACHE 0x00000002
  182. #define NETLBL_SECATTR_MLS_LVL 0x00000004
  183. #define NETLBL_SECATTR_MLS_CAT 0x00000008
  184. #define NETLBL_SECATTR_SECID 0x00000010
  185. /* bitmap meta-values for 'flags' */
  186. #define NETLBL_SECATTR_FREE_DOMAIN 0x01000000
  187. #define NETLBL_SECATTR_CACHEABLE (NETLBL_SECATTR_MLS_LVL | \
  188. NETLBL_SECATTR_MLS_CAT | \
  189. NETLBL_SECATTR_SECID)
  190. u32 type;
  191. char *domain;
  192. struct netlbl_lsm_cache *cache;
  193. struct {
  194. struct {
  195. struct netlbl_lsm_secattr_catmap *cat;
  196. u32 lvl;
  197. } mls;
  198. u32 secid;
  199. } attr;
  200. };
  201. /*
  202. * LSM security attribute operations (inline)
  203. */
  204. /**
  205. * netlbl_secattr_cache_alloc - Allocate and initialize a secattr cache
  206. * @flags: the memory allocation flags
  207. *
  208. * Description:
  209. * Allocate and initialize a netlbl_lsm_cache structure. Returns a pointer
  210. * on success, NULL on failure.
  211. *
  212. */
  213. static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(gfp_t flags)
  214. {
  215. struct netlbl_lsm_cache *cache;
  216. cache = kzalloc(sizeof(*cache), flags);
  217. if (cache)
  218. atomic_set(&cache->refcount, 1);
  219. return cache;
  220. }
  221. /**
  222. * netlbl_secattr_cache_free - Frees a netlbl_lsm_cache struct
  223. * @cache: the struct to free
  224. *
  225. * Description:
  226. * Frees @secattr including all of the internal buffers.
  227. *
  228. */
  229. static inline void netlbl_secattr_cache_free(struct netlbl_lsm_cache *cache)
  230. {
  231. if (!atomic_dec_and_test(&cache->refcount))
  232. return;
  233. if (cache->free)
  234. cache->free(cache->data);
  235. kfree(cache);
  236. }
  237. /**
  238. * netlbl_secattr_catmap_alloc - Allocate a LSM secattr catmap
  239. * @flags: memory allocation flags
  240. *
  241. * Description:
  242. * Allocate memory for a LSM secattr catmap, returns a pointer on success, NULL
  243. * on failure.
  244. *
  245. */
  246. static inline struct netlbl_lsm_secattr_catmap *netlbl_secattr_catmap_alloc(
  247. gfp_t flags)
  248. {
  249. return kzalloc(sizeof(struct netlbl_lsm_secattr_catmap), flags);
  250. }
  251. /**
  252. * netlbl_secattr_catmap_free - Free a LSM secattr catmap
  253. * @catmap: the category bitmap
  254. *
  255. * Description:
  256. * Free a LSM secattr catmap.
  257. *
  258. */
  259. static inline void netlbl_secattr_catmap_free(
  260. struct netlbl_lsm_secattr_catmap *catmap)
  261. {
  262. struct netlbl_lsm_secattr_catmap *iter;
  263. do {
  264. iter = catmap;
  265. catmap = catmap->next;
  266. kfree(iter);
  267. } while (catmap);
  268. }
  269. /**
  270. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  271. * @secattr: the struct to initialize
  272. *
  273. * Description:
  274. * Initialize an already allocated netlbl_lsm_secattr struct.
  275. *
  276. */
  277. static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  278. {
  279. memset(secattr, 0, sizeof(*secattr));
  280. }
  281. /**
  282. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  283. * @secattr: the struct to clear
  284. *
  285. * Description:
  286. * Destroys the @secattr struct, including freeing all of the internal buffers.
  287. * The struct must be reset with a call to netlbl_secattr_init() before reuse.
  288. *
  289. */
  290. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
  291. {
  292. if (secattr->flags & NETLBL_SECATTR_FREE_DOMAIN)
  293. kfree(secattr->domain);
  294. if (secattr->flags & NETLBL_SECATTR_CACHE)
  295. netlbl_secattr_cache_free(secattr->cache);
  296. if (secattr->flags & NETLBL_SECATTR_MLS_CAT)
  297. netlbl_secattr_catmap_free(secattr->attr.mls.cat);
  298. }
  299. /**
  300. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  301. * @flags: the memory allocation flags
  302. *
  303. * Description:
  304. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  305. * pointer on success, or NULL on failure.
  306. *
  307. */
  308. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
  309. {
  310. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  311. }
  312. /**
  313. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  314. * @secattr: the struct to free
  315. *
  316. * Description:
  317. * Frees @secattr including all of the internal buffers.
  318. *
  319. */
  320. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
  321. {
  322. netlbl_secattr_destroy(secattr);
  323. kfree(secattr);
  324. }
  325. #ifdef CONFIG_NETLABEL
  326. /*
  327. * LSM configuration operations
  328. */
  329. int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info);
  330. int netlbl_cfg_unlbl_add_map(const char *domain,
  331. struct netlbl_audit *audit_info);
  332. int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
  333. const char *domain,
  334. struct netlbl_audit *audit_info);
  335. /*
  336. * LSM security attribute operations
  337. */
  338. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  339. u32 offset);
  340. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  341. u32 offset);
  342. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  343. u32 bit,
  344. gfp_t flags);
  345. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  346. u32 start,
  347. u32 end,
  348. gfp_t flags);
  349. /*
  350. * LSM protocol operations (NetLabel LSM/kernel API)
  351. */
  352. int netlbl_enabled(void);
  353. int netlbl_sock_setattr(struct sock *sk,
  354. const struct netlbl_lsm_secattr *secattr);
  355. void netlbl_sock_delattr(struct sock *sk);
  356. int netlbl_sock_getattr(struct sock *sk,
  357. struct netlbl_lsm_secattr *secattr);
  358. int netlbl_conn_setattr(struct sock *sk,
  359. struct sockaddr *addr,
  360. const struct netlbl_lsm_secattr *secattr);
  361. int netlbl_skbuff_setattr(struct sk_buff *skb,
  362. u16 family,
  363. const struct netlbl_lsm_secattr *secattr);
  364. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  365. u16 family,
  366. struct netlbl_lsm_secattr *secattr);
  367. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway);
  368. /*
  369. * LSM label mapping cache operations
  370. */
  371. void netlbl_cache_invalidate(void);
  372. int netlbl_cache_add(const struct sk_buff *skb,
  373. const struct netlbl_lsm_secattr *secattr);
  374. #else
  375. static inline int netlbl_cfg_map_del(const char *domain,
  376. struct netlbl_audit *audit_info)
  377. {
  378. return -ENOSYS;
  379. }
  380. static inline int netlbl_cfg_unlbl_add_map(const char *domain,
  381. struct netlbl_audit *audit_info)
  382. {
  383. return -ENOSYS;
  384. }
  385. static inline int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
  386. const char *domain,
  387. struct netlbl_audit *audit_info)
  388. {
  389. return -ENOSYS;
  390. }
  391. static inline int netlbl_secattr_catmap_walk(
  392. struct netlbl_lsm_secattr_catmap *catmap,
  393. u32 offset)
  394. {
  395. return -ENOENT;
  396. }
  397. static inline int netlbl_secattr_catmap_walk_rng(
  398. struct netlbl_lsm_secattr_catmap *catmap,
  399. u32 offset)
  400. {
  401. return -ENOENT;
  402. }
  403. static inline int netlbl_secattr_catmap_setbit(
  404. struct netlbl_lsm_secattr_catmap *catmap,
  405. u32 bit,
  406. gfp_t flags)
  407. {
  408. return 0;
  409. }
  410. static inline int netlbl_secattr_catmap_setrng(
  411. struct netlbl_lsm_secattr_catmap *catmap,
  412. u32 start,
  413. u32 end,
  414. gfp_t flags)
  415. {
  416. return 0;
  417. }
  418. static inline int netlbl_enabled(void)
  419. {
  420. return 0;
  421. }
  422. static inline int netlbl_sock_setattr(struct sock *sk,
  423. const struct netlbl_lsm_secattr *secattr)
  424. {
  425. return -ENOSYS;
  426. }
  427. static inline void netlbl_sock_delattr(struct sock *sk)
  428. {
  429. }
  430. static inline int netlbl_sock_getattr(struct sock *sk,
  431. struct netlbl_lsm_secattr *secattr)
  432. {
  433. return -ENOSYS;
  434. }
  435. static inline int netlbl_conn_setattr(struct sock *sk,
  436. struct sockaddr *addr,
  437. const struct netlbl_lsm_secattr *secattr)
  438. {
  439. return -ENOSYS;
  440. }
  441. static inline int netlbl_skbuff_setattr(struct sk_buff *skb,
  442. u16 family,
  443. const struct netlbl_lsm_secattr *secattr)
  444. {
  445. return -ENOSYS;
  446. }
  447. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  448. u16 family,
  449. struct netlbl_lsm_secattr *secattr)
  450. {
  451. return -ENOSYS;
  452. }
  453. static inline void netlbl_skbuff_err(struct sk_buff *skb,
  454. int error,
  455. int gateway)
  456. {
  457. return;
  458. }
  459. static inline void netlbl_cache_invalidate(void)
  460. {
  461. return;
  462. }
  463. static inline int netlbl_cache_add(const struct sk_buff *skb,
  464. const struct netlbl_lsm_secattr *secattr)
  465. {
  466. return 0;
  467. }
  468. #endif /* CONFIG_NETLABEL */
  469. #endif /* _NETLABEL_H */