netlabel_kapi.c 38 KB

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