netlabel_kapi.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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@paul-moore.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, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/audit.h>
  32. #include <linux/in.h>
  33. #include <linux/in6.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <net/netlabel.h>
  37. #include <net/cipso_ipv4.h>
  38. #include <net/calipso.h>
  39. #include <asm/bug.h>
  40. #include <linux/atomic.h>
  41. #include "netlabel_domainhash.h"
  42. #include "netlabel_unlabeled.h"
  43. #include "netlabel_cipso_v4.h"
  44. #include "netlabel_calipso.h"
  45. #include "netlabel_user.h"
  46. #include "netlabel_mgmt.h"
  47. #include "netlabel_addrlist.h"
  48. /*
  49. * Configuration Functions
  50. */
  51. /**
  52. * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
  53. * @domain: the domain mapping to remove
  54. * @family: address family
  55. * @addr: IP address
  56. * @mask: IP address mask
  57. * @audit_info: NetLabel audit information
  58. *
  59. * Description:
  60. * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
  61. * default domain mapping to be removed. Returns zero on success, negative
  62. * values on failure.
  63. *
  64. */
  65. int netlbl_cfg_map_del(const char *domain,
  66. u16 family,
  67. const void *addr,
  68. const void *mask,
  69. struct netlbl_audit *audit_info)
  70. {
  71. if (addr == NULL && mask == NULL) {
  72. return netlbl_domhsh_remove(domain, family, audit_info);
  73. } else if (addr != NULL && mask != NULL) {
  74. switch (family) {
  75. case AF_INET:
  76. return netlbl_domhsh_remove_af4(domain, addr, mask,
  77. audit_info);
  78. default:
  79. return -EPFNOSUPPORT;
  80. }
  81. } else
  82. return -EINVAL;
  83. }
  84. /**
  85. * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
  86. * @domain: the domain mapping to add
  87. * @family: address family
  88. * @addr: IP address
  89. * @mask: IP address mask
  90. * @audit_info: NetLabel audit information
  91. *
  92. * Description:
  93. * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
  94. * causes a new default domain mapping to be added. Returns zero on success,
  95. * negative values on failure.
  96. *
  97. */
  98. int netlbl_cfg_unlbl_map_add(const char *domain,
  99. u16 family,
  100. const void *addr,
  101. const void *mask,
  102. struct netlbl_audit *audit_info)
  103. {
  104. int ret_val = -ENOMEM;
  105. struct netlbl_dom_map *entry;
  106. struct netlbl_domaddr_map *addrmap = NULL;
  107. struct netlbl_domaddr4_map *map4 = NULL;
  108. struct netlbl_domaddr6_map *map6 = NULL;
  109. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  110. if (entry == NULL)
  111. return -ENOMEM;
  112. if (domain != NULL) {
  113. entry->domain = kstrdup(domain, GFP_ATOMIC);
  114. if (entry->domain == NULL)
  115. goto cfg_unlbl_map_add_failure;
  116. }
  117. entry->family = family;
  118. if (addr == NULL && mask == NULL)
  119. entry->def.type = NETLBL_NLTYPE_UNLABELED;
  120. else if (addr != NULL && mask != NULL) {
  121. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  122. if (addrmap == NULL)
  123. goto cfg_unlbl_map_add_failure;
  124. INIT_LIST_HEAD(&addrmap->list4);
  125. INIT_LIST_HEAD(&addrmap->list6);
  126. switch (family) {
  127. case AF_INET: {
  128. const struct in_addr *addr4 = addr;
  129. const struct in_addr *mask4 = mask;
  130. map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
  131. if (map4 == NULL)
  132. goto cfg_unlbl_map_add_failure;
  133. map4->def.type = NETLBL_NLTYPE_UNLABELED;
  134. map4->list.addr = addr4->s_addr & mask4->s_addr;
  135. map4->list.mask = mask4->s_addr;
  136. map4->list.valid = 1;
  137. ret_val = netlbl_af4list_add(&map4->list,
  138. &addrmap->list4);
  139. if (ret_val != 0)
  140. goto cfg_unlbl_map_add_failure;
  141. break;
  142. }
  143. #if IS_ENABLED(CONFIG_IPV6)
  144. case AF_INET6: {
  145. const struct in6_addr *addr6 = addr;
  146. const struct in6_addr *mask6 = mask;
  147. map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
  148. if (map6 == NULL)
  149. goto cfg_unlbl_map_add_failure;
  150. map6->def.type = NETLBL_NLTYPE_UNLABELED;
  151. map6->list.addr = *addr6;
  152. map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
  153. map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
  154. map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
  155. map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
  156. map6->list.mask = *mask6;
  157. map6->list.valid = 1;
  158. ret_val = netlbl_af6list_add(&map6->list,
  159. &addrmap->list6);
  160. if (ret_val != 0)
  161. goto cfg_unlbl_map_add_failure;
  162. break;
  163. }
  164. #endif /* IPv6 */
  165. default:
  166. goto cfg_unlbl_map_add_failure;
  167. }
  168. entry->def.addrsel = addrmap;
  169. entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
  170. } else {
  171. ret_val = -EINVAL;
  172. goto cfg_unlbl_map_add_failure;
  173. }
  174. ret_val = netlbl_domhsh_add(entry, audit_info);
  175. if (ret_val != 0)
  176. goto cfg_unlbl_map_add_failure;
  177. return 0;
  178. cfg_unlbl_map_add_failure:
  179. kfree(entry->domain);
  180. kfree(entry);
  181. kfree(addrmap);
  182. kfree(map4);
  183. kfree(map6);
  184. return ret_val;
  185. }
  186. /**
  187. * netlbl_cfg_unlbl_static_add - Adds a new static label
  188. * @net: network namespace
  189. * @dev_name: interface name
  190. * @addr: IP address in network byte order (struct in[6]_addr)
  191. * @mask: address mask in network byte order (struct in[6]_addr)
  192. * @family: address family
  193. * @secid: LSM secid value for the entry
  194. * @audit_info: NetLabel audit information
  195. *
  196. * Description:
  197. * Adds a new NetLabel static label to be used when protocol provided labels
  198. * are not present on incoming traffic. If @dev_name is NULL then the default
  199. * interface will be used. Returns zero on success, negative values on failure.
  200. *
  201. */
  202. int netlbl_cfg_unlbl_static_add(struct net *net,
  203. const char *dev_name,
  204. const void *addr,
  205. const void *mask,
  206. u16 family,
  207. u32 secid,
  208. struct netlbl_audit *audit_info)
  209. {
  210. u32 addr_len;
  211. switch (family) {
  212. case AF_INET:
  213. addr_len = sizeof(struct in_addr);
  214. break;
  215. #if IS_ENABLED(CONFIG_IPV6)
  216. case AF_INET6:
  217. addr_len = sizeof(struct in6_addr);
  218. break;
  219. #endif /* IPv6 */
  220. default:
  221. return -EPFNOSUPPORT;
  222. }
  223. return netlbl_unlhsh_add(net,
  224. dev_name, addr, mask, addr_len,
  225. secid, audit_info);
  226. }
  227. /**
  228. * netlbl_cfg_unlbl_static_del - Removes an existing static label
  229. * @net: network namespace
  230. * @dev_name: interface name
  231. * @addr: IP address in network byte order (struct in[6]_addr)
  232. * @mask: address mask in network byte order (struct in[6]_addr)
  233. * @family: address family
  234. * @audit_info: NetLabel audit information
  235. *
  236. * Description:
  237. * Removes an existing NetLabel static label used when protocol provided labels
  238. * are not present on incoming traffic. If @dev_name is NULL then the default
  239. * interface will be used. Returns zero on success, negative values on failure.
  240. *
  241. */
  242. int netlbl_cfg_unlbl_static_del(struct net *net,
  243. const char *dev_name,
  244. const void *addr,
  245. const void *mask,
  246. u16 family,
  247. struct netlbl_audit *audit_info)
  248. {
  249. u32 addr_len;
  250. switch (family) {
  251. case AF_INET:
  252. addr_len = sizeof(struct in_addr);
  253. break;
  254. #if IS_ENABLED(CONFIG_IPV6)
  255. case AF_INET6:
  256. addr_len = sizeof(struct in6_addr);
  257. break;
  258. #endif /* IPv6 */
  259. default:
  260. return -EPFNOSUPPORT;
  261. }
  262. return netlbl_unlhsh_remove(net,
  263. dev_name, addr, mask, addr_len,
  264. audit_info);
  265. }
  266. /**
  267. * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
  268. * @doi_def: CIPSO DOI definition
  269. * @audit_info: NetLabel audit information
  270. *
  271. * Description:
  272. * Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on
  273. * success and negative values on failure.
  274. *
  275. */
  276. int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
  277. struct netlbl_audit *audit_info)
  278. {
  279. return cipso_v4_doi_add(doi_def, audit_info);
  280. }
  281. /**
  282. * netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition
  283. * @doi: CIPSO DOI
  284. * @audit_info: NetLabel audit information
  285. *
  286. * Description:
  287. * Remove an existing CIPSO DOI definition matching @doi. Returns zero on
  288. * success and negative values on failure.
  289. *
  290. */
  291. void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
  292. {
  293. cipso_v4_doi_remove(doi, audit_info);
  294. }
  295. /**
  296. * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
  297. * @doi: the CIPSO DOI
  298. * @domain: the domain mapping to add
  299. * @addr: IP address
  300. * @mask: IP address mask
  301. * @audit_info: NetLabel audit information
  302. *
  303. * Description:
  304. * Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel
  305. * subsystem. A @domain value of NULL adds a new default domain mapping.
  306. * Returns zero on success, negative values on failure.
  307. *
  308. */
  309. int netlbl_cfg_cipsov4_map_add(u32 doi,
  310. const char *domain,
  311. const struct in_addr *addr,
  312. const struct in_addr *mask,
  313. struct netlbl_audit *audit_info)
  314. {
  315. int ret_val = -ENOMEM;
  316. struct cipso_v4_doi *doi_def;
  317. struct netlbl_dom_map *entry;
  318. struct netlbl_domaddr_map *addrmap = NULL;
  319. struct netlbl_domaddr4_map *addrinfo = NULL;
  320. doi_def = cipso_v4_doi_getdef(doi);
  321. if (doi_def == NULL)
  322. return -ENOENT;
  323. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  324. if (entry == NULL)
  325. goto out_entry;
  326. entry->family = AF_INET;
  327. if (domain != NULL) {
  328. entry->domain = kstrdup(domain, GFP_ATOMIC);
  329. if (entry->domain == NULL)
  330. goto out_domain;
  331. }
  332. if (addr == NULL && mask == NULL) {
  333. entry->def.cipso = doi_def;
  334. entry->def.type = NETLBL_NLTYPE_CIPSOV4;
  335. } else if (addr != NULL && mask != NULL) {
  336. addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
  337. if (addrmap == NULL)
  338. goto out_addrmap;
  339. INIT_LIST_HEAD(&addrmap->list4);
  340. INIT_LIST_HEAD(&addrmap->list6);
  341. addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
  342. if (addrinfo == NULL)
  343. goto out_addrinfo;
  344. addrinfo->def.cipso = doi_def;
  345. addrinfo->def.type = NETLBL_NLTYPE_CIPSOV4;
  346. addrinfo->list.addr = addr->s_addr & mask->s_addr;
  347. addrinfo->list.mask = mask->s_addr;
  348. addrinfo->list.valid = 1;
  349. ret_val = netlbl_af4list_add(&addrinfo->list, &addrmap->list4);
  350. if (ret_val != 0)
  351. goto cfg_cipsov4_map_add_failure;
  352. entry->def.addrsel = addrmap;
  353. entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
  354. } else {
  355. ret_val = -EINVAL;
  356. goto out_addrmap;
  357. }
  358. ret_val = netlbl_domhsh_add(entry, audit_info);
  359. if (ret_val != 0)
  360. goto cfg_cipsov4_map_add_failure;
  361. return 0;
  362. cfg_cipsov4_map_add_failure:
  363. kfree(addrinfo);
  364. out_addrinfo:
  365. kfree(addrmap);
  366. out_addrmap:
  367. kfree(entry->domain);
  368. out_domain:
  369. kfree(entry);
  370. out_entry:
  371. cipso_v4_doi_putdef(doi_def);
  372. return ret_val;
  373. }
  374. /*
  375. * Security Attribute Functions
  376. */
  377. #define _CM_F_NONE 0x00000000
  378. #define _CM_F_ALLOC 0x00000001
  379. #define _CM_F_WALK 0x00000002
  380. /**
  381. * _netlbl_catmap_getnode - Get a individual node from a catmap
  382. * @catmap: pointer to the category bitmap
  383. * @offset: the requested offset
  384. * @cm_flags: catmap flags, see _CM_F_*
  385. * @gfp_flags: memory allocation flags
  386. *
  387. * Description:
  388. * Iterate through the catmap looking for the node associated with @offset.
  389. * If the _CM_F_ALLOC flag is set in @cm_flags and there is no associated node,
  390. * one will be created and inserted into the catmap. If the _CM_F_WALK flag is
  391. * set in @cm_flags and there is no associated node, the next highest node will
  392. * be returned. Returns a pointer to the node on success, NULL on failure.
  393. *
  394. */
  395. static struct netlbl_lsm_catmap *_netlbl_catmap_getnode(
  396. struct netlbl_lsm_catmap **catmap,
  397. u32 offset,
  398. unsigned int cm_flags,
  399. gfp_t gfp_flags)
  400. {
  401. struct netlbl_lsm_catmap *iter = *catmap;
  402. struct netlbl_lsm_catmap *prev = NULL;
  403. if (iter == NULL)
  404. goto catmap_getnode_alloc;
  405. if (offset < iter->startbit)
  406. goto catmap_getnode_walk;
  407. while (iter && offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
  408. prev = iter;
  409. iter = iter->next;
  410. }
  411. if (iter == NULL || offset < iter->startbit)
  412. goto catmap_getnode_walk;
  413. return iter;
  414. catmap_getnode_walk:
  415. if (cm_flags & _CM_F_WALK)
  416. return iter;
  417. catmap_getnode_alloc:
  418. if (!(cm_flags & _CM_F_ALLOC))
  419. return NULL;
  420. iter = netlbl_catmap_alloc(gfp_flags);
  421. if (iter == NULL)
  422. return NULL;
  423. iter->startbit = offset & ~(NETLBL_CATMAP_SIZE - 1);
  424. if (prev == NULL) {
  425. iter->next = *catmap;
  426. *catmap = iter;
  427. } else {
  428. iter->next = prev->next;
  429. prev->next = iter;
  430. }
  431. return iter;
  432. }
  433. /**
  434. * netlbl_catmap_walk - Walk a LSM secattr catmap looking for a bit
  435. * @catmap: the category bitmap
  436. * @offset: the offset to start searching at, in bits
  437. *
  438. * Description:
  439. * This function walks a LSM secattr category bitmap starting at @offset and
  440. * returns the spot of the first set bit or -ENOENT if no bits are set.
  441. *
  442. */
  443. int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset)
  444. {
  445. struct netlbl_lsm_catmap *iter = catmap;
  446. u32 idx;
  447. u32 bit;
  448. NETLBL_CATMAP_MAPTYPE bitmap;
  449. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  450. if (iter == NULL)
  451. return -ENOENT;
  452. if (offset > iter->startbit) {
  453. offset -= iter->startbit;
  454. idx = offset / NETLBL_CATMAP_MAPSIZE;
  455. bit = offset % NETLBL_CATMAP_MAPSIZE;
  456. } else {
  457. idx = 0;
  458. bit = 0;
  459. }
  460. bitmap = iter->bitmap[idx] >> bit;
  461. for (;;) {
  462. if (bitmap != 0) {
  463. while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
  464. bitmap >>= 1;
  465. bit++;
  466. }
  467. return iter->startbit +
  468. (NETLBL_CATMAP_MAPSIZE * idx) + bit;
  469. }
  470. if (++idx >= NETLBL_CATMAP_MAPCNT) {
  471. if (iter->next != NULL) {
  472. iter = iter->next;
  473. idx = 0;
  474. } else
  475. return -ENOENT;
  476. }
  477. bitmap = iter->bitmap[idx];
  478. bit = 0;
  479. }
  480. return -ENOENT;
  481. }
  482. EXPORT_SYMBOL(netlbl_catmap_walk);
  483. /**
  484. * netlbl_catmap_walkrng - Find the end of a string of set bits
  485. * @catmap: the category bitmap
  486. * @offset: the offset to start searching at, in bits
  487. *
  488. * Description:
  489. * This function walks a LSM secattr category bitmap starting at @offset and
  490. * returns the spot of the first cleared bit or -ENOENT if the offset is past
  491. * the end of the bitmap.
  492. *
  493. */
  494. int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset)
  495. {
  496. struct netlbl_lsm_catmap *iter;
  497. struct netlbl_lsm_catmap *prev = NULL;
  498. u32 idx;
  499. u32 bit;
  500. NETLBL_CATMAP_MAPTYPE bitmask;
  501. NETLBL_CATMAP_MAPTYPE bitmap;
  502. iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
  503. if (iter == NULL)
  504. return -ENOENT;
  505. if (offset > iter->startbit) {
  506. offset -= iter->startbit;
  507. idx = offset / NETLBL_CATMAP_MAPSIZE;
  508. bit = offset % NETLBL_CATMAP_MAPSIZE;
  509. } else {
  510. idx = 0;
  511. bit = 0;
  512. }
  513. bitmask = NETLBL_CATMAP_BIT << bit;
  514. for (;;) {
  515. bitmap = iter->bitmap[idx];
  516. while (bitmask != 0 && (bitmap & bitmask) != 0) {
  517. bitmask <<= 1;
  518. bit++;
  519. }
  520. if (prev && idx == 0 && bit == 0)
  521. return prev->startbit + NETLBL_CATMAP_SIZE - 1;
  522. else if (bitmask != 0)
  523. return iter->startbit +
  524. (NETLBL_CATMAP_MAPSIZE * idx) + bit - 1;
  525. else if (++idx >= NETLBL_CATMAP_MAPCNT) {
  526. if (iter->next == NULL)
  527. return iter->startbit + NETLBL_CATMAP_SIZE - 1;
  528. prev = iter;
  529. iter = iter->next;
  530. idx = 0;
  531. }
  532. bitmask = NETLBL_CATMAP_BIT;
  533. bit = 0;
  534. }
  535. return -ENOENT;
  536. }
  537. /**
  538. * netlbl_catmap_getlong - Export an unsigned long bitmap
  539. * @catmap: pointer to the category bitmap
  540. * @offset: pointer to the requested offset
  541. * @bitmap: the exported bitmap
  542. *
  543. * Description:
  544. * Export a bitmap with an offset greater than or equal to @offset and return
  545. * it in @bitmap. The @offset must be aligned to an unsigned long and will be
  546. * updated on return if different from what was requested; if the catmap is
  547. * empty at the requested offset and beyond, the @offset is set to (u32)-1.
  548. * Returns zero on sucess, negative values on failure.
  549. *
  550. */
  551. int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
  552. u32 *offset,
  553. unsigned long *bitmap)
  554. {
  555. struct netlbl_lsm_catmap *iter;
  556. u32 off = *offset;
  557. u32 idx;
  558. /* only allow aligned offsets */
  559. if ((off & (BITS_PER_LONG - 1)) != 0)
  560. return -EINVAL;
  561. if (off < catmap->startbit) {
  562. off = catmap->startbit;
  563. *offset = off;
  564. }
  565. iter = _netlbl_catmap_getnode(&catmap, off, _CM_F_WALK, 0);
  566. if (iter == NULL) {
  567. *offset = (u32)-1;
  568. return 0;
  569. }
  570. if (off < iter->startbit) {
  571. *offset = iter->startbit;
  572. off = 0;
  573. } else
  574. off -= iter->startbit;
  575. idx = off / NETLBL_CATMAP_MAPSIZE;
  576. *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_MAPSIZE);
  577. return 0;
  578. }
  579. /**
  580. * netlbl_catmap_setbit - Set a bit in a LSM secattr catmap
  581. * @catmap: pointer to the category bitmap
  582. * @bit: the bit to set
  583. * @flags: memory allocation flags
  584. *
  585. * Description:
  586. * Set the bit specified by @bit in @catmap. Returns zero on success,
  587. * negative values on failure.
  588. *
  589. */
  590. int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
  591. u32 bit,
  592. gfp_t flags)
  593. {
  594. struct netlbl_lsm_catmap *iter;
  595. u32 idx;
  596. iter = _netlbl_catmap_getnode(catmap, bit, _CM_F_ALLOC, flags);
  597. if (iter == NULL)
  598. return -ENOMEM;
  599. bit -= iter->startbit;
  600. idx = bit / NETLBL_CATMAP_MAPSIZE;
  601. iter->bitmap[idx] |= NETLBL_CATMAP_BIT << (bit % NETLBL_CATMAP_MAPSIZE);
  602. return 0;
  603. }
  604. EXPORT_SYMBOL(netlbl_catmap_setbit);
  605. /**
  606. * netlbl_catmap_setrng - Set a range of bits in a LSM secattr catmap
  607. * @catmap: pointer to the category bitmap
  608. * @start: the starting bit
  609. * @end: the last bit in the string
  610. * @flags: memory allocation flags
  611. *
  612. * Description:
  613. * Set a range of bits, starting at @start and ending with @end. Returns zero
  614. * on success, negative values on failure.
  615. *
  616. */
  617. int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
  618. u32 start,
  619. u32 end,
  620. gfp_t flags)
  621. {
  622. int rc = 0;
  623. u32 spot = start;
  624. while (rc == 0 && spot <= end) {
  625. if (((spot & (BITS_PER_LONG - 1)) == 0) &&
  626. ((end - spot) > BITS_PER_LONG)) {
  627. rc = netlbl_catmap_setlong(catmap,
  628. spot,
  629. (unsigned long)-1,
  630. flags);
  631. spot += BITS_PER_LONG;
  632. } else
  633. rc = netlbl_catmap_setbit(catmap, spot++, flags);
  634. }
  635. return rc;
  636. }
  637. /**
  638. * netlbl_catmap_setlong - Import an unsigned long bitmap
  639. * @catmap: pointer to the category bitmap
  640. * @offset: offset to the start of the imported bitmap
  641. * @bitmap: the bitmap to import
  642. * @flags: memory allocation flags
  643. *
  644. * Description:
  645. * Import the bitmap specified in @bitmap into @catmap, using the offset
  646. * in @offset. The offset must be aligned to an unsigned long. Returns zero
  647. * on success, negative values on failure.
  648. *
  649. */
  650. int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
  651. u32 offset,
  652. unsigned long bitmap,
  653. gfp_t flags)
  654. {
  655. struct netlbl_lsm_catmap *iter;
  656. u32 idx;
  657. /* only allow aligned offsets */
  658. if ((offset & (BITS_PER_LONG - 1)) != 0)
  659. return -EINVAL;
  660. iter = _netlbl_catmap_getnode(catmap, offset, _CM_F_ALLOC, flags);
  661. if (iter == NULL)
  662. return -ENOMEM;
  663. offset -= iter->startbit;
  664. idx = offset / NETLBL_CATMAP_MAPSIZE;
  665. iter->bitmap[idx] |= bitmap << (offset % NETLBL_CATMAP_MAPSIZE);
  666. return 0;
  667. }
  668. /* Bitmap functions
  669. */
  670. /**
  671. * netlbl_bitmap_walk - Walk a bitmap looking for a bit
  672. * @bitmap: the bitmap
  673. * @bitmap_len: length in bits
  674. * @offset: starting offset
  675. * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
  676. *
  677. * Description:
  678. * Starting at @offset, walk the bitmap from left to right until either the
  679. * desired bit is found or we reach the end. Return the bit offset, -1 if
  680. * not found, or -2 if error.
  681. */
  682. int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
  683. u32 offset, u8 state)
  684. {
  685. u32 bit_spot;
  686. u32 byte_offset;
  687. unsigned char bitmask;
  688. unsigned char byte;
  689. byte_offset = offset / 8;
  690. byte = bitmap[byte_offset];
  691. bit_spot = offset;
  692. bitmask = 0x80 >> (offset % 8);
  693. while (bit_spot < bitmap_len) {
  694. if ((state && (byte & bitmask) == bitmask) ||
  695. (state == 0 && (byte & bitmask) == 0))
  696. return bit_spot;
  697. bit_spot++;
  698. bitmask >>= 1;
  699. if (bitmask == 0) {
  700. byte = bitmap[++byte_offset];
  701. bitmask = 0x80;
  702. }
  703. }
  704. return -1;
  705. }
  706. EXPORT_SYMBOL(netlbl_bitmap_walk);
  707. /**
  708. * netlbl_bitmap_setbit - Sets a single bit in a bitmap
  709. * @bitmap: the bitmap
  710. * @bit: the bit
  711. * @state: if non-zero, set the bit (1) else clear the bit (0)
  712. *
  713. * Description:
  714. * Set a single bit in the bitmask. Returns zero on success, negative values
  715. * on error.
  716. */
  717. void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state)
  718. {
  719. u32 byte_spot;
  720. u8 bitmask;
  721. /* gcc always rounds to zero when doing integer division */
  722. byte_spot = bit / 8;
  723. bitmask = 0x80 >> (bit % 8);
  724. if (state)
  725. bitmap[byte_spot] |= bitmask;
  726. else
  727. bitmap[byte_spot] &= ~bitmask;
  728. }
  729. EXPORT_SYMBOL(netlbl_bitmap_setbit);
  730. /*
  731. * LSM Functions
  732. */
  733. /**
  734. * netlbl_enabled - Determine if the NetLabel subsystem is enabled
  735. *
  736. * Description:
  737. * The LSM can use this function to determine if it should use NetLabel
  738. * security attributes in it's enforcement mechanism. Currently, NetLabel is
  739. * considered to be enabled when it's configuration contains a valid setup for
  740. * at least one labeled protocol (i.e. NetLabel can understand incoming
  741. * labeled packets of at least one type); otherwise NetLabel is considered to
  742. * be disabled.
  743. *
  744. */
  745. int netlbl_enabled(void)
  746. {
  747. /* At some point we probably want to expose this mechanism to the user
  748. * as well so that admins can toggle NetLabel regardless of the
  749. * configuration */
  750. return (atomic_read(&netlabel_mgmt_protocount) > 0);
  751. }
  752. /**
  753. * netlbl_sock_setattr - Label a socket using the correct protocol
  754. * @sk: the socket to label
  755. * @family: protocol family
  756. * @secattr: the security attributes
  757. *
  758. * Description:
  759. * Attach the correct label to the given socket using the security attributes
  760. * specified in @secattr. This function requires exclusive access to @sk,
  761. * which means it either needs to be in the process of being created or locked.
  762. * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
  763. * network address selectors (can't blindly label the socket), and negative
  764. * values on all other failures.
  765. *
  766. */
  767. int netlbl_sock_setattr(struct sock *sk,
  768. u16 family,
  769. const struct netlbl_lsm_secattr *secattr)
  770. {
  771. int ret_val;
  772. struct netlbl_dom_map *dom_entry;
  773. rcu_read_lock();
  774. dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
  775. if (dom_entry == NULL) {
  776. ret_val = -ENOENT;
  777. goto socket_setattr_return;
  778. }
  779. switch (family) {
  780. case AF_INET:
  781. switch (dom_entry->def.type) {
  782. case NETLBL_NLTYPE_ADDRSELECT:
  783. ret_val = -EDESTADDRREQ;
  784. break;
  785. case NETLBL_NLTYPE_CIPSOV4:
  786. ret_val = cipso_v4_sock_setattr(sk,
  787. dom_entry->def.cipso,
  788. secattr);
  789. break;
  790. case NETLBL_NLTYPE_UNLABELED:
  791. ret_val = 0;
  792. break;
  793. default:
  794. ret_val = -ENOENT;
  795. }
  796. break;
  797. #if IS_ENABLED(CONFIG_IPV6)
  798. case AF_INET6:
  799. switch (dom_entry->def.type) {
  800. case NETLBL_NLTYPE_ADDRSELECT:
  801. ret_val = -EDESTADDRREQ;
  802. break;
  803. case NETLBL_NLTYPE_CALIPSO:
  804. ret_val = calipso_sock_setattr(sk,
  805. dom_entry->def.calipso,
  806. secattr);
  807. break;
  808. case NETLBL_NLTYPE_UNLABELED:
  809. ret_val = 0;
  810. break;
  811. default:
  812. ret_val = -ENOENT;
  813. }
  814. break;
  815. #endif /* IPv6 */
  816. default:
  817. ret_val = -EPROTONOSUPPORT;
  818. }
  819. socket_setattr_return:
  820. rcu_read_unlock();
  821. return ret_val;
  822. }
  823. /**
  824. * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
  825. * @sk: the socket
  826. *
  827. * Description:
  828. * Remove all the NetLabel labeling from @sk. The caller is responsible for
  829. * ensuring that @sk is locked.
  830. *
  831. */
  832. void netlbl_sock_delattr(struct sock *sk)
  833. {
  834. switch (sk->sk_family) {
  835. case AF_INET:
  836. cipso_v4_sock_delattr(sk);
  837. break;
  838. #if IS_ENABLED(CONFIG_IPV6)
  839. case AF_INET6:
  840. calipso_sock_delattr(sk);
  841. break;
  842. #endif /* IPv6 */
  843. }
  844. }
  845. /**
  846. * netlbl_sock_getattr - Determine the security attributes of a sock
  847. * @sk: the sock
  848. * @secattr: the security attributes
  849. *
  850. * Description:
  851. * Examines the given sock to see if any NetLabel style labeling has been
  852. * applied to the sock, if so it parses the socket label and returns the
  853. * security attributes in @secattr. Returns zero on success, negative values
  854. * on failure.
  855. *
  856. */
  857. int netlbl_sock_getattr(struct sock *sk,
  858. struct netlbl_lsm_secattr *secattr)
  859. {
  860. int ret_val;
  861. switch (sk->sk_family) {
  862. case AF_INET:
  863. ret_val = cipso_v4_sock_getattr(sk, secattr);
  864. break;
  865. #if IS_ENABLED(CONFIG_IPV6)
  866. case AF_INET6:
  867. ret_val = calipso_sock_getattr(sk, secattr);
  868. break;
  869. #endif /* IPv6 */
  870. default:
  871. ret_val = -EPROTONOSUPPORT;
  872. }
  873. return ret_val;
  874. }
  875. /**
  876. * netlbl_conn_setattr - Label a connected socket using the correct protocol
  877. * @sk: the socket to label
  878. * @addr: the destination address
  879. * @secattr: the security attributes
  880. *
  881. * Description:
  882. * Attach the correct label to the given connected socket using the security
  883. * attributes specified in @secattr. The caller is responsible for ensuring
  884. * that @sk is locked. Returns zero on success, negative values on failure.
  885. *
  886. */
  887. int netlbl_conn_setattr(struct sock *sk,
  888. struct sockaddr *addr,
  889. const struct netlbl_lsm_secattr *secattr)
  890. {
  891. int ret_val;
  892. struct sockaddr_in *addr4;
  893. #if IS_ENABLED(CONFIG_IPV6)
  894. struct sockaddr_in6 *addr6;
  895. #endif
  896. struct netlbl_dommap_def *entry;
  897. rcu_read_lock();
  898. switch (addr->sa_family) {
  899. case AF_INET:
  900. addr4 = (struct sockaddr_in *)addr;
  901. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  902. addr4->sin_addr.s_addr);
  903. if (entry == NULL) {
  904. ret_val = -ENOENT;
  905. goto conn_setattr_return;
  906. }
  907. switch (entry->type) {
  908. case NETLBL_NLTYPE_CIPSOV4:
  909. ret_val = cipso_v4_sock_setattr(sk,
  910. entry->cipso, secattr);
  911. break;
  912. case NETLBL_NLTYPE_UNLABELED:
  913. /* just delete the protocols we support for right now
  914. * but we could remove other protocols if needed */
  915. netlbl_sock_delattr(sk);
  916. ret_val = 0;
  917. break;
  918. default:
  919. ret_val = -ENOENT;
  920. }
  921. break;
  922. #if IS_ENABLED(CONFIG_IPV6)
  923. case AF_INET6:
  924. addr6 = (struct sockaddr_in6 *)addr;
  925. entry = netlbl_domhsh_getentry_af6(secattr->domain,
  926. &addr6->sin6_addr);
  927. if (entry == NULL) {
  928. ret_val = -ENOENT;
  929. goto conn_setattr_return;
  930. }
  931. switch (entry->type) {
  932. case NETLBL_NLTYPE_CALIPSO:
  933. ret_val = calipso_sock_setattr(sk,
  934. entry->calipso, secattr);
  935. break;
  936. case NETLBL_NLTYPE_UNLABELED:
  937. /* just delete the protocols we support for right now
  938. * but we could remove other protocols if needed */
  939. netlbl_sock_delattr(sk);
  940. ret_val = 0;
  941. break;
  942. default:
  943. ret_val = -ENOENT;
  944. }
  945. break;
  946. #endif /* IPv6 */
  947. default:
  948. ret_val = -EPROTONOSUPPORT;
  949. }
  950. conn_setattr_return:
  951. rcu_read_unlock();
  952. return ret_val;
  953. }
  954. /**
  955. * netlbl_req_setattr - Label a request socket using the correct protocol
  956. * @req: the request socket to label
  957. * @secattr: the security attributes
  958. *
  959. * Description:
  960. * Attach the correct label to the given socket using the security attributes
  961. * specified in @secattr. Returns zero on success, negative values on failure.
  962. *
  963. */
  964. int netlbl_req_setattr(struct request_sock *req,
  965. const struct netlbl_lsm_secattr *secattr)
  966. {
  967. int ret_val;
  968. struct netlbl_dommap_def *entry;
  969. rcu_read_lock();
  970. switch (req->rsk_ops->family) {
  971. case AF_INET:
  972. entry = netlbl_domhsh_getentry_af4(secattr->domain,
  973. inet_rsk(req)->ir_rmt_addr);
  974. if (entry == NULL) {
  975. ret_val = -ENOENT;
  976. goto req_setattr_return;
  977. }
  978. switch (entry->type) {
  979. case NETLBL_NLTYPE_CIPSOV4:
  980. ret_val = cipso_v4_req_setattr(req,
  981. entry->cipso, secattr);
  982. break;
  983. case NETLBL_NLTYPE_UNLABELED:
  984. /* just delete the protocols we support for right now
  985. * but we could remove other protocols if needed */
  986. cipso_v4_req_delattr(req);
  987. ret_val = 0;
  988. break;
  989. default:
  990. ret_val = -ENOENT;
  991. }
  992. break;
  993. #if IS_ENABLED(CONFIG_IPV6)
  994. case AF_INET6:
  995. /* since we don't support any IPv6 labeling protocols right
  996. * now we can optimize everything away until we do */
  997. ret_val = 0;
  998. break;
  999. #endif /* IPv6 */
  1000. default:
  1001. ret_val = -EPROTONOSUPPORT;
  1002. }
  1003. req_setattr_return:
  1004. rcu_read_unlock();
  1005. return ret_val;
  1006. }
  1007. /**
  1008. * netlbl_req_delattr - Delete all the NetLabel labels on a socket
  1009. * @req: the socket
  1010. *
  1011. * Description:
  1012. * Remove all the NetLabel labeling from @req.
  1013. *
  1014. */
  1015. void netlbl_req_delattr(struct request_sock *req)
  1016. {
  1017. switch (req->rsk_ops->family) {
  1018. case AF_INET:
  1019. cipso_v4_req_delattr(req);
  1020. break;
  1021. }
  1022. }
  1023. /**
  1024. * netlbl_skbuff_setattr - Label a packet using the correct protocol
  1025. * @skb: the packet
  1026. * @family: protocol family
  1027. * @secattr: the security attributes
  1028. *
  1029. * Description:
  1030. * Attach the correct label to the given packet using the security attributes
  1031. * specified in @secattr. Returns zero on success, negative values on failure.
  1032. *
  1033. */
  1034. int netlbl_skbuff_setattr(struct sk_buff *skb,
  1035. u16 family,
  1036. const struct netlbl_lsm_secattr *secattr)
  1037. {
  1038. int ret_val;
  1039. struct iphdr *hdr4;
  1040. struct netlbl_dommap_def *entry;
  1041. rcu_read_lock();
  1042. switch (family) {
  1043. case AF_INET:
  1044. hdr4 = ip_hdr(skb);
  1045. entry = netlbl_domhsh_getentry_af4(secattr->domain,hdr4->daddr);
  1046. if (entry == NULL) {
  1047. ret_val = -ENOENT;
  1048. goto skbuff_setattr_return;
  1049. }
  1050. switch (entry->type) {
  1051. case NETLBL_NLTYPE_CIPSOV4:
  1052. ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
  1053. secattr);
  1054. break;
  1055. case NETLBL_NLTYPE_UNLABELED:
  1056. /* just delete the protocols we support for right now
  1057. * but we could remove other protocols if needed */
  1058. ret_val = cipso_v4_skbuff_delattr(skb);
  1059. break;
  1060. default:
  1061. ret_val = -ENOENT;
  1062. }
  1063. break;
  1064. #if IS_ENABLED(CONFIG_IPV6)
  1065. case AF_INET6:
  1066. /* since we don't support any IPv6 labeling protocols right
  1067. * now we can optimize everything away until we do */
  1068. ret_val = 0;
  1069. break;
  1070. #endif /* IPv6 */
  1071. default:
  1072. ret_val = -EPROTONOSUPPORT;
  1073. }
  1074. skbuff_setattr_return:
  1075. rcu_read_unlock();
  1076. return ret_val;
  1077. }
  1078. /**
  1079. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  1080. * @skb: the packet
  1081. * @family: protocol family
  1082. * @secattr: the security attributes
  1083. *
  1084. * Description:
  1085. * Examines the given packet to see if a recognized form of packet labeling
  1086. * is present, if so it parses the packet label and returns the security
  1087. * attributes in @secattr. Returns zero on success, negative values on
  1088. * failure.
  1089. *
  1090. */
  1091. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  1092. u16 family,
  1093. struct netlbl_lsm_secattr *secattr)
  1094. {
  1095. unsigned char *ptr;
  1096. switch (family) {
  1097. case AF_INET:
  1098. ptr = cipso_v4_optptr(skb);
  1099. if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
  1100. return 0;
  1101. break;
  1102. #if IS_ENABLED(CONFIG_IPV6)
  1103. case AF_INET6:
  1104. break;
  1105. #endif /* IPv6 */
  1106. }
  1107. return netlbl_unlabel_getattr(skb, family, secattr);
  1108. }
  1109. /**
  1110. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  1111. * @skb: the packet
  1112. * @error: the error code
  1113. * @gateway: true if host is acting as a gateway, false otherwise
  1114. *
  1115. * Description:
  1116. * Deal with a LSM problem when handling the packet in @skb, typically this is
  1117. * a permission denied problem (-EACCES). The correct action is determined
  1118. * according to the packet's labeling protocol.
  1119. *
  1120. */
  1121. void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
  1122. {
  1123. if (cipso_v4_optptr(skb))
  1124. cipso_v4_error(skb, error, gateway);
  1125. }
  1126. /**
  1127. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  1128. *
  1129. * Description:
  1130. * For all of the NetLabel protocols that support some form of label mapping
  1131. * cache, invalidate the cache. Returns zero on success, negative values on
  1132. * error.
  1133. *
  1134. */
  1135. void netlbl_cache_invalidate(void)
  1136. {
  1137. cipso_v4_cache_invalidate();
  1138. }
  1139. /**
  1140. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  1141. * @skb: the packet
  1142. * @secattr: the packet's security attributes
  1143. *
  1144. * Description:
  1145. * Add the LSM security attributes for the given packet to the underlying
  1146. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  1147. * values on error.
  1148. *
  1149. */
  1150. int netlbl_cache_add(const struct sk_buff *skb,
  1151. const struct netlbl_lsm_secattr *secattr)
  1152. {
  1153. unsigned char *ptr;
  1154. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  1155. return -ENOMSG;
  1156. ptr = cipso_v4_optptr(skb);
  1157. if (ptr)
  1158. return cipso_v4_cache_add(ptr, secattr);
  1159. return -ENOMSG;
  1160. }
  1161. /*
  1162. * Protocol Engine Functions
  1163. */
  1164. /**
  1165. * netlbl_audit_start - Start an audit message
  1166. * @type: audit message type
  1167. * @audit_info: NetLabel audit information
  1168. *
  1169. * Description:
  1170. * Start an audit message using the type specified in @type and fill the audit
  1171. * message with some fields common to all NetLabel audit messages. This
  1172. * function should only be used by protocol engines, not LSMs. Returns a
  1173. * pointer to the audit buffer on success, NULL on failure.
  1174. *
  1175. */
  1176. struct audit_buffer *netlbl_audit_start(int type,
  1177. struct netlbl_audit *audit_info)
  1178. {
  1179. return netlbl_audit_start_common(type, audit_info);
  1180. }
  1181. EXPORT_SYMBOL(netlbl_audit_start);
  1182. /*
  1183. * Setup Functions
  1184. */
  1185. /**
  1186. * netlbl_init - Initialize NetLabel
  1187. *
  1188. * Description:
  1189. * Perform the required NetLabel initialization before first use.
  1190. *
  1191. */
  1192. static int __init netlbl_init(void)
  1193. {
  1194. int ret_val;
  1195. printk(KERN_INFO "NetLabel: Initializing\n");
  1196. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  1197. (1 << NETLBL_DOMHSH_BITSIZE));
  1198. printk(KERN_INFO "NetLabel: protocols ="
  1199. " UNLABELED"
  1200. " CIPSOv4"
  1201. "\n");
  1202. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  1203. if (ret_val != 0)
  1204. goto init_failure;
  1205. ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
  1206. if (ret_val != 0)
  1207. goto init_failure;
  1208. ret_val = netlbl_netlink_init();
  1209. if (ret_val != 0)
  1210. goto init_failure;
  1211. ret_val = netlbl_unlabel_defconf();
  1212. if (ret_val != 0)
  1213. goto init_failure;
  1214. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  1215. return 0;
  1216. init_failure:
  1217. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  1218. }
  1219. subsys_initcall(netlbl_init);