netlabel.h 17 KB

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