netlabel_kapi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * NetLabel Kernel API
  3. *
  4. * This file defines the kernel API for the NetLabel system. The NetLabel
  5. * system manages static and dynamic label mappings for network protocols such
  6. * as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/audit.h>
  32. #include <net/ip.h>
  33. #include <net/netlabel.h>
  34. #include <net/cipso_ipv4.h>
  35. #include <asm/bug.h>
  36. #include <asm/atomic.h>
  37. #include "netlabel_domainhash.h"
  38. #include "netlabel_unlabeled.h"
  39. #include "netlabel_cipso_v4.h"
  40. #include "netlabel_user.h"
  41. #include "netlabel_mgmt.h"
  42. /*
  43. * Configuration Functions
  44. */
  45. /**
  46. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  47. * @domain: the domain mapping to remove
  48. * @audit_info: NetLabel audit information
  49. *
  50. * Description:
  51. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  52. * default domain mapping to be removed. Returns zero on success, negative
  53. * values on failure.
  54. *
  55. */
  56. int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info)
  57. {
  58. return netlbl_domhsh_remove(domain, audit_info);
  59. }
  60. /**
  61. * netlbl_cfg_unlbl_add_map - Add an unlabeled NetLabel/LSM domain mapping
  62. * @domain: the domain mapping to add
  63. * @audit_info: NetLabel audit information
  64. *
  65. * Description:
  66. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  67. * causes a new default domain mapping to be added. Returns zero on success,
  68. * negative values on failure.
  69. *
  70. */
  71. int netlbl_cfg_unlbl_add_map(const char *domain,
  72. struct netlbl_audit *audit_info)
  73. {
  74. int ret_val = -ENOMEM;
  75. struct netlbl_dom_map *entry;
  76. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  77. if (entry == NULL)
  78. return -ENOMEM;
  79. if (domain != NULL) {
  80. entry->domain = kstrdup(domain, GFP_ATOMIC);
  81. if (entry->domain == NULL)
  82. goto cfg_unlbl_add_map_failure;
  83. }
  84. entry->type = NETLBL_NLTYPE_UNLABELED;
  85. ret_val = netlbl_domhsh_add(entry, audit_info);
  86. if (ret_val != 0)
  87. goto cfg_unlbl_add_map_failure;
  88. return 0;
  89. cfg_unlbl_add_map_failure:
  90. if (entry != NULL)
  91. kfree(entry->domain);
  92. kfree(entry);
  93. return ret_val;
  94. }
  95. /**
  96. * netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping
  97. * @doi_def: the DOI definition
  98. * @domain: the domain mapping to add
  99. * @audit_info: NetLabel audit information
  100. *
  101. * Description:
  102. * Add a new CIPSOv4 DOI definition and NetLabel/LSM domain mapping for this
  103. * new DOI definition to the NetLabel subsystem. A @domain value of NULL adds
  104. * a new default domain mapping. Returns zero on success, negative values on
  105. * failure.
  106. *
  107. */
  108. int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
  109. const char *domain,
  110. struct netlbl_audit *audit_info)
  111. {
  112. int ret_val = -ENOMEM;
  113. u32 doi;
  114. u32 doi_type;
  115. struct netlbl_dom_map *entry;
  116. const char *type_str;
  117. struct audit_buffer *audit_buf;
  118. doi = doi_def->doi;
  119. doi_type = doi_def->type;
  120. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  121. if (entry == NULL)
  122. return -ENOMEM;
  123. if (domain != NULL) {
  124. entry->domain = kstrdup(domain, GFP_ATOMIC);
  125. if (entry->domain == NULL)
  126. goto cfg_cipsov4_add_map_failure;
  127. }
  128. ret_val = cipso_v4_doi_add(doi_def);
  129. if (ret_val != 0)
  130. goto cfg_cipsov4_add_map_failure_remove_doi;
  131. entry->type = NETLBL_NLTYPE_CIPSOV4;
  132. entry->type_def.cipsov4 = cipso_v4_doi_getdef(doi);
  133. if (entry->type_def.cipsov4 == NULL) {
  134. ret_val = -ENOENT;
  135. goto cfg_cipsov4_add_map_failure_remove_doi;
  136. }
  137. ret_val = netlbl_domhsh_add(entry, audit_info);
  138. if (ret_val != 0)
  139. goto cfg_cipsov4_add_map_failure_release_doi;
  140. cfg_cipsov4_add_map_return:
  141. audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
  142. audit_info);
  143. if (audit_buf != NULL) {
  144. switch (doi_type) {
  145. case CIPSO_V4_MAP_TRANS:
  146. type_str = "trans";
  147. break;
  148. case CIPSO_V4_MAP_PASS:
  149. type_str = "pass";
  150. break;
  151. case CIPSO_V4_MAP_LOCAL:
  152. type_str = "local";
  153. break;
  154. default:
  155. type_str = "(unknown)";
  156. }
  157. audit_log_format(audit_buf,
  158. " cipso_doi=%u cipso_type=%s res=%u",
  159. doi, type_str, ret_val == 0 ? 1 : 0);
  160. audit_log_end(audit_buf);
  161. }
  162. return ret_val;
  163. cfg_cipsov4_add_map_failure_release_doi:
  164. cipso_v4_doi_putdef(doi_def);
  165. cfg_cipsov4_add_map_failure_remove_doi:
  166. cipso_v4_doi_remove(doi, audit_info);
  167. cfg_cipsov4_add_map_failure:
  168. if (entry != NULL)
  169. kfree(entry->domain);
  170. kfree(entry);
  171. goto cfg_cipsov4_add_map_return;
  172. }
  173. /*
  174. * Security Attribute Functions
  175. */
  176. /**
  177. * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
  178. * @catmap: the category bitmap
  179. * @offset: the offset to start searching at, in bits
  180. *
  181. * Description:
  182. * This function walks a LSM secattr category bitmap starting at @offset and
  183. * returns the spot of the first set bit or -ENOENT if no bits are set.
  184. *
  185. */
  186. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  187. u32 offset)
  188. {
  189. struct netlbl_lsm_secattr_catmap *iter = catmap;
  190. u32 node_idx;
  191. u32 node_bit;
  192. NETLBL_CATMAP_MAPTYPE bitmap;
  193. if (offset > iter->startbit) {
  194. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  195. iter = iter->next;
  196. if (iter == NULL)
  197. return -ENOENT;
  198. }
  199. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  200. node_bit = offset - iter->startbit -
  201. (NETLBL_CATMAP_MAPSIZE * node_idx);
  202. } else {
  203. node_idx = 0;
  204. node_bit = 0;
  205. }
  206. bitmap = iter->bitmap[node_idx] >> node_bit;
  207. for (;;) {
  208. if (bitmap != 0) {
  209. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  210. bitmap >>= 1;
  211. node_bit++;
  212. }
  213. return iter->startbit +
  214. (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
  215. }
  216. if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  217. if (iter->next != NULL) {
  218. iter = iter->next;
  219. node_idx = 0;
  220. } else
  221. return -ENOENT;
  222. }
  223. bitmap = iter->bitmap[node_idx];
  224. node_bit = 0;
  225. }
  226. return -ENOENT;
  227. }
  228. /**
  229. * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
  230. * @catmap: the category bitmap
  231. * @offset: the offset to start searching at, in bits
  232. *
  233. * Description:
  234. * This function walks a LSM secattr category bitmap starting at @offset and
  235. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  236. * the end of the bitmap.
  237. *
  238. */
  239. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  240. u32 offset)
  241. {
  242. struct netlbl_lsm_secattr_catmap *iter = catmap;
  243. u32 node_idx;
  244. u32 node_bit;
  245. NETLBL_CATMAP_MAPTYPE bitmask;
  246. NETLBL_CATMAP_MAPTYPE bitmap;
  247. if (offset > iter->startbit) {
  248. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  249. iter = iter->next;
  250. if (iter == NULL)
  251. return -ENOENT;
  252. }
  253. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  254. node_bit = offset - iter->startbit -
  255. (NETLBL_CATMAP_MAPSIZE * node_idx);
  256. } else {
  257. node_idx = 0;
  258. node_bit = 0;
  259. }
  260. bitmask = NETLBL_CATMAP_BIT << node_bit;
  261. for (;;) {
  262. bitmap = iter->bitmap[node_idx];
  263. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  264. bitmask <<= 1;
  265. node_bit++;
  266. }
  267. if (bitmask != 0)
  268. return iter->startbit +
  269. (NETLBL_CATMAP_MAPSIZE * node_idx) +
  270. node_bit - 1;
  271. else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  272. if (iter->next == NULL)
  273. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  274. iter = iter->next;
  275. node_idx = 0;
  276. }
  277. bitmask = NETLBL_CATMAP_BIT;
  278. node_bit = 0;
  279. }
  280. return -ENOENT;
  281. }
  282. /**
  283. * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
  284. * @catmap: the category bitmap
  285. * @bit: the bit to set
  286. * @flags: memory allocation flags
  287. *
  288. * Description:
  289. * Set the bit specified by @bit in @catmap. Returns zero on success,
  290. * negative values on failure.
  291. *
  292. */
  293. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  294. u32 bit,
  295. gfp_t flags)
  296. {
  297. struct netlbl_lsm_secattr_catmap *iter = catmap;
  298. u32 node_bit;
  299. u32 node_idx;
  300. while (iter->next != NULL &&
  301. bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
  302. iter = iter->next;
  303. if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  304. iter->next = netlbl_secattr_catmap_alloc(flags);
  305. if (iter->next == NULL)
  306. return -ENOMEM;
  307. iter = iter->next;
  308. iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
  309. }
  310. /* gcc always rounds to zero when doing integer division */
  311. node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  312. node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
  313. iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
  314. return 0;
  315. }
  316. /**
  317. * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
  318. * @catmap: the category bitmap
  319. * @start: the starting bit
  320. * @end: the last bit in the string
  321. * @flags: memory allocation flags
  322. *
  323. * Description:
  324. * Set a range of bits, starting at @start and ending with @end. Returns zero
  325. * on success, negative values on failure.
  326. *
  327. */
  328. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  329. u32 start,
  330. u32 end,
  331. gfp_t flags)
  332. {
  333. int ret_val = 0;
  334. struct netlbl_lsm_secattr_catmap *iter = catmap;
  335. u32 iter_max_spot;
  336. u32 spot;
  337. /* XXX - This could probably be made a bit faster by combining writes
  338. * to the catmap instead of setting a single bit each time, but for
  339. * right now skipping to the start of the range in the catmap should
  340. * be a nice improvement over calling the individual setbit function
  341. * repeatedly from a loop. */
  342. while (iter->next != NULL &&
  343. start >= (iter->startbit + NETLBL_CATMAP_SIZE))
  344. iter = iter->next;
  345. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  346. for (spot = start; spot <= end && ret_val == 0; spot++) {
  347. if (spot >= iter_max_spot && iter->next != NULL) {
  348. iter = iter->next;
  349. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  350. }
  351. ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
  352. }
  353. return ret_val;
  354. }
  355. /*
  356. * LSM Functions
  357. */
  358. /**
  359. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  360. *
  361. * Description:
  362. * The LSM can use this function to determine if it should use NetLabel
  363. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  364. * considered to be enabled when it's configuration contains a valid setup for
  365. * at least one labeled protocol (i.e. NetLabel can understand incoming
  366. * labeled packets of at least one type); otherwise NetLabel is considered to
  367. * be disabled.
  368. *
  369. */
  370. int netlbl_enabled(void)
  371. {
  372. /* At some point we probably want to expose this mechanism to the user
  373. * as well so that admins can toggle NetLabel regardless of the
  374. * configuration */
  375. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  376. }
  377. /**
  378. * netlbl_socket_setattr - Label a socket using the correct protocol
  379. * @sk: the socket to label
  380. * @secattr: the security attributes
  381. *
  382. * Description:
  383. * Attach the correct label to the given socket using the security attributes
  384. * specified in @secattr. This function requires exclusive access to @sk,
  385. * which means it either needs to be in the process of being created or locked.
  386. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  387. * network address selectors (can't blindly label the socket), and negative
  388. * values on all other failures.
  389. *
  390. */
  391. int netlbl_sock_setattr(struct sock *sk,
  392. const struct netlbl_lsm_secattr *secattr)
  393. {
  394. int ret_val = -ENOENT;
  395. struct netlbl_dom_map *dom_entry;
  396. rcu_read_lock();
  397. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  398. if (dom_entry == NULL)
  399. goto socket_setattr_return;
  400. switch (dom_entry->type) {
  401. case NETLBL_NLTYPE_ADDRSELECT:
  402. ret_val = -EDESTADDRREQ;
  403. break;
  404. case NETLBL_NLTYPE_CIPSOV4:
  405. ret_val = cipso_v4_sock_setattr(sk,
  406. dom_entry->type_def.cipsov4,
  407. secattr);
  408. break;
  409. case NETLBL_NLTYPE_UNLABELED:
  410. ret_val = 0;
  411. break;
  412. default:
  413. ret_val = -ENOENT;
  414. }
  415. socket_setattr_return:
  416. rcu_read_unlock();
  417. return ret_val;
  418. }
  419. /**
  420. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  421. * @sk: the socket
  422. *
  423. * Description:
  424. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  425. * ensuring that @sk is locked.
  426. *
  427. */
  428. void netlbl_sock_delattr(struct sock *sk)
  429. {
  430. cipso_v4_sock_delattr(sk);
  431. }
  432. /**
  433. * netlbl_sock_getattr - Determine the security attributes of a sock
  434. * @sk: the sock
  435. * @secattr: the security attributes
  436. *
  437. * Description:
  438. * Examines the given sock to see if any NetLabel style labeling has been
  439. * applied to the sock, if so it parses the socket label and returns the
  440. * security attributes in @secattr. Returns zero on success, negative values
  441. * on failure.
  442. *
  443. */
  444. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  445. {
  446. return cipso_v4_sock_getattr(sk, secattr);
  447. }
  448. /**
  449. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  450. * @sk: the socket to label
  451. * @addr: the destination address
  452. * @secattr: the security attributes
  453. *
  454. * Description:
  455. * Attach the correct label to the given connected socket using the security
  456. * attributes specified in @secattr. The caller is responsible for ensuring
  457. * that @sk is locked. Returns zero on success, negative values on failure.
  458. *
  459. */
  460. int netlbl_conn_setattr(struct sock *sk,
  461. struct sockaddr *addr,
  462. const struct netlbl_lsm_secattr *secattr)
  463. {
  464. int ret_val;
  465. struct sockaddr_in *addr4;
  466. struct netlbl_domaddr4_map *af4_entry;
  467. rcu_read_lock();
  468. switch (addr->sa_family) {
  469. case AF_INET:
  470. addr4 = (struct sockaddr_in *)addr;
  471. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  472. addr4->sin_addr.s_addr);
  473. if (af4_entry == NULL) {
  474. ret_val = -ENOENT;
  475. goto conn_setattr_return;
  476. }
  477. switch (af4_entry->type) {
  478. case NETLBL_NLTYPE_CIPSOV4:
  479. ret_val = cipso_v4_sock_setattr(sk,
  480. af4_entry->type_def.cipsov4,
  481. secattr);
  482. break;
  483. case NETLBL_NLTYPE_UNLABELED:
  484. /* just delete the protocols we support for right now
  485. * but we could remove other protocols if needed */
  486. cipso_v4_sock_delattr(sk);
  487. ret_val = 0;
  488. break;
  489. default:
  490. ret_val = -ENOENT;
  491. }
  492. break;
  493. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  494. case AF_INET6:
  495. /* since we don't support any IPv6 labeling protocols right
  496. * now we can optimize everything away until we do */
  497. ret_val = 0;
  498. break;
  499. #endif /* IPv6 */
  500. default:
  501. ret_val = 0;
  502. }
  503. conn_setattr_return:
  504. rcu_read_unlock();
  505. return ret_val;
  506. }
  507. /**
  508. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  509. * @skb: the packet
  510. * @family: protocol family
  511. * @secattr: the security attributes
  512. *
  513. * Description:
  514. * Attach the correct label to the given packet using the security attributes
  515. * specified in @secattr. Returns zero on success, negative values on failure.
  516. *
  517. */
  518. int netlbl_skbuff_setattr(struct sk_buff *skb,
  519. u16 family,
  520. const struct netlbl_lsm_secattr *secattr)
  521. {
  522. int ret_val;
  523. struct iphdr *hdr4;
  524. struct netlbl_domaddr4_map *af4_entry;
  525. rcu_read_lock();
  526. switch (family) {
  527. case AF_INET:
  528. hdr4 = ip_hdr(skb);
  529. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  530. hdr4->daddr);
  531. if (af4_entry == NULL) {
  532. ret_val = -ENOENT;
  533. goto skbuff_setattr_return;
  534. }
  535. switch (af4_entry->type) {
  536. case NETLBL_NLTYPE_CIPSOV4:
  537. ret_val = cipso_v4_skbuff_setattr(skb,
  538. af4_entry->type_def.cipsov4,
  539. secattr);
  540. break;
  541. case NETLBL_NLTYPE_UNLABELED:
  542. /* just delete the protocols we support for right now
  543. * but we could remove other protocols if needed */
  544. ret_val = cipso_v4_skbuff_delattr(skb);
  545. break;
  546. default:
  547. ret_val = -ENOENT;
  548. }
  549. break;
  550. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  551. case AF_INET6:
  552. /* since we don't support any IPv6 labeling protocols right
  553. * now we can optimize everything away until we do */
  554. ret_val = 0;
  555. break;
  556. #endif /* IPv6 */
  557. default:
  558. ret_val = 0;
  559. }
  560. skbuff_setattr_return:
  561. rcu_read_unlock();
  562. return ret_val;
  563. }
  564. /**
  565. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  566. * @skb: the packet
  567. * @family: protocol family
  568. * @secattr: the security attributes
  569. *
  570. * Description:
  571. * Examines the given packet to see if a recognized form of packet labeling
  572. * is present, if so it parses the packet label and returns the security
  573. * attributes in @secattr. Returns zero on success, negative values on
  574. * failure.
  575. *
  576. */
  577. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  578. u16 family,
  579. struct netlbl_lsm_secattr *secattr)
  580. {
  581. if (CIPSO_V4_OPTEXIST(skb) &&
  582. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  583. return 0;
  584. return netlbl_unlabel_getattr(skb, family, secattr);
  585. }
  586. /**
  587. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  588. * @skb: the packet
  589. * @error: the error code
  590. * @gateway: true if host is acting as a gateway, false otherwise
  591. *
  592. * Description:
  593. * Deal with a LSM problem when handling the packet in @skb, typically this is
  594. * a permission denied problem (-EACCES). The correct action is determined
  595. * according to the packet's labeling protocol.
  596. *
  597. */
  598. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  599. {
  600. if (CIPSO_V4_OPTEXIST(skb))
  601. cipso_v4_error(skb, error, gateway);
  602. }
  603. /**
  604. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  605. *
  606. * Description:
  607. * For all of the NetLabel protocols that support some form of label mapping
  608. * cache, invalidate the cache. Returns zero on success, negative values on
  609. * error.
  610. *
  611. */
  612. void netlbl_cache_invalidate(void)
  613. {
  614. cipso_v4_cache_invalidate();
  615. }
  616. /**
  617. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  618. * @skb: the packet
  619. * @secattr: the packet's security attributes
  620. *
  621. * Description:
  622. * Add the LSM security attributes for the given packet to the underlying
  623. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  624. * values on error.
  625. *
  626. */
  627. int netlbl_cache_add(const struct sk_buff *skb,
  628. const struct netlbl_lsm_secattr *secattr)
  629. {
  630. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  631. return -ENOMSG;
  632. if (CIPSO_V4_OPTEXIST(skb))
  633. return cipso_v4_cache_add(skb, secattr);
  634. return -ENOMSG;
  635. }
  636. /*
  637. * Setup Functions
  638. */
  639. /**
  640. * netlbl_init - Initialize NetLabel
  641. *
  642. * Description:
  643. * Perform the required NetLabel initialization before first use.
  644. *
  645. */
  646. static int __init netlbl_init(void)
  647. {
  648. int ret_val;
  649. printk(KERN_INFO "NetLabel: Initializing\n");
  650. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  651. (1 << NETLBL_DOMHSH_BITSIZE));
  652. printk(KERN_INFO "NetLabel: protocols ="
  653. " UNLABELED"
  654. " CIPSOv4"
  655. "\n");
  656. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  657. if (ret_val != 0)
  658. goto init_failure;
  659. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  660. if (ret_val != 0)
  661. goto init_failure;
  662. ret_val = netlbl_netlink_init();
  663. if (ret_val != 0)
  664. goto init_failure;
  665. ret_val = netlbl_unlabel_defconf();
  666. if (ret_val != 0)
  667. goto init_failure;
  668. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  669. return 0;
  670. init_failure:
  671. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  672. }
  673. subsys_initcall(netlbl_init);