netlabel_kapi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_STD:
  146. type_str = "std";
  147. break;
  148. case CIPSO_V4_MAP_PASS:
  149. type_str = "pass";
  150. break;
  151. default:
  152. type_str = "(unknown)";
  153. }
  154. audit_log_format(audit_buf,
  155. " cipso_doi=%u cipso_type=%s res=%u",
  156. doi, type_str, ret_val == 0 ? 1 : 0);
  157. audit_log_end(audit_buf);
  158. }
  159. return ret_val;
  160. cfg_cipsov4_add_map_failure_release_doi:
  161. cipso_v4_doi_putdef(doi_def);
  162. cfg_cipsov4_add_map_failure_remove_doi:
  163. cipso_v4_doi_remove(doi, audit_info);
  164. cfg_cipsov4_add_map_failure:
  165. if (entry != NULL)
  166. kfree(entry->domain);
  167. kfree(entry);
  168. goto cfg_cipsov4_add_map_return;
  169. }
  170. /*
  171. * Security Attribute Functions
  172. */
  173. /**
  174. * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
  175. * @catmap: the category bitmap
  176. * @offset: the offset to start searching at, in bits
  177. *
  178. * Description:
  179. * This function walks a LSM secattr category bitmap starting at @offset and
  180. * returns the spot of the first set bit or -ENOENT if no bits are set.
  181. *
  182. */
  183. int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
  184. u32 offset)
  185. {
  186. struct netlbl_lsm_secattr_catmap *iter = catmap;
  187. u32 node_idx;
  188. u32 node_bit;
  189. NETLBL_CATMAP_MAPTYPE bitmap;
  190. if (offset > iter->startbit) {
  191. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  192. iter = iter->next;
  193. if (iter == NULL)
  194. return -ENOENT;
  195. }
  196. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  197. node_bit = offset - iter->startbit -
  198. (NETLBL_CATMAP_MAPSIZE * node_idx);
  199. } else {
  200. node_idx = 0;
  201. node_bit = 0;
  202. }
  203. bitmap = iter->bitmap[node_idx] >> node_bit;
  204. for (;;) {
  205. if (bitmap != 0) {
  206. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  207. bitmap >>= 1;
  208. node_bit++;
  209. }
  210. return iter->startbit +
  211. (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
  212. }
  213. if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  214. if (iter->next != NULL) {
  215. iter = iter->next;
  216. node_idx = 0;
  217. } else
  218. return -ENOENT;
  219. }
  220. bitmap = iter->bitmap[node_idx];
  221. node_bit = 0;
  222. }
  223. return -ENOENT;
  224. }
  225. /**
  226. * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
  227. * @catmap: the category bitmap
  228. * @offset: the offset to start searching at, in bits
  229. *
  230. * Description:
  231. * This function walks a LSM secattr category bitmap starting at @offset and
  232. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  233. * the end of the bitmap.
  234. *
  235. */
  236. int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
  237. u32 offset)
  238. {
  239. struct netlbl_lsm_secattr_catmap *iter = catmap;
  240. u32 node_idx;
  241. u32 node_bit;
  242. NETLBL_CATMAP_MAPTYPE bitmask;
  243. NETLBL_CATMAP_MAPTYPE bitmap;
  244. if (offset > iter->startbit) {
  245. while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  246. iter = iter->next;
  247. if (iter == NULL)
  248. return -ENOENT;
  249. }
  250. node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  251. node_bit = offset - iter->startbit -
  252. (NETLBL_CATMAP_MAPSIZE * node_idx);
  253. } else {
  254. node_idx = 0;
  255. node_bit = 0;
  256. }
  257. bitmask = NETLBL_CATMAP_BIT << node_bit;
  258. for (;;) {
  259. bitmap = iter->bitmap[node_idx];
  260. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  261. bitmask <<= 1;
  262. node_bit++;
  263. }
  264. if (bitmask != 0)
  265. return iter->startbit +
  266. (NETLBL_CATMAP_MAPSIZE * node_idx) +
  267. node_bit - 1;
  268. else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
  269. if (iter->next == NULL)
  270. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  271. iter = iter->next;
  272. node_idx = 0;
  273. }
  274. bitmask = NETLBL_CATMAP_BIT;
  275. node_bit = 0;
  276. }
  277. return -ENOENT;
  278. }
  279. /**
  280. * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
  281. * @catmap: the category bitmap
  282. * @bit: the bit to set
  283. * @flags: memory allocation flags
  284. *
  285. * Description:
  286. * Set the bit specified by @bit in @catmap. Returns zero on success,
  287. * negative values on failure.
  288. *
  289. */
  290. int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
  291. u32 bit,
  292. gfp_t flags)
  293. {
  294. struct netlbl_lsm_secattr_catmap *iter = catmap;
  295. u32 node_bit;
  296. u32 node_idx;
  297. while (iter->next != NULL &&
  298. bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
  299. iter = iter->next;
  300. if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  301. iter->next = netlbl_secattr_catmap_alloc(flags);
  302. if (iter->next == NULL)
  303. return -ENOMEM;
  304. iter = iter->next;
  305. iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
  306. }
  307. /* gcc always rounds to zero when doing integer division */
  308. node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
  309. node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
  310. iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
  311. return 0;
  312. }
  313. /**
  314. * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
  315. * @catmap: the category bitmap
  316. * @start: the starting bit
  317. * @end: the last bit in the string
  318. * @flags: memory allocation flags
  319. *
  320. * Description:
  321. * Set a range of bits, starting at @start and ending with @end. Returns zero
  322. * on success, negative values on failure.
  323. *
  324. */
  325. int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
  326. u32 start,
  327. u32 end,
  328. gfp_t flags)
  329. {
  330. int ret_val = 0;
  331. struct netlbl_lsm_secattr_catmap *iter = catmap;
  332. u32 iter_max_spot;
  333. u32 spot;
  334. /* XXX - This could probably be made a bit faster by combining writes
  335. * to the catmap instead of setting a single bit each time, but for
  336. * right now skipping to the start of the range in the catmap should
  337. * be a nice improvement over calling the individual setbit function
  338. * repeatedly from a loop. */
  339. while (iter->next != NULL &&
  340. start >= (iter->startbit + NETLBL_CATMAP_SIZE))
  341. iter = iter->next;
  342. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  343. for (spot = start; spot <= end && ret_val == 0; spot++) {
  344. if (spot >= iter_max_spot && iter->next != NULL) {
  345. iter = iter->next;
  346. iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
  347. }
  348. ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
  349. }
  350. return ret_val;
  351. }
  352. /*
  353. * LSM Functions
  354. */
  355. /**
  356. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  357. *
  358. * Description:
  359. * The LSM can use this function to determine if it should use NetLabel
  360. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  361. * considered to be enabled when it's configuration contains a valid setup for
  362. * at least one labeled protocol (i.e. NetLabel can understand incoming
  363. * labeled packets of at least one type); otherwise NetLabel is considered to
  364. * be disabled.
  365. *
  366. */
  367. int netlbl_enabled(void)
  368. {
  369. /* At some point we probably want to expose this mechanism to the user
  370. * as well so that admins can toggle NetLabel regardless of the
  371. * configuration */
  372. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  373. }
  374. /**
  375. * netlbl_socket_setattr - Label a socket using the correct protocol
  376. * @sk: the socket to label
  377. * @secattr: the security attributes
  378. *
  379. * Description:
  380. * Attach the correct label to the given socket using the security attributes
  381. * specified in @secattr. This function requires exclusive access to @sk,
  382. * which means it either needs to be in the process of being created or locked.
  383. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  384. * network address selectors (can't blindly label the socket), and negative
  385. * values on all other failures.
  386. *
  387. */
  388. int netlbl_sock_setattr(struct sock *sk,
  389. const struct netlbl_lsm_secattr *secattr)
  390. {
  391. int ret_val = -ENOENT;
  392. struct netlbl_dom_map *dom_entry;
  393. rcu_read_lock();
  394. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  395. if (dom_entry == NULL)
  396. goto socket_setattr_return;
  397. switch (dom_entry->type) {
  398. case NETLBL_NLTYPE_ADDRSELECT:
  399. ret_val = -EDESTADDRREQ;
  400. break;
  401. case NETLBL_NLTYPE_CIPSOV4:
  402. ret_val = cipso_v4_sock_setattr(sk,
  403. dom_entry->type_def.cipsov4,
  404. secattr);
  405. break;
  406. case NETLBL_NLTYPE_UNLABELED:
  407. ret_val = 0;
  408. break;
  409. default:
  410. ret_val = -ENOENT;
  411. }
  412. socket_setattr_return:
  413. rcu_read_unlock();
  414. return ret_val;
  415. }
  416. /**
  417. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  418. * @sk: the socket
  419. *
  420. * Description:
  421. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  422. * ensuring that @sk is locked.
  423. *
  424. */
  425. void netlbl_sock_delattr(struct sock *sk)
  426. {
  427. cipso_v4_sock_delattr(sk);
  428. }
  429. /**
  430. * netlbl_sock_getattr - Determine the security attributes of a sock
  431. * @sk: the sock
  432. * @secattr: the security attributes
  433. *
  434. * Description:
  435. * Examines the given sock to see if any NetLabel style labeling has been
  436. * applied to the sock, if so it parses the socket label and returns the
  437. * security attributes in @secattr. Returns zero on success, negative values
  438. * on failure.
  439. *
  440. */
  441. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  442. {
  443. return cipso_v4_sock_getattr(sk, secattr);
  444. }
  445. /**
  446. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  447. * @sk: the socket to label
  448. * @addr: the destination address
  449. * @secattr: the security attributes
  450. *
  451. * Description:
  452. * Attach the correct label to the given connected socket using the security
  453. * attributes specified in @secattr. The caller is responsible for ensuring
  454. * that @sk is locked. Returns zero on success, negative values on failure.
  455. *
  456. */
  457. int netlbl_conn_setattr(struct sock *sk,
  458. struct sockaddr *addr,
  459. const struct netlbl_lsm_secattr *secattr)
  460. {
  461. int ret_val;
  462. struct sockaddr_in *addr4;
  463. struct netlbl_domaddr4_map *af4_entry;
  464. rcu_read_lock();
  465. switch (addr->sa_family) {
  466. case AF_INET:
  467. addr4 = (struct sockaddr_in *)addr;
  468. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  469. addr4->sin_addr.s_addr);
  470. if (af4_entry == NULL) {
  471. ret_val = -ENOENT;
  472. goto conn_setattr_return;
  473. }
  474. switch (af4_entry->type) {
  475. case NETLBL_NLTYPE_CIPSOV4:
  476. ret_val = cipso_v4_sock_setattr(sk,
  477. af4_entry->type_def.cipsov4,
  478. secattr);
  479. break;
  480. case NETLBL_NLTYPE_UNLABELED:
  481. /* just delete the protocols we support for right now
  482. * but we could remove other protocols if needed */
  483. cipso_v4_sock_delattr(sk);
  484. ret_val = 0;
  485. break;
  486. default:
  487. ret_val = -ENOENT;
  488. }
  489. break;
  490. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  491. case AF_INET6:
  492. /* since we don't support any IPv6 labeling protocols right
  493. * now we can optimize everything away until we do */
  494. ret_val = 0;
  495. break;
  496. #endif /* IPv6 */
  497. default:
  498. ret_val = 0;
  499. }
  500. conn_setattr_return:
  501. rcu_read_unlock();
  502. return ret_val;
  503. }
  504. /**
  505. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  506. * @skb: the packet
  507. * @family: protocol family
  508. * @secattr: the security attributes
  509. *
  510. * Description:
  511. * Attach the correct label to the given packet using the security attributes
  512. * specified in @secattr. Returns zero on success, negative values on failure.
  513. *
  514. */
  515. int netlbl_skbuff_setattr(struct sk_buff *skb,
  516. u16 family,
  517. const struct netlbl_lsm_secattr *secattr)
  518. {
  519. int ret_val;
  520. struct iphdr *hdr4;
  521. struct netlbl_domaddr4_map *af4_entry;
  522. rcu_read_lock();
  523. switch (family) {
  524. case AF_INET:
  525. hdr4 = ip_hdr(skb);
  526. af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
  527. hdr4->daddr);
  528. if (af4_entry == NULL) {
  529. ret_val = -ENOENT;
  530. goto skbuff_setattr_return;
  531. }
  532. switch (af4_entry->type) {
  533. case NETLBL_NLTYPE_CIPSOV4:
  534. ret_val = cipso_v4_skbuff_setattr(skb,
  535. af4_entry->type_def.cipsov4,
  536. secattr);
  537. break;
  538. case NETLBL_NLTYPE_UNLABELED:
  539. /* just delete the protocols we support for right now
  540. * but we could remove other protocols if needed */
  541. ret_val = cipso_v4_skbuff_delattr(skb);
  542. break;
  543. default:
  544. ret_val = -ENOENT;
  545. }
  546. break;
  547. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  548. case AF_INET6:
  549. /* since we don't support any IPv6 labeling protocols right
  550. * now we can optimize everything away until we do */
  551. ret_val = 0;
  552. break;
  553. #endif /* IPv6 */
  554. default:
  555. ret_val = 0;
  556. }
  557. skbuff_setattr_return:
  558. rcu_read_unlock();
  559. return ret_val;
  560. }
  561. /**
  562. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  563. * @skb: the packet
  564. * @family: protocol family
  565. * @secattr: the security attributes
  566. *
  567. * Description:
  568. * Examines the given packet to see if a recognized form of packet labeling
  569. * is present, if so it parses the packet label and returns the security
  570. * attributes in @secattr. Returns zero on success, negative values on
  571. * failure.
  572. *
  573. */
  574. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  575. u16 family,
  576. struct netlbl_lsm_secattr *secattr)
  577. {
  578. if (CIPSO_V4_OPTEXIST(skb) &&
  579. cipso_v4_skbuff_getattr(skb, secattr) == 0)
  580. return 0;
  581. return netlbl_unlabel_getattr(skb, family, secattr);
  582. }
  583. /**
  584. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  585. * @skb: the packet
  586. * @error: the error code
  587. * @gateway: true if host is acting as a gateway, false otherwise
  588. *
  589. * Description:
  590. * Deal with a LSM problem when handling the packet in @skb, typically this is
  591. * a permission denied problem (-EACCES). The correct action is determined
  592. * according to the packet's labeling protocol.
  593. *
  594. */
  595. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  596. {
  597. if (CIPSO_V4_OPTEXIST(skb))
  598. cipso_v4_error(skb, error, gateway);
  599. }
  600. /**
  601. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  602. *
  603. * Description:
  604. * For all of the NetLabel protocols that support some form of label mapping
  605. * cache, invalidate the cache. Returns zero on success, negative values on
  606. * error.
  607. *
  608. */
  609. void netlbl_cache_invalidate(void)
  610. {
  611. cipso_v4_cache_invalidate();
  612. }
  613. /**
  614. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  615. * @skb: the packet
  616. * @secattr: the packet's security attributes
  617. *
  618. * Description:
  619. * Add the LSM security attributes for the given packet to the underlying
  620. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  621. * values on error.
  622. *
  623. */
  624. int netlbl_cache_add(const struct sk_buff *skb,
  625. const struct netlbl_lsm_secattr *secattr)
  626. {
  627. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  628. return -ENOMSG;
  629. if (CIPSO_V4_OPTEXIST(skb))
  630. return cipso_v4_cache_add(skb, secattr);
  631. return -ENOMSG;
  632. }
  633. /*
  634. * Setup Functions
  635. */
  636. /**
  637. * netlbl_init - Initialize NetLabel
  638. *
  639. * Description:
  640. * Perform the required NetLabel initialization before first use.
  641. *
  642. */
  643. static int __init netlbl_init(void)
  644. {
  645. int ret_val;
  646. printk(KERN_INFO "NetLabel: Initializing\n");
  647. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  648. (1 << NETLBL_DOMHSH_BITSIZE));
  649. printk(KERN_INFO "NetLabel: protocols ="
  650. " UNLABELED"
  651. " CIPSOv4"
  652. "\n");
  653. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  654. if (ret_val != 0)
  655. goto init_failure;
  656. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  657. if (ret_val != 0)
  658. goto init_failure;
  659. ret_val = netlbl_netlink_init();
  660. if (ret_val != 0)
  661. goto init_failure;
  662. ret_val = netlbl_unlabel_defconf();
  663. if (ret_val != 0)
  664. goto init_failure;
  665. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  666. return 0;
  667. init_failure:
  668. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  669. }
  670. subsys_initcall(netlbl_init);