netlabel_cipso_v4.c 20 KB

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