netlabel_cipso_v4.c 20 KB

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