netlabel_cipso_v4.c 21 KB

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