netlabel_cipso_v4.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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@paul-moore.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 <linux/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. 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. ret_val = netlbl_cipsov4_add_std(info, &audit_info);
  393. break;
  394. case CIPSO_V4_MAP_PASS:
  395. ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
  396. break;
  397. case CIPSO_V4_MAP_LOCAL:
  398. ret_val = netlbl_cipsov4_add_local(info, &audit_info);
  399. break;
  400. }
  401. if (ret_val == 0)
  402. atomic_inc(&netlabel_mgmt_protocount);
  403. return ret_val;
  404. }
  405. /**
  406. * netlbl_cipsov4_list - Handle a LIST message
  407. * @skb: the NETLINK buffer
  408. * @info: the Generic NETLINK info block
  409. *
  410. * Description:
  411. * Process a user generated LIST message and respond accordingly. While the
  412. * response message generated by the kernel is straightforward, determining
  413. * before hand the size of the buffer to allocate is not (we have to generate
  414. * the message to know the size). In order to keep this function sane what we
  415. * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
  416. * that size, if we fail then we restart with a larger buffer and try again.
  417. * We continue in this manner until we hit a limit of failed attempts then we
  418. * give up and just send an error message. Returns zero on success and
  419. * negative values on error.
  420. *
  421. */
  422. static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
  423. {
  424. int ret_val;
  425. struct sk_buff *ans_skb = NULL;
  426. u32 nlsze_mult = 1;
  427. void *data;
  428. u32 doi;
  429. struct nlattr *nla_a;
  430. struct nlattr *nla_b;
  431. struct cipso_v4_doi *doi_def;
  432. u32 iter;
  433. if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
  434. ret_val = -EINVAL;
  435. goto list_failure;
  436. }
  437. list_start:
  438. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE * nlsze_mult, GFP_KERNEL);
  439. if (ans_skb == NULL) {
  440. ret_val = -ENOMEM;
  441. goto list_failure;
  442. }
  443. data = genlmsg_put_reply(ans_skb, info, &netlbl_cipsov4_gnl_family,
  444. 0, NLBL_CIPSOV4_C_LIST);
  445. if (data == NULL) {
  446. ret_val = -ENOMEM;
  447. goto list_failure;
  448. }
  449. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  450. rcu_read_lock();
  451. doi_def = cipso_v4_doi_getdef(doi);
  452. if (doi_def == NULL) {
  453. ret_val = -EINVAL;
  454. goto list_failure_lock;
  455. }
  456. ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
  457. if (ret_val != 0)
  458. goto list_failure_lock;
  459. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
  460. if (nla_a == NULL) {
  461. ret_val = -ENOMEM;
  462. goto list_failure_lock;
  463. }
  464. for (iter = 0;
  465. iter < CIPSO_V4_TAG_MAXCNT &&
  466. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
  467. iter++) {
  468. ret_val = nla_put_u8(ans_skb,
  469. NLBL_CIPSOV4_A_TAG,
  470. doi_def->tags[iter]);
  471. if (ret_val != 0)
  472. goto list_failure_lock;
  473. }
  474. nla_nest_end(ans_skb, nla_a);
  475. switch (doi_def->type) {
  476. case CIPSO_V4_MAP_TRANS:
  477. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
  478. if (nla_a == NULL) {
  479. ret_val = -ENOMEM;
  480. goto list_failure_lock;
  481. }
  482. for (iter = 0;
  483. iter < doi_def->map.std->lvl.local_size;
  484. iter++) {
  485. if (doi_def->map.std->lvl.local[iter] ==
  486. CIPSO_V4_INV_LVL)
  487. continue;
  488. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
  489. if (nla_b == NULL) {
  490. ret_val = -ENOMEM;
  491. goto list_retry;
  492. }
  493. ret_val = nla_put_u32(ans_skb,
  494. NLBL_CIPSOV4_A_MLSLVLLOC,
  495. iter);
  496. if (ret_val != 0)
  497. goto list_retry;
  498. ret_val = nla_put_u32(ans_skb,
  499. NLBL_CIPSOV4_A_MLSLVLREM,
  500. doi_def->map.std->lvl.local[iter]);
  501. if (ret_val != 0)
  502. goto list_retry;
  503. nla_nest_end(ans_skb, nla_b);
  504. }
  505. nla_nest_end(ans_skb, nla_a);
  506. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
  507. if (nla_a == NULL) {
  508. ret_val = -ENOMEM;
  509. goto list_retry;
  510. }
  511. for (iter = 0;
  512. iter < doi_def->map.std->cat.local_size;
  513. iter++) {
  514. if (doi_def->map.std->cat.local[iter] ==
  515. CIPSO_V4_INV_CAT)
  516. continue;
  517. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
  518. if (nla_b == NULL) {
  519. ret_val = -ENOMEM;
  520. goto list_retry;
  521. }
  522. ret_val = nla_put_u32(ans_skb,
  523. NLBL_CIPSOV4_A_MLSCATLOC,
  524. iter);
  525. if (ret_val != 0)
  526. goto list_retry;
  527. ret_val = nla_put_u32(ans_skb,
  528. NLBL_CIPSOV4_A_MLSCATREM,
  529. doi_def->map.std->cat.local[iter]);
  530. if (ret_val != 0)
  531. goto list_retry;
  532. nla_nest_end(ans_skb, nla_b);
  533. }
  534. nla_nest_end(ans_skb, nla_a);
  535. break;
  536. }
  537. rcu_read_unlock();
  538. genlmsg_end(ans_skb, data);
  539. return genlmsg_reply(ans_skb, info);
  540. list_retry:
  541. /* XXX - this limit is a guesstimate */
  542. if (nlsze_mult < 4) {
  543. rcu_read_unlock();
  544. kfree_skb(ans_skb);
  545. nlsze_mult *= 2;
  546. goto list_start;
  547. }
  548. list_failure_lock:
  549. rcu_read_unlock();
  550. list_failure:
  551. kfree_skb(ans_skb);
  552. return ret_val;
  553. }
  554. /**
  555. * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
  556. * @doi_def: the CIPSOv4 DOI definition
  557. * @arg: the netlbl_cipsov4_doiwalk_arg structure
  558. *
  559. * Description:
  560. * This function is designed to be used as a callback to the
  561. * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
  562. * message. Returns the size of the message on success, negative values on
  563. * failure.
  564. *
  565. */
  566. static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
  567. {
  568. int ret_val = -ENOMEM;
  569. struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
  570. void *data;
  571. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  572. cb_arg->seq, &netlbl_cipsov4_gnl_family,
  573. NLM_F_MULTI, NLBL_CIPSOV4_C_LISTALL);
  574. if (data == NULL)
  575. goto listall_cb_failure;
  576. ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
  577. if (ret_val != 0)
  578. goto listall_cb_failure;
  579. ret_val = nla_put_u32(cb_arg->skb,
  580. NLBL_CIPSOV4_A_MTYPE,
  581. doi_def->type);
  582. if (ret_val != 0)
  583. goto listall_cb_failure;
  584. return genlmsg_end(cb_arg->skb, data);
  585. listall_cb_failure:
  586. genlmsg_cancel(cb_arg->skb, data);
  587. return ret_val;
  588. }
  589. /**
  590. * netlbl_cipsov4_listall - Handle a LISTALL message
  591. * @skb: the NETLINK buffer
  592. * @cb: the NETLINK callback
  593. *
  594. * Description:
  595. * Process a user generated LISTALL message and respond accordingly. Returns
  596. * zero on success and negative values on error.
  597. *
  598. */
  599. static int netlbl_cipsov4_listall(struct sk_buff *skb,
  600. struct netlink_callback *cb)
  601. {
  602. struct netlbl_cipsov4_doiwalk_arg cb_arg;
  603. u32 doi_skip = cb->args[0];
  604. cb_arg.nl_cb = cb;
  605. cb_arg.skb = skb;
  606. cb_arg.seq = cb->nlh->nlmsg_seq;
  607. cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
  608. cb->args[0] = doi_skip;
  609. return skb->len;
  610. }
  611. /**
  612. * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
  613. * @entry: LSM domain mapping entry
  614. * @arg: the netlbl_domhsh_walk_arg structure
  615. *
  616. * Description:
  617. * This function is intended for use by netlbl_cipsov4_remove() as the callback
  618. * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
  619. * which are associated with the CIPSO DOI specified in @arg. Returns zero on
  620. * success, negative values on failure.
  621. *
  622. */
  623. static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
  624. {
  625. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  626. if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
  627. entry->type_def.cipsov4->doi == cb_arg->doi)
  628. return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
  629. return 0;
  630. }
  631. /**
  632. * netlbl_cipsov4_remove - Handle a REMOVE message
  633. * @skb: the NETLINK buffer
  634. * @info: the Generic NETLINK info block
  635. *
  636. * Description:
  637. * Process a user generated REMOVE message and respond accordingly. Returns
  638. * zero on success, negative values on failure.
  639. *
  640. */
  641. static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
  642. {
  643. int ret_val = -EINVAL;
  644. struct netlbl_domhsh_walk_arg cb_arg;
  645. struct netlbl_audit audit_info;
  646. u32 skip_bkt = 0;
  647. u32 skip_chain = 0;
  648. if (!info->attrs[NLBL_CIPSOV4_A_DOI])
  649. return -EINVAL;
  650. netlbl_netlink_auditinfo(skb, &audit_info);
  651. cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  652. cb_arg.audit_info = &audit_info;
  653. ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
  654. netlbl_cipsov4_remove_cb, &cb_arg);
  655. if (ret_val == 0 || ret_val == -ENOENT) {
  656. ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
  657. if (ret_val == 0)
  658. atomic_dec(&netlabel_mgmt_protocount);
  659. }
  660. return ret_val;
  661. }
  662. /*
  663. * NetLabel Generic NETLINK Command Definitions
  664. */
  665. static struct genl_ops netlbl_cipsov4_ops[] = {
  666. {
  667. .cmd = NLBL_CIPSOV4_C_ADD,
  668. .flags = GENL_ADMIN_PERM,
  669. .policy = netlbl_cipsov4_genl_policy,
  670. .doit = netlbl_cipsov4_add,
  671. .dumpit = NULL,
  672. },
  673. {
  674. .cmd = NLBL_CIPSOV4_C_REMOVE,
  675. .flags = GENL_ADMIN_PERM,
  676. .policy = netlbl_cipsov4_genl_policy,
  677. .doit = netlbl_cipsov4_remove,
  678. .dumpit = NULL,
  679. },
  680. {
  681. .cmd = NLBL_CIPSOV4_C_LIST,
  682. .flags = 0,
  683. .policy = netlbl_cipsov4_genl_policy,
  684. .doit = netlbl_cipsov4_list,
  685. .dumpit = NULL,
  686. },
  687. {
  688. .cmd = NLBL_CIPSOV4_C_LISTALL,
  689. .flags = 0,
  690. .policy = netlbl_cipsov4_genl_policy,
  691. .doit = NULL,
  692. .dumpit = netlbl_cipsov4_listall,
  693. },
  694. };
  695. /*
  696. * NetLabel Generic NETLINK Protocol Functions
  697. */
  698. /**
  699. * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
  700. *
  701. * Description:
  702. * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
  703. * mechanism. Returns zero on success, negative values on failure.
  704. *
  705. */
  706. int __init netlbl_cipsov4_genl_init(void)
  707. {
  708. return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family,
  709. netlbl_cipsov4_ops, ARRAY_SIZE(netlbl_cipsov4_ops));
  710. }