netlabel.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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@paul-moore.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, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. #ifndef _NETLABEL_H
  28. #define _NETLABEL_H
  29. #include <linux/types.h>
  30. #include <linux/slab.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 <linux/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. kuid_t loginuid;
  103. unsigned int 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_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_catmap {
  153. u32 startbit;
  154. NETLBL_CATMAP_MAPTYPE bitmap[NETLBL_CATMAP_MAPCNT];
  155. struct netlbl_lsm_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_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_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_catmap *netlbl_catmap_alloc(gfp_t flags)
  250. {
  251. return kzalloc(sizeof(struct netlbl_lsm_catmap), flags);
  252. }
  253. /**
  254. * netlbl_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_catmap_free(struct netlbl_lsm_catmap *catmap)
  262. {
  263. struct netlbl_lsm_catmap *iter;
  264. while (catmap) {
  265. iter = catmap;
  266. catmap = catmap->next;
  267. kfree(iter);
  268. }
  269. }
  270. /**
  271. * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
  272. * @secattr: the struct to initialize
  273. *
  274. * Description:
  275. * Initialize an already allocated netlbl_lsm_secattr struct.
  276. *
  277. */
  278. static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
  279. {
  280. memset(secattr, 0, sizeof(*secattr));
  281. }
  282. /**
  283. * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
  284. * @secattr: the struct to clear
  285. *
  286. * Description:
  287. * Destroys the @secattr struct, including freeing all of the internal buffers.
  288. * The struct must be reset with a call to netlbl_secattr_init() before reuse.
  289. *
  290. */
  291. static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
  292. {
  293. if (secattr->flags & NETLBL_SECATTR_FREE_DOMAIN)
  294. kfree(secattr->domain);
  295. if (secattr->flags & NETLBL_SECATTR_CACHE)
  296. netlbl_secattr_cache_free(secattr->cache);
  297. if (secattr->flags & NETLBL_SECATTR_MLS_CAT)
  298. netlbl_catmap_free(secattr->attr.mls.cat);
  299. }
  300. /**
  301. * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
  302. * @flags: the memory allocation flags
  303. *
  304. * Description:
  305. * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
  306. * pointer on success, or NULL on failure.
  307. *
  308. */
  309. static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
  310. {
  311. return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
  312. }
  313. /**
  314. * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
  315. * @secattr: the struct to free
  316. *
  317. * Description:
  318. * Frees @secattr including all of the internal buffers.
  319. *
  320. */
  321. static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
  322. {
  323. netlbl_secattr_destroy(secattr);
  324. kfree(secattr);
  325. }
  326. #ifdef CONFIG_NETLABEL
  327. /*
  328. * LSM configuration operations
  329. */
  330. int netlbl_cfg_map_del(const char *domain,
  331. u16 family,
  332. const void *addr,
  333. const void *mask,
  334. struct netlbl_audit *audit_info);
  335. int netlbl_cfg_unlbl_map_add(const char *domain,
  336. u16 family,
  337. const void *addr,
  338. const void *mask,
  339. struct netlbl_audit *audit_info);
  340. int netlbl_cfg_unlbl_static_add(struct net *net,
  341. const char *dev_name,
  342. const void *addr,
  343. const void *mask,
  344. u16 family,
  345. u32 secid,
  346. struct netlbl_audit *audit_info);
  347. int netlbl_cfg_unlbl_static_del(struct net *net,
  348. const char *dev_name,
  349. const void *addr,
  350. const void *mask,
  351. u16 family,
  352. struct netlbl_audit *audit_info);
  353. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  354. struct netlbl_audit *audit_info);
  355. void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info);
  356. int netlbl_cfg_cipsov4_map_add(u32 doi,
  357. const char *domain,
  358. const struct in_addr *addr,
  359. const struct in_addr *mask,
  360. struct netlbl_audit *audit_info);
  361. /*
  362. * LSM security attribute operations
  363. */
  364. int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset);
  365. int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset);
  366. int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
  367. u32 *offset,
  368. unsigned long *bitmap);
  369. int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
  370. u32 bit,
  371. gfp_t flags);
  372. int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
  373. u32 start,
  374. u32 end,
  375. gfp_t flags);
  376. int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
  377. u32 offset,
  378. unsigned long bitmap,
  379. gfp_t flags);
  380. /*
  381. * LSM protocol operations (NetLabel LSM/kernel API)
  382. */
  383. int netlbl_enabled(void);
  384. int netlbl_sock_setattr(struct sock *sk,
  385. u16 family,
  386. const struct netlbl_lsm_secattr *secattr);
  387. void netlbl_sock_delattr(struct sock *sk);
  388. int netlbl_sock_getattr(struct sock *sk,
  389. struct netlbl_lsm_secattr *secattr);
  390. int netlbl_conn_setattr(struct sock *sk,
  391. struct sockaddr *addr,
  392. const struct netlbl_lsm_secattr *secattr);
  393. int netlbl_req_setattr(struct request_sock *req,
  394. const struct netlbl_lsm_secattr *secattr);
  395. void netlbl_req_delattr(struct request_sock *req);
  396. int netlbl_skbuff_setattr(struct sk_buff *skb,
  397. u16 family,
  398. const struct netlbl_lsm_secattr *secattr);
  399. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  400. u16 family,
  401. struct netlbl_lsm_secattr *secattr);
  402. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway);
  403. /*
  404. * LSM label mapping cache operations
  405. */
  406. void netlbl_cache_invalidate(void);
  407. int netlbl_cache_add(const struct sk_buff *skb,
  408. const struct netlbl_lsm_secattr *secattr);
  409. /*
  410. * Protocol engine operations
  411. */
  412. struct audit_buffer *netlbl_audit_start(int type,
  413. struct netlbl_audit *audit_info);
  414. #else
  415. static inline int netlbl_cfg_map_del(const char *domain,
  416. u16 family,
  417. const void *addr,
  418. const void *mask,
  419. struct netlbl_audit *audit_info)
  420. {
  421. return -ENOSYS;
  422. }
  423. static inline int netlbl_cfg_unlbl_map_add(const char *domain,
  424. u16 family,
  425. void *addr,
  426. void *mask,
  427. struct netlbl_audit *audit_info)
  428. {
  429. return -ENOSYS;
  430. }
  431. static inline int netlbl_cfg_unlbl_static_add(struct net *net,
  432. const char *dev_name,
  433. const void *addr,
  434. const void *mask,
  435. u16 family,
  436. u32 secid,
  437. struct netlbl_audit *audit_info)
  438. {
  439. return -ENOSYS;
  440. }
  441. static inline int netlbl_cfg_unlbl_static_del(struct net *net,
  442. const char *dev_name,
  443. const void *addr,
  444. const void *mask,
  445. u16 family,
  446. struct netlbl_audit *audit_info)
  447. {
  448. return -ENOSYS;
  449. }
  450. static inline int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  451. struct netlbl_audit *audit_info)
  452. {
  453. return -ENOSYS;
  454. }
  455. static inline void netlbl_cfg_cipsov4_del(u32 doi,
  456. struct netlbl_audit *audit_info)
  457. {
  458. return;
  459. }
  460. static inline int netlbl_cfg_cipsov4_map_add(u32 doi,
  461. const char *domain,
  462. const struct in_addr *addr,
  463. const struct in_addr *mask,
  464. struct netlbl_audit *audit_info)
  465. {
  466. return -ENOSYS;
  467. }
  468. static inline int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap,
  469. u32 offset)
  470. {
  471. return -ENOENT;
  472. }
  473. static inline int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap,
  474. u32 offset)
  475. {
  476. return -ENOENT;
  477. }
  478. static inline int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
  479. u32 *offset,
  480. unsigned long *bitmap)
  481. {
  482. return 0;
  483. }
  484. static inline int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
  485. u32 bit,
  486. gfp_t flags)
  487. {
  488. return 0;
  489. }
  490. static inline int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
  491. u32 start,
  492. u32 end,
  493. gfp_t flags)
  494. {
  495. return 0;
  496. }
  497. static inline int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
  498. u32 offset,
  499. unsigned long bitmap,
  500. gfp_t flags)
  501. {
  502. return 0;
  503. }
  504. static inline int netlbl_enabled(void)
  505. {
  506. return 0;
  507. }
  508. static inline int netlbl_sock_setattr(struct sock *sk,
  509. u16 family,
  510. const struct netlbl_lsm_secattr *secattr)
  511. {
  512. return -ENOSYS;
  513. }
  514. static inline void netlbl_sock_delattr(struct sock *sk)
  515. {
  516. }
  517. static inline int netlbl_sock_getattr(struct sock *sk,
  518. struct netlbl_lsm_secattr *secattr)
  519. {
  520. return -ENOSYS;
  521. }
  522. static inline int netlbl_conn_setattr(struct sock *sk,
  523. struct sockaddr *addr,
  524. const struct netlbl_lsm_secattr *secattr)
  525. {
  526. return -ENOSYS;
  527. }
  528. static inline int netlbl_req_setattr(struct request_sock *req,
  529. const struct netlbl_lsm_secattr *secattr)
  530. {
  531. return -ENOSYS;
  532. }
  533. static inline void netlbl_req_delattr(struct request_sock *req)
  534. {
  535. return;
  536. }
  537. static inline int netlbl_skbuff_setattr(struct sk_buff *skb,
  538. u16 family,
  539. const struct netlbl_lsm_secattr *secattr)
  540. {
  541. return -ENOSYS;
  542. }
  543. static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
  544. u16 family,
  545. struct netlbl_lsm_secattr *secattr)
  546. {
  547. return -ENOSYS;
  548. }
  549. static inline void netlbl_skbuff_err(struct sk_buff *skb,
  550. int error,
  551. int gateway)
  552. {
  553. return;
  554. }
  555. static inline void netlbl_cache_invalidate(void)
  556. {
  557. return;
  558. }
  559. static inline int netlbl_cache_add(const struct sk_buff *skb,
  560. const struct netlbl_lsm_secattr *secattr)
  561. {
  562. return 0;
  563. }
  564. static inline struct audit_buffer *netlbl_audit_start(int type,
  565. struct netlbl_audit *audit_info)
  566. {
  567. return NULL;
  568. }
  569. #endif /* CONFIG_NETLABEL */
  570. #endif /* _NETLABEL_H */