netlabel_cipso_v4.c 21 KB

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