netlabel.h 17 KB

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