netlabel_cipso_v4.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 <net/sock.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include <net/netlabel.h>
  37. #include <net/cipso_ipv4.h>
  38. #include "netlabel_user.h"
  39. #include "netlabel_cipso_v4.h"
  40. /* Argument struct for cipso_v4_doi_walk() */
  41. struct netlbl_cipsov4_doiwalk_arg {
  42. struct netlink_callback *nl_cb;
  43. struct sk_buff *skb;
  44. u32 seq;
  45. };
  46. /* NetLabel Generic NETLINK CIPSOv4 family */
  47. static struct genl_family netlbl_cipsov4_gnl_family = {
  48. .id = GENL_ID_GENERATE,
  49. .hdrsize = 0,
  50. .name = NETLBL_NLTYPE_CIPSOV4_NAME,
  51. .version = NETLBL_PROTO_VERSION,
  52. .maxattr = NLBL_CIPSOV4_A_MAX,
  53. };
  54. /* NetLabel Netlink attribute policy */
  55. static struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
  56. [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
  57. [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
  58. [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
  59. [NLBL_CIPSOV4_A_TAGLST] = { .type = NLA_NESTED },
  60. [NLBL_CIPSOV4_A_MLSLVLLOC] = { .type = NLA_U32 },
  61. [NLBL_CIPSOV4_A_MLSLVLREM] = { .type = NLA_U32 },
  62. [NLBL_CIPSOV4_A_MLSLVL] = { .type = NLA_NESTED },
  63. [NLBL_CIPSOV4_A_MLSLVLLST] = { .type = NLA_NESTED },
  64. [NLBL_CIPSOV4_A_MLSCATLOC] = { .type = NLA_U32 },
  65. [NLBL_CIPSOV4_A_MLSCATREM] = { .type = NLA_U32 },
  66. [NLBL_CIPSOV4_A_MLSCAT] = { .type = NLA_NESTED },
  67. [NLBL_CIPSOV4_A_MLSCATLST] = { .type = NLA_NESTED },
  68. };
  69. /*
  70. * Helper Functions
  71. */
  72. /**
  73. * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
  74. * @entry: the entry's RCU field
  75. *
  76. * Description:
  77. * This function is designed to be used as a callback to the call_rcu()
  78. * function so that the memory allocated to the DOI definition can be released
  79. * safely.
  80. *
  81. */
  82. static void netlbl_cipsov4_doi_free(struct rcu_head *entry)
  83. {
  84. struct cipso_v4_doi *ptr;
  85. ptr = container_of(entry, struct cipso_v4_doi, rcu);
  86. switch (ptr->type) {
  87. case CIPSO_V4_MAP_STD:
  88. kfree(ptr->map.std->lvl.cipso);
  89. kfree(ptr->map.std->lvl.local);
  90. kfree(ptr->map.std->cat.cipso);
  91. kfree(ptr->map.std->cat.local);
  92. break;
  93. }
  94. kfree(ptr);
  95. }
  96. /**
  97. * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
  98. * @info: the Generic NETLINK info block
  99. * @doi_def: the CIPSO V4 DOI definition
  100. *
  101. * Description:
  102. * Parse the common sections of a ADD message and fill in the related values
  103. * in @doi_def. Returns zero on success, negative values on failure.
  104. *
  105. */
  106. static int netlbl_cipsov4_add_common(struct genl_info *info,
  107. struct cipso_v4_doi *doi_def)
  108. {
  109. struct nlattr *nla;
  110. int nla_rem;
  111. u32 iter = 0;
  112. doi_def->doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  113. if (nla_validate_nested(info->attrs[NLBL_CIPSOV4_A_TAGLST],
  114. NLBL_CIPSOV4_A_MAX,
  115. netlbl_cipsov4_genl_policy) != 0)
  116. return -EINVAL;
  117. nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
  118. if (nla->nla_type == NLBL_CIPSOV4_A_TAG) {
  119. if (iter > CIPSO_V4_TAG_MAXCNT)
  120. return -EINVAL;
  121. doi_def->tags[iter++] = nla_get_u8(nla);
  122. }
  123. if (iter < CIPSO_V4_TAG_MAXCNT)
  124. doi_def->tags[iter] = CIPSO_V4_TAG_INVALID;
  125. return 0;
  126. }
  127. /*
  128. * NetLabel Command Handlers
  129. */
  130. /**
  131. * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
  132. * @info: the Generic NETLINK info block
  133. *
  134. * Description:
  135. * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
  136. * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
  137. * error.
  138. *
  139. */
  140. static int netlbl_cipsov4_add_std(struct genl_info *info)
  141. {
  142. int ret_val = -EINVAL;
  143. struct cipso_v4_doi *doi_def = NULL;
  144. struct nlattr *nla_a;
  145. struct nlattr *nla_b;
  146. int nla_a_rem;
  147. int nla_b_rem;
  148. if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
  149. !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_DOI] ||
  318. !info->attrs[NLBL_CIPSOV4_A_TAGLST])
  319. return -EINVAL;
  320. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  321. if (doi_def == NULL)
  322. return -ENOMEM;
  323. doi_def->type = CIPSO_V4_MAP_PASS;
  324. ret_val = netlbl_cipsov4_add_common(info, doi_def);
  325. if (ret_val != 0)
  326. goto add_pass_failure;
  327. ret_val = cipso_v4_doi_add(doi_def);
  328. if (ret_val != 0)
  329. goto add_pass_failure;
  330. return 0;
  331. add_pass_failure:
  332. netlbl_cipsov4_doi_free(&doi_def->rcu);
  333. return ret_val;
  334. }
  335. /**
  336. * netlbl_cipsov4_add - Handle an ADD message
  337. * @skb: the NETLINK buffer
  338. * @info: the Generic NETLINK info block
  339. *
  340. * Description:
  341. * Create a new DOI definition based on the given ADD message and add it to the
  342. * CIPSO V4 engine. Returns zero on success, negative values on failure.
  343. *
  344. */
  345. static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
  346. {
  347. int ret_val = -EINVAL;
  348. u32 map_type;
  349. if (!info->attrs[NLBL_CIPSOV4_A_MTYPE])
  350. return -EINVAL;
  351. map_type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]);
  352. switch (map_type) {
  353. case CIPSO_V4_MAP_STD:
  354. ret_val = netlbl_cipsov4_add_std(info);
  355. break;
  356. case CIPSO_V4_MAP_PASS:
  357. ret_val = netlbl_cipsov4_add_pass(info);
  358. break;
  359. }
  360. return ret_val;
  361. }
  362. /**
  363. * netlbl_cipsov4_list - Handle a LIST message
  364. * @skb: the NETLINK buffer
  365. * @info: the Generic NETLINK info block
  366. *
  367. * Description:
  368. * Process a user generated LIST message and respond accordingly. While the
  369. * response message generated by the kernel is straightforward, determining
  370. * before hand the size of the buffer to allocate is not (we have to generate
  371. * the message to know the size). In order to keep this function sane what we
  372. * do is allocate a buffer of NLMSG_GOODSIZE and try to fit the response in
  373. * that size, if we fail then we restart with a larger buffer and try again.
  374. * We continue in this manner until we hit a limit of failed attempts then we
  375. * give up and just send an error message. Returns zero on success and
  376. * negative values on error.
  377. *
  378. */
  379. static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
  380. {
  381. int ret_val;
  382. struct sk_buff *ans_skb = NULL;
  383. u32 nlsze_mult = 1;
  384. void *data;
  385. u32 doi;
  386. struct nlattr *nla_a;
  387. struct nlattr *nla_b;
  388. struct cipso_v4_doi *doi_def;
  389. u32 iter;
  390. if (!info->attrs[NLBL_CIPSOV4_A_DOI]) {
  391. ret_val = -EINVAL;
  392. goto list_failure;
  393. }
  394. list_start:
  395. ans_skb = nlmsg_new(NLMSG_GOODSIZE * nlsze_mult, GFP_KERNEL);
  396. if (ans_skb == NULL) {
  397. ret_val = -ENOMEM;
  398. goto list_failure;
  399. }
  400. data = netlbl_netlink_hdr_put(ans_skb,
  401. info->snd_pid,
  402. info->snd_seq,
  403. netlbl_cipsov4_gnl_family.id,
  404. 0,
  405. NLBL_CIPSOV4_C_LIST);
  406. if (data == NULL) {
  407. ret_val = -ENOMEM;
  408. goto list_failure;
  409. }
  410. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  411. rcu_read_lock();
  412. doi_def = cipso_v4_doi_getdef(doi);
  413. if (doi_def == NULL) {
  414. ret_val = -EINVAL;
  415. goto list_failure;
  416. }
  417. ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
  418. if (ret_val != 0)
  419. goto list_failure_lock;
  420. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_TAGLST);
  421. if (nla_a == NULL) {
  422. ret_val = -ENOMEM;
  423. goto list_failure_lock;
  424. }
  425. for (iter = 0;
  426. iter < CIPSO_V4_TAG_MAXCNT &&
  427. doi_def->tags[iter] != CIPSO_V4_TAG_INVALID;
  428. iter++) {
  429. ret_val = nla_put_u8(ans_skb,
  430. NLBL_CIPSOV4_A_TAG,
  431. doi_def->tags[iter]);
  432. if (ret_val != 0)
  433. goto list_failure_lock;
  434. }
  435. nla_nest_end(ans_skb, nla_a);
  436. switch (doi_def->type) {
  437. case CIPSO_V4_MAP_STD:
  438. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
  439. if (nla_a == NULL) {
  440. ret_val = -ENOMEM;
  441. goto list_failure_lock;
  442. }
  443. for (iter = 0;
  444. iter < doi_def->map.std->lvl.local_size;
  445. iter++) {
  446. if (doi_def->map.std->lvl.local[iter] ==
  447. CIPSO_V4_INV_LVL)
  448. continue;
  449. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVL);
  450. if (nla_b == NULL) {
  451. ret_val = -ENOMEM;
  452. goto list_retry;
  453. }
  454. ret_val = nla_put_u32(ans_skb,
  455. NLBL_CIPSOV4_A_MLSLVLLOC,
  456. iter);
  457. if (ret_val != 0)
  458. goto list_retry;
  459. ret_val = nla_put_u32(ans_skb,
  460. NLBL_CIPSOV4_A_MLSLVLREM,
  461. doi_def->map.std->lvl.local[iter]);
  462. if (ret_val != 0)
  463. goto list_retry;
  464. nla_nest_end(ans_skb, nla_b);
  465. }
  466. nla_nest_end(ans_skb, nla_a);
  467. nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCATLST);
  468. if (nla_a == NULL) {
  469. ret_val = -ENOMEM;
  470. goto list_retry;
  471. }
  472. for (iter = 0;
  473. iter < doi_def->map.std->cat.local_size;
  474. iter++) {
  475. if (doi_def->map.std->cat.local[iter] ==
  476. CIPSO_V4_INV_CAT)
  477. continue;
  478. nla_b = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSCAT);
  479. if (nla_b == NULL) {
  480. ret_val = -ENOMEM;
  481. goto list_retry;
  482. }
  483. ret_val = nla_put_u32(ans_skb,
  484. NLBL_CIPSOV4_A_MLSCATLOC,
  485. iter);
  486. if (ret_val != 0)
  487. goto list_retry;
  488. ret_val = nla_put_u32(ans_skb,
  489. NLBL_CIPSOV4_A_MLSCATREM,
  490. doi_def->map.std->cat.local[iter]);
  491. if (ret_val != 0)
  492. goto list_retry;
  493. nla_nest_end(ans_skb, nla_b);
  494. }
  495. nla_nest_end(ans_skb, nla_a);
  496. break;
  497. }
  498. rcu_read_unlock();
  499. genlmsg_end(ans_skb, data);
  500. ret_val = genlmsg_unicast(ans_skb, info->snd_pid);
  501. if (ret_val != 0)
  502. goto list_failure;
  503. return 0;
  504. list_retry:
  505. /* XXX - this limit is a guesstimate */
  506. if (nlsze_mult < 4) {
  507. rcu_read_unlock();
  508. kfree_skb(ans_skb);
  509. nlsze_mult++;
  510. goto list_start;
  511. }
  512. list_failure_lock:
  513. rcu_read_unlock();
  514. list_failure:
  515. kfree_skb(ans_skb);
  516. return ret_val;
  517. }
  518. /**
  519. * netlbl_cipsov4_listall_cb - cipso_v4_doi_walk() callback for LISTALL
  520. * @doi_def: the CIPSOv4 DOI definition
  521. * @arg: the netlbl_cipsov4_doiwalk_arg structure
  522. *
  523. * Description:
  524. * This function is designed to be used as a callback to the
  525. * cipso_v4_doi_walk() function for use in generating a response for a LISTALL
  526. * message. Returns the size of the message on success, negative values on
  527. * failure.
  528. *
  529. */
  530. static int netlbl_cipsov4_listall_cb(struct cipso_v4_doi *doi_def, void *arg)
  531. {
  532. int ret_val = -ENOMEM;
  533. struct netlbl_cipsov4_doiwalk_arg *cb_arg = arg;
  534. void *data;
  535. data = netlbl_netlink_hdr_put(cb_arg->skb,
  536. NETLINK_CB(cb_arg->nl_cb->skb).pid,
  537. cb_arg->seq,
  538. netlbl_cipsov4_gnl_family.id,
  539. NLM_F_MULTI,
  540. NLBL_CIPSOV4_C_LISTALL);
  541. if (data == NULL)
  542. goto listall_cb_failure;
  543. ret_val = nla_put_u32(cb_arg->skb, NLBL_CIPSOV4_A_DOI, doi_def->doi);
  544. if (ret_val != 0)
  545. goto listall_cb_failure;
  546. ret_val = nla_put_u32(cb_arg->skb,
  547. NLBL_CIPSOV4_A_MTYPE,
  548. doi_def->type);
  549. if (ret_val != 0)
  550. goto listall_cb_failure;
  551. return genlmsg_end(cb_arg->skb, data);
  552. listall_cb_failure:
  553. genlmsg_cancel(cb_arg->skb, data);
  554. return ret_val;
  555. }
  556. /**
  557. * netlbl_cipsov4_listall - Handle a LISTALL message
  558. * @skb: the NETLINK buffer
  559. * @cb: the NETLINK callback
  560. *
  561. * Description:
  562. * Process a user generated LISTALL message and respond accordingly. Returns
  563. * zero on success and negative values on error.
  564. *
  565. */
  566. static int netlbl_cipsov4_listall(struct sk_buff *skb,
  567. struct netlink_callback *cb)
  568. {
  569. struct netlbl_cipsov4_doiwalk_arg cb_arg;
  570. int doi_skip = cb->args[0];
  571. cb_arg.nl_cb = cb;
  572. cb_arg.skb = skb;
  573. cb_arg.seq = cb->nlh->nlmsg_seq;
  574. cipso_v4_doi_walk(&doi_skip, netlbl_cipsov4_listall_cb, &cb_arg);
  575. cb->args[0] = doi_skip;
  576. return skb->len;
  577. }
  578. /**
  579. * netlbl_cipsov4_remove - Handle a REMOVE message
  580. * @skb: the NETLINK buffer
  581. * @info: the Generic NETLINK info block
  582. *
  583. * Description:
  584. * Process a user generated REMOVE message and respond accordingly. Returns
  585. * zero on success, negative values on failure.
  586. *
  587. */
  588. static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
  589. {
  590. int ret_val = -EINVAL;
  591. u32 doi;
  592. if (info->attrs[NLBL_CIPSOV4_A_DOI]) {
  593. doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
  594. ret_val = cipso_v4_doi_remove(doi, netlbl_cipsov4_doi_free);
  595. }
  596. return ret_val;
  597. }
  598. /*
  599. * NetLabel Generic NETLINK Command Definitions
  600. */
  601. static struct genl_ops netlbl_cipsov4_genl_c_add = {
  602. .cmd = NLBL_CIPSOV4_C_ADD,
  603. .flags = GENL_ADMIN_PERM,
  604. .policy = netlbl_cipsov4_genl_policy,
  605. .doit = netlbl_cipsov4_add,
  606. .dumpit = NULL,
  607. };
  608. static struct genl_ops netlbl_cipsov4_genl_c_remove = {
  609. .cmd = NLBL_CIPSOV4_C_REMOVE,
  610. .flags = GENL_ADMIN_PERM,
  611. .policy = netlbl_cipsov4_genl_policy,
  612. .doit = netlbl_cipsov4_remove,
  613. .dumpit = NULL,
  614. };
  615. static struct genl_ops netlbl_cipsov4_genl_c_list = {
  616. .cmd = NLBL_CIPSOV4_C_LIST,
  617. .flags = 0,
  618. .policy = netlbl_cipsov4_genl_policy,
  619. .doit = netlbl_cipsov4_list,
  620. .dumpit = NULL,
  621. };
  622. static struct genl_ops netlbl_cipsov4_genl_c_listall = {
  623. .cmd = NLBL_CIPSOV4_C_LISTALL,
  624. .flags = 0,
  625. .policy = netlbl_cipsov4_genl_policy,
  626. .doit = NULL,
  627. .dumpit = netlbl_cipsov4_listall,
  628. };
  629. /*
  630. * NetLabel Generic NETLINK Protocol Functions
  631. */
  632. /**
  633. * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
  634. *
  635. * Description:
  636. * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
  637. * mechanism. Returns zero on success, negative values on failure.
  638. *
  639. */
  640. int netlbl_cipsov4_genl_init(void)
  641. {
  642. int ret_val;
  643. ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
  644. if (ret_val != 0)
  645. return ret_val;
  646. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  647. &netlbl_cipsov4_genl_c_add);
  648. if (ret_val != 0)
  649. return ret_val;
  650. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  651. &netlbl_cipsov4_genl_c_remove);
  652. if (ret_val != 0)
  653. return ret_val;
  654. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  655. &netlbl_cipsov4_genl_c_list);
  656. if (ret_val != 0)
  657. return ret_val;
  658. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  659. &netlbl_cipsov4_genl_c_listall);
  660. if (ret_val != 0)
  661. return ret_val;
  662. return 0;
  663. }