netlabel.h 17 KB

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