netlabel_cipso_v4.c 21 KB

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