netlabel_cipso_v4.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * NetLabel CIPSO/IPv4 Support
  3. *
  4. * This file defines the CIPSO/IPv4 functions for the NetLabel system. The
  5. * NetLabel system manages static and dynamic label mappings for network
  6. * protocols such as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  22. * the GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/socket.h>
  31. #include <linux/string.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/audit.h>
  34. #include <net/sock.h>
  35. #include <net/netlink.h>
  36. #include <net/genetlink.h>
  37. #include <net/netlabel.h>
  38. #include <net/cipso_ipv4.h>
  39. #include <asm/atomic.h>
  40. #include "netlabel_user.h"
  41. #include "netlabel_cipso_v4.h"
  42. #include "netlabel_mgmt.h"
  43. #include "netlabel_domainhash.h"
  44. /* Argument struct for cipso_v4_doi_walk() */
  45. struct netlbl_cipsov4_doiwalk_arg {
  46. struct netlink_callback *nl_cb;
  47. struct sk_buff *skb;
  48. u32 seq;
  49. };
  50. /* Argument struct for netlbl_domhsh_walk() */
  51. struct netlbl_domhsh_walk_arg {
  52. struct netlbl_audit *audit_info;
  53. u32 doi;
  54. };
  55. /* NetLabel Generic NETLINK CIPSOv4 family */
  56. static struct genl_family netlbl_cipsov4_gnl_family = {
  57. .id = GENL_ID_GENERATE,
  58. .hdrsize = 0,
  59. .name = NETLBL_NLTYPE_CIPSOV4_NAME,
  60. .version = NETLBL_PROTO_VERSION,
  61. .maxattr = NLBL_CIPSOV4_A_MAX,
  62. };
  63. /* NetLabel Netlink attribute policy */
  64. static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
  65. [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
  66. [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
  67. [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
  68. [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
  69. [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
  70. [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
  71. [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
  72. [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
  73. [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
  74. [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
  75. [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
  76. [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
  77. };
  78. /*
  79. * Helper Functions
  80. */
  81. /**
  82. * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
  83. * @info: the Generic NETLINK info block
  84. * @doi_def: the CIPSO V4 DOI definition
  85. *
  86. * Description:
  87. * Parse the common sections of a ADD message and fill in the related values
  88. * in @doi_def. Returns zero on success, negative values on failure.
  89. *
  90. */
  91. static int netlbl_cipsov4_add_common(struct genl_info *info,
  92. struct cipso_v4_doi *doi_def)
  93. {
  94. struct nlattr *nla;
  95. int nla_rem;
  96. u32 iter = 0;
  97. doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  98. if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
  99. NLBL_CIPSOV4_A_MAX,
  100. netlbl_cipsov4_genl_policy) != 0)
  101. return -EINVAL;
  102. nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
  103. if (nla_type(nla) == NLBL_CIPSOV4_A_TAG) {
  104. if (iter >= CIPSO_V4_TAG_MAXCNT)
  105. return -EINVAL;
  106. doi_def->tags[iter++] = nla_get_u8(nla);
  107. }
  108. while (iter < CIPSO_V4_TAG_MAXCNT)
  109. doi_def->tags[iter++] = CIPSO_V4_TAG_INVALID;
  110. return 0;
  111. }
  112. /*
  113. * NetLabel Command Handlers
  114. */
  115. /**
  116. * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
  117. * @info: the Generic NETLINK info block
  118. *
  119. * Description:
  120. * Create a new CIPSO_V4_MAP_TRANS DOI definition based on the given ADD
  121. * message and add it to the CIPSO V4 engine. Return zero on success and
  122. * non-zero on error.
  123. *
  124. */
  125. static int netlbl_cipsov4_add_std(struct genl_info *info)
  126. {
  127. int ret_val = -EINVAL;
  128. struct cipso_v4_doi *doi_def = NULL;
  129. struct nlattr *nla_a;
  130. struct nlattr *nla_b;
  131. int nla_a_rem;
  132. int nla_b_rem;
  133. u32 iter;
  134. if (!info->attrs[NLBL_CIPSOV4_A_TAGLST] ||
  135. !info->attrs[NLBL_CIPSOV4_A_MLSLVLLST])
  136. return -EINVAL;
  137. if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
  138. NLBL_CIPSOV4_A_MAX,
  139. netlbl_cipsov4_genl_policy) != 0)
  140. return -EINVAL;
  141. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  142. if (doi_def == NULL)
  143. return -ENOMEM;
  144. doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
  145. if (doi_def->map.std == NULL) {
  146. ret_val = -ENOMEM;
  147. goto add_std_failure;
  148. }
  149. doi_def->type = CIPSO_V4_MAP_TRANS;
  150. ret_val = netlbl_cipsov4_add_common(info, doi_def);
  151. if (ret_val != 0)
  152. goto add_std_failure;
  153. ret_val = -EINVAL;
  154. nla_for_each_nested(nla_a,
  155. info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
  156. nla_a_rem)
  157. if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
  158. if (nla_validate_nested(nla_a,
  159. NLBL_CIPSOV4_A_MAX,
  160. netlbl_cipsov4_genl_policy) != 0)
  161. goto add_std_failure;
  162. nla_for_each_nested(nla_b, nla_a, nla_b_rem)
  163. switch (nla_type(nla_b)) {
  164. case NLBL_CIPSOV4_A_MLSLVLLOC:
  165. if (nla_get_u32(nla_b) >
  166. CIPSO_V4_MAX_LOC_LVLS)
  167. goto add_std_failure;
  168. if (nla_get_u32(nla_b) >=
  169. doi_def->map.std->lvl.local_size)
  170. doi_def->map.std->lvl.local_size =
  171. nla_get_u32(nla_b) + 1;
  172. break;
  173. case NLBL_CIPSOV4_A_MLSLVLREM:
  174. if (nla_get_u32(nla_b) >
  175. CIPSO_V4_MAX_REM_LVLS)
  176. goto add_std_failure;
  177. if (nla_get_u32(nla_b) >=
  178. doi_def->map.std->lvl.cipso_size)
  179. doi_def->map.std->lvl.cipso_size =
  180. nla_get_u32(nla_b) + 1;
  181. break;
  182. }
  183. }
  184. doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
  185. sizeof(u32),
  186. GFP_KERNEL);
  187. if (doi_def->map.std->lvl.local == NULL) {
  188. ret_val = -ENOMEM;
  189. goto add_std_failure;
  190. }
  191. doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
  192. sizeof(u32),
  193. GFP_KERNEL);
  194. if (doi_def->map.std->lvl.cipso == NULL) {
  195. ret_val = -ENOMEM;
  196. goto add_std_failure;
  197. }
  198. for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
  199. doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
  200. for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
  201. doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
  202. nla_for_each_nested(nla_a,
  203. info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
  204. nla_a_rem)
  205. if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
  206. struct nlattr *lvl_loc;
  207. struct nlattr *lvl_rem;
  208. lvl_loc = nla_find_nested(nla_a,
  209. NLBL_CIPSOV4_A_MLSLVLLOC);
  210. lvl_rem = nla_find_nested(nla_a,
  211. NLBL_CIPSOV4_A_MLSLVLREM);
  212. if (lvl_loc == NULL || lvl_rem == NULL)
  213. goto add_std_failure;
  214. doi_def->map.std->lvl.local[nla_get_u32(lvl_loc)] =
  215. nla_get_u32(lvl_rem);
  216. doi_def->map.std->lvl.cipso[nla_get_u32(lvl_rem)] =
  217. nla_get_u32(lvl_loc);
  218. }
  219. if (info->attrs[NLBL_CIPSOV4_A_MLSCATLST]) {
  220. if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
  221. NLBL_CIPSOV4_A_MAX,
  222. netlbl_cipsov4_genl_policy) != 0)
  223. goto add_std_failure;
  224. nla_for_each_nested(nla_a,
  225. info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
  226. nla_a_rem)
  227. if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
  228. if (nla_validate_nested(nla_a,
  229. NLBL_CIPSOV4_A_MAX,
  230. netlbl_cipsov4_genl_policy) != 0)
  231. goto add_std_failure;
  232. nla_for_each_nested(nla_b, nla_a, nla_b_rem)
  233. switch (nla_type(nla_b)) {
  234. case NLBL_CIPSOV4_A_MLSCATLOC:
  235. if (nla_get_u32(nla_b) >
  236. CIPSO_V4_MAX_LOC_CATS)
  237. goto add_std_failure;
  238. if (nla_get_u32(nla_b) >=
  239. doi_def->map.std->cat.local_size)
  240. doi_def->map.std->cat.local_size =
  241. nla_get_u32(nla_b) + 1;
  242. break;
  243. case NLBL_CIPSOV4_A_MLSCATREM:
  244. if (nla_get_u32(nla_b) >
  245. CIPSO_V4_MAX_REM_CATS)
  246. goto add_std_failure;
  247. if (nla_get_u32(nla_b) >=
  248. doi_def->map.std->cat.cipso_size)
  249. doi_def->map.std->cat.cipso_size =
  250. nla_get_u32(nla_b) + 1;
  251. break;
  252. }
  253. }
  254. doi_def->map.std->cat.local = kcalloc(
  255. doi_def->map.std->cat.local_size,
  256. sizeof(u32),
  257. GFP_KERNEL);
  258. if (doi_def->map.std->cat.local == NULL) {
  259. ret_val = -ENOMEM;
  260. goto add_std_failure;
  261. }
  262. doi_def->map.std->cat.cipso = kcalloc(
  263. doi_def->map.std->cat.cipso_size,
  264. sizeof(u32),
  265. GFP_KERNEL);
  266. if (doi_def->map.std->cat.cipso == NULL) {
  267. ret_val = -ENOMEM;
  268. goto add_std_failure;
  269. }
  270. for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
  271. doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
  272. for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
  273. doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
  274. nla_for_each_nested(nla_a,
  275. info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
  276. nla_a_rem)
  277. if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
  278. struct nlattr *cat_loc;
  279. struct nlattr *cat_rem;
  280. cat_loc = nla_find_nested(nla_a,
  281. NLBL_CIPSOV4_A_MLSCATLOC);
  282. cat_rem = nla_find_nested(nla_a,
  283. NLBL_CIPSOV4_A_MLSCATREM);
  284. if (cat_loc == NULL || cat_rem == NULL)
  285. goto add_std_failure;
  286. doi_def->map.std->cat.local[
  287. nla_get_u32(cat_loc)] =
  288. nla_get_u32(cat_rem);
  289. doi_def->map.std->cat.cipso[
  290. nla_get_u32(cat_rem)] =
  291. nla_get_u32(cat_loc);
  292. }
  293. }
  294. ret_val = cipso_v4_doi_add(doi_def);
  295. if (ret_val != 0)
  296. goto add_std_failure;
  297. return 0;
  298. add_std_failure:
  299. if (doi_def)
  300. cipso_v4_doi_free(doi_def);
  301. return ret_val;
  302. }
  303. /**
  304. * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
  305. * @info: the Generic NETLINK info block
  306. *
  307. * Description:
  308. * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
  309. * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
  310. * error.
  311. *
  312. */
  313. static int netlbl_cipsov4_add_pass(struct genl_info *info)
  314. {
  315. int ret_val;
  316. struct cipso_v4_doi *doi_def = NULL;
  317. if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
  318. return -EINVAL;
  319. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  320. if (doi_def == NULL)
  321. return -ENOMEM;
  322. doi_def->type = CIPSO_V4_MAP_PASS;
  323. ret_val = netlbl_cipsov4_add_common(info, doi_def);
  324. if (ret_val != 0)
  325. goto add_pass_failure;
  326. ret_val = cipso_v4_doi_add(doi_def);
  327. if (ret_val != 0)
  328. goto add_pass_failure;
  329. return 0;
  330. add_pass_failure:
  331. cipso_v4_doi_free(doi_def);
  332. return ret_val;
  333. }
  334. /**
  335. * netlbl_cipsov4_add_local - Adds a CIPSO V4 DOI definition
  336. * @info: the Generic NETLINK info block
  337. *
  338. * Description:
  339. * Create a new CIPSO_V4_MAP_LOCAL DOI definition based on the given ADD
  340. * message and add it to the CIPSO V4 engine. Return zero on success and
  341. * non-zero on error.
  342. *
  343. */
  344. static int netlbl_cipsov4_add_local(struct genl_info *info)
  345. {
  346. int ret_val;
  347. struct cipso_v4_doi *doi_def = NULL;
  348. if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
  349. return -EINVAL;
  350. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  351. if (doi_def == NULL)
  352. return -ENOMEM;
  353. doi_def->type = CIPSO_V4_MAP_LOCAL;
  354. ret_val = netlbl_cipsov4_add_common(info, doi_def);
  355. if (ret_val != 0)
  356. goto add_local_failure;
  357. ret_val = cipso_v4_doi_add(doi_def);
  358. if (ret_val != 0)
  359. goto add_local_failure;
  360. return 0;
  361. add_local_failure:
  362. cipso_v4_doi_free(doi_def);
  363. return ret_val;
  364. }
  365. /**
  366. * netlbl_cipsov4_add - Handle an ADD message
  367. * @skb: the NETLINK buffer
  368. * @info: the Generic NETLINK info block
  369. *
  370. * Description:
  371. * Create a new DOI definition based on the given ADD message and add it to the
  372. * CIPSO V4 engine. Returns zero on success, negative values on failure.
  373. *
  374. */
  375. static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
  376. {
  377. int ret_val = -EINVAL;
  378. u32 type;
  379. u32 doi;
  380. const char *type_str = "(unknown)";
  381. struct audit_buffer *audit_buf;
  382. struct netlbl_audit audit_info;
  383. if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
  384. !info->attrs[NLBL_CIPSOV4_A_MTYPE])
  385. return -EINVAL;
  386. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  387. netlbl_netlink_auditinfo(skb, &audit_info);
  388. type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]);
  389. switch (type) {
  390. case CIPSO_V4_MAP_TRANS:
  391. type_str = "trans";
  392. ret_val = netlbl_cipsov4_add_std(info);
  393. break;
  394. case CIPSO_V4_MAP_PASS:
  395. type_str = "pass";
  396. ret_val = netlbl_cipsov4_add_pass(info);
  397. break;
  398. case CIPSO_V4_MAP_LOCAL:
  399. type_str = "local";
  400. ret_val = netlbl_cipsov4_add_local(info);
  401. break;
  402. }
  403. if (ret_val == 0)
  404. atomic_inc(&netlabel_mgmt_protocount);
  405. audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
  406. &audit_info);
  407. if (audit_buf != NULL) {
  408. audit_log_format(audit_buf,
  409. " cipso_doi=%u cipso_type=%s res=%u",
  410. doi,
  411. type_str,
  412. ret_val == 0 ? 1 : 0);
  413. audit_log_end(audit_buf);
  414. }
  415. return ret_val;
  416. }
  417. /**
  418. * netlbl_cipsov4_list - Handle a LIST message
  419. * @skb: the NETLINK buffer
  420. * @info: the Generic NETLINK info block
  421. *
  422. * Description:
  423. * Process a user generated LIST message and respond accordingly. While the
  424. * response message generated by the kernel is straightforward, determining
  425. * before hand the size of the buffer to allocate is not (we have to generate
  426. * the message to know the size). In order to keep this function sane what we
  427. * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
  428. * that size, if we fail then we restart with a larger buffer and try again.
  429. * We continue in this manner until we hit a limit of failed attempts then we
  430. * give up and just send an error message. Returns zero on success and
  431. * negative values on error.
  432. *
  433. */
  434. static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
  435. {
  436. int ret_val;
  437. struct sk_buff *ans_skb = NULL;
  438. u32 nlsze_mult = 1;
  439. void *data;
  440. u32 doi;
  441. struct nlattr *nla_a;
  442. struct nlattr *nla_b;
  443. struct cipso_v4_doi *doi_def;
  444. u32 iter;
  445. if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
  446. ret_val = -EINVAL;
  447. goto list_failure;
  448. }
  449. list_start:
  450. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
  451. if (ans_skb == NULL) {
  452. ret_val = -ENOMEM;
  453. goto list_failure;
  454. }
  455. data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
  456. 0, NLBL_CIPSOV4_C_LIST);
  457. if (data == NULL) {
  458. ret_val = -ENOMEM;
  459. goto list_failure;
  460. }
  461. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  462. rcu_read_lock();
  463. doi_def = cipso_v4_doi_getdef(doi);
  464. if (doi_def == NULL) {
  465. ret_val = -EINVAL;
  466. goto list_failure_lock;
  467. }
  468. ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
  469. if (ret_val != 0)
  470. goto list_failure_lock;
  471. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
  472. if (nla_a == NULL) {
  473. ret_val = -ENOMEM;
  474. goto list_failure_lock;
  475. }
  476. for (iter = 0;
  477. iter < CIPSO_V4_TAG_MAXCNT &&
  478. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
  479. iter++) {
  480. ret_val = nla_put_u8(ans_skb,
  481. NLBL_CIPSOV4_A_TAG,
  482. doi_def->tags[iter]);
  483. if (ret_val != 0)
  484. goto list_failure_lock;
  485. }
  486. nla_nest_end(ans_skb, nla_a);
  487. switch (doi_def->type) {
  488. case CIPSO_V4_MAP_TRANS:
  489. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
  490. if (nla_a == NULL) {
  491. ret_val = -ENOMEM;
  492. goto list_failure_lock;
  493. }
  494. for (iter = 0;
  495. iter < doi_def->map.std->lvl.local_size;
  496. iter++) {
  497. if (doi_def->map.std->lvl.local[iter] ==
  498. CIPSO_V4_INV_LVL)
  499. continue;
  500. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
  501. if (nla_b == NULL) {
  502. ret_val = -ENOMEM;
  503. goto list_retry;
  504. }
  505. ret_val = nla_put_u32(ans_skb,
  506. NLBL_CIPSOV4_A_MLSLVLLOC,
  507. iter);
  508. if (ret_val != 0)
  509. goto list_retry;
  510. ret_val = nla_put_u32(ans_skb,
  511. NLBL_CIPSOV4_A_MLSLVLREM,
  512. doi_def->map.std->lvl.local[iter]);
  513. if (ret_val != 0)
  514. goto list_retry;
  515. nla_nest_end(ans_skb, nla_b);
  516. }
  517. nla_nest_end(ans_skb, nla_a);
  518. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
  519. if (nla_a == NULL) {
  520. ret_val = -ENOMEM;
  521. goto list_retry;
  522. }
  523. for (iter = 0;
  524. iter < doi_def->map.std->cat.local_size;
  525. iter++) {
  526. if (doi_def->map.std->cat.local[iter] ==
  527. CIPSO_V4_INV_CAT)
  528. continue;
  529. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
  530. if (nla_b == NULL) {
  531. ret_val = -ENOMEM;
  532. goto list_retry;
  533. }
  534. ret_val = nla_put_u32(ans_skb,
  535. NLBL_CIPSOV4_A_MLSCATLOC,
  536. iter);
  537. if (ret_val != 0)
  538. goto list_retry;
  539. ret_val = nla_put_u32(ans_skb,
  540. NLBL_CIPSOV4_A_MLSCATREM,
  541. doi_def->map.std->cat.local[iter]);
  542. if (ret_val != 0)
  543. goto list_retry;
  544. nla_nest_end(ans_skb, nla_b);
  545. }
  546. nla_nest_end(ans_skb, nla_a);
  547. break;
  548. }
  549. rcu_read_unlock();
  550. genlmsg_end(ans_skb, data);
  551. return genlmsg_reply(ans_skb, info);
  552. list_retry:
  553. /* XXX - this limit is a guesstimate */
  554. if (nlsze_mult < 4) {
  555. rcu_read_unlock();
  556. kfree_skb(ans_skb);
  557. nlsze_mult *= 2;
  558. goto list_start;
  559. }
  560. list_failure_lock:
  561. rcu_read_unlock();
  562. list_failure:
  563. kfree_skb(ans_skb);
  564. return ret_val;
  565. }
  566. /**
  567. * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
  568. * @doi_def: the CIPSOv4 DOI definition
  569. * @arg: the netlbl_cipsov4_doiwalk_arg structure
  570. *
  571. * Description:
  572. * This function is designed to be used as a callback to the
  573. * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
  574. * message. Returns the size of the message on success, negative values on
  575. * failure.
  576. *
  577. */
  578. static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
  579. {
  580. int ret_val = -ENOMEM;
  581. struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
  582. void *data;
  583. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  584. cb_arg->seq, &netlbl_cipsov4_gnl_family,
  585. NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
  586. if (data == NULL)
  587. goto listall_cb_failure;
  588. ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
  589. if (ret_val != 0)
  590. goto listall_cb_failure;
  591. ret_val = nla_put_u32(cb_arg->skb,
  592. NLBL_CIPSOV4_A_MTYPE,
  593. doi_def->type);
  594. if (ret_val != 0)
  595. goto listall_cb_failure;
  596. return genlmsg_end(cb_arg->skb, data);
  597. listall_cb_failure:
  598. genlmsg_cancel(cb_arg->skb, data);
  599. return ret_val;
  600. }
  601. /**
  602. * netlbl_cipsov4_listall - Handle a LISTALL message
  603. * @skb: the NETLINK buffer
  604. * @cb: the NETLINK callback
  605. *
  606. * Description:
  607. * Process a user generated LISTALL message and respond accordingly. Returns
  608. * zero on success and negative values on error.
  609. *
  610. */
  611. static int netlbl_cipsov4_listall(struct sk_buff *skb,
  612. struct netlink_callback *cb)
  613. {
  614. struct netlbl_cipsov4_doiwalk_arg cb_arg;
  615. u32 doi_skip = cb->args[0];
  616. cb_arg.nl_cb = cb;
  617. cb_arg.skb = skb;
  618. cb_arg.seq = cb->nlh->nlmsg_seq;
  619. cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
  620. cb->args[0] = doi_skip;
  621. return skb->len;
  622. }
  623. /**
  624. * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
  625. * @entry: LSM domain mapping entry
  626. * @arg: the netlbl_domhsh_walk_arg structure
  627. *
  628. * Description:
  629. * This function is intended for use by netlbl_cipsov4_remove() as the callback
  630. * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
  631. * which are associated with the CIPSO DOI specified in @arg. Returns zero on
  632. * success, negative values on failure.
  633. *
  634. */
  635. static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
  636. {
  637. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  638. if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
  639. entry->type_def.cipsov4->doi == cb_arg->doi)
  640. return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
  641. return 0;
  642. }
  643. /**
  644. * netlbl_cipsov4_remove - Handle a REMOVE message
  645. * @skb: the NETLINK buffer
  646. * @info: the Generic NETLINK info block
  647. *
  648. * Description:
  649. * Process a user generated REMOVE message and respond accordingly. Returns
  650. * zero on success, negative values on failure.
  651. *
  652. */
  653. static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
  654. {
  655. int ret_val = -EINVAL;
  656. u32 doi = 0;
  657. struct netlbl_domhsh_walk_arg cb_arg;
  658. struct audit_buffer *audit_buf;
  659. struct netlbl_audit audit_info;
  660. u32 skip_bkt = 0;
  661. u32 skip_chain = 0;
  662. if (!info->attrs[NLBL_CIPSOV4_A_DOI])
  663. return -EINVAL;
  664. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  665. netlbl_netlink_auditinfo(skb, &audit_info);
  666. cb_arg.doi = doi;
  667. cb_arg.audit_info = &audit_info;
  668. ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
  669. netlbl_cipsov4_remove_cb, &cb_arg);
  670. if (ret_val == 0 || ret_val == -ENOENT) {
  671. ret_val = cipso_v4_doi_remove(doi, &audit_info);
  672. if (ret_val == 0)
  673. atomic_dec(&netlabel_mgmt_protocount);
  674. }
  675. audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_DEL,
  676. &audit_info);
  677. if (audit_buf != NULL) {
  678. audit_log_format(audit_buf,
  679. " cipso_doi=%u res=%u",
  680. doi,
  681. ret_val == 0 ? 1 : 0);
  682. audit_log_end(audit_buf);
  683. }
  684. return ret_val;
  685. }
  686. /*
  687. * NetLabel Generic NETLINK Command Definitions
  688. */
  689. static struct genl_ops netlbl_cipsov4_ops[] = {
  690. {
  691. .cmd = NLBL_CIPSOV4_C_ADD,
  692. .flags = GENL_ADMIN_PERM,
  693. .policy = netlbl_cipsov4_genl_policy,
  694. .doit = netlbl_cipsov4_add,
  695. .dumpit = NULL,
  696. },
  697. {
  698. .cmd = NLBL_CIPSOV4_C_REMOVE,
  699. .flags = GENL_ADMIN_PERM,
  700. .policy = netlbl_cipsov4_genl_policy,
  701. .doit = netlbl_cipsov4_remove,
  702. .dumpit = NULL,
  703. },
  704. {
  705. .cmd = NLBL_CIPSOV4_C_LIST,
  706. .flags = 0,
  707. .policy = netlbl_cipsov4_genl_policy,
  708. .doit = netlbl_cipsov4_list,
  709. .dumpit = NULL,
  710. },
  711. {
  712. .cmd = NLBL_CIPSOV4_C_LISTALL,
  713. .flags = 0,
  714. .policy = netlbl_cipsov4_genl_policy,
  715. .doit = NULL,
  716. .dumpit = netlbl_cipsov4_listall,
  717. },
  718. };
  719. /*
  720. * NetLabel Generic NETLINK Protocol Functions
  721. */
  722. /**
  723. * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
  724. *
  725. * Description:
  726. * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
  727. * mechanism. Returns zero on success, negative values on failure.
  728. *
  729. */
  730. int __init netlbl_cipsov4_genl_init(void)
  731. {
  732. int ret_val, i;
  733. ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
  734. if (ret_val != 0)
  735. return ret_val;
  736. for (i = 0; i < ARRAY_SIZE(netlbl_cipsov4_ops); i++) {
  737. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  738. &netlbl_cipsov4_ops[i]);
  739. if (ret_val != 0)
  740. return ret_val;
  741. }
  742. return 0;
  743. }