netlabel_cipso_v4.c 20 KB

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