netlabel_cipso_v4.c 21 KB

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