netlabel_cipso_v4.c 21 KB

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