netlabel_cipso_v4.c 20 KB

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