netlabel_cipso_v4.c 21 KB

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