netlabel_cipso_v4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. /* NetLabel Generic NETLINK CIPSOv4 family */
  41. static struct genl_family netlbl_cipsov4_gnl_family = {
  42. .id = GENL_ID_GENERATE,
  43. .hdrsize = 0,
  44. .name = NETLBL_NLTYPE_CIPSOV4_NAME,
  45. .version = NETLBL_PROTO_VERSION,
  46. .maxattr = 0,
  47. };
  48. /*
  49. * Helper Functions
  50. */
  51. /**
  52. * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
  53. * @entry: the entry's RCU field
  54. *
  55. * Description:
  56. * This function is designed to be used as a callback to the call_rcu()
  57. * function so that the memory allocated to the DOI definition can be released
  58. * safely.
  59. *
  60. */
  61. static void netlbl_cipsov4_doi_free(struct rcu_head *entry)
  62. {
  63. struct cipso_v4_doi *ptr;
  64. ptr = container_of(entry, struct cipso_v4_doi, rcu);
  65. switch (ptr->type) {
  66. case CIPSO_V4_MAP_STD:
  67. kfree(ptr->map.std->lvl.cipso);
  68. kfree(ptr->map.std->lvl.local);
  69. kfree(ptr->map.std->cat.cipso);
  70. kfree(ptr->map.std->cat.local);
  71. break;
  72. }
  73. kfree(ptr);
  74. }
  75. /*
  76. * NetLabel Command Handlers
  77. */
  78. /**
  79. * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
  80. * @doi: the DOI value
  81. * @msg: the ADD message data
  82. * @msg_size: the size of the ADD message buffer
  83. *
  84. * Description:
  85. * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
  86. * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
  87. * error.
  88. *
  89. */
  90. static int netlbl_cipsov4_add_std(u32 doi, struct nlattr *msg, size_t msg_size)
  91. {
  92. int ret_val = -EINVAL;
  93. int msg_len = msg_size;
  94. u32 num_tags;
  95. u32 num_lvls;
  96. u32 num_cats;
  97. struct cipso_v4_doi *doi_def = NULL;
  98. u32 iter;
  99. u32 tmp_val_a;
  100. u32 tmp_val_b;
  101. if (msg_len < NETLBL_LEN_U32)
  102. goto add_std_failure;
  103. num_tags = netlbl_getinc_u32(&msg, &msg_len);
  104. if (num_tags == 0 || num_tags > CIPSO_V4_TAG_MAXCNT)
  105. goto add_std_failure;
  106. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  107. if (doi_def == NULL) {
  108. ret_val = -ENOMEM;
  109. goto add_std_failure;
  110. }
  111. doi_def->map.std = kzalloc(sizeof(*doi_def->map.std), GFP_KERNEL);
  112. if (doi_def->map.std == NULL) {
  113. ret_val = -ENOMEM;
  114. goto add_std_failure;
  115. }
  116. doi_def->type = CIPSO_V4_MAP_STD;
  117. for (iter = 0; iter < num_tags; iter++) {
  118. if (msg_len < NETLBL_LEN_U8)
  119. goto add_std_failure;
  120. doi_def->tags[iter] = netlbl_getinc_u8(&msg, &msg_len);
  121. switch (doi_def->tags[iter]) {
  122. case CIPSO_V4_TAG_RBITMAP:
  123. break;
  124. default:
  125. goto add_std_failure;
  126. }
  127. }
  128. if (iter < CIPSO_V4_TAG_MAXCNT)
  129. doi_def->tags[iter] = CIPSO_V4_TAG_INVALID;
  130. if (msg_len < 6 * NETLBL_LEN_U32)
  131. goto add_std_failure;
  132. num_lvls = netlbl_getinc_u32(&msg, &msg_len);
  133. if (num_lvls == 0)
  134. goto add_std_failure;
  135. doi_def->map.std->lvl.local_size = netlbl_getinc_u32(&msg, &msg_len);
  136. if (doi_def->map.std->lvl.local_size > CIPSO_V4_MAX_LOC_LVLS)
  137. goto add_std_failure;
  138. doi_def->map.std->lvl.local = kcalloc(doi_def->map.std->lvl.local_size,
  139. sizeof(u32),
  140. GFP_KERNEL);
  141. if (doi_def->map.std->lvl.local == NULL) {
  142. ret_val = -ENOMEM;
  143. goto add_std_failure;
  144. }
  145. doi_def->map.std->lvl.cipso_size = netlbl_getinc_u8(&msg, &msg_len);
  146. if (doi_def->map.std->lvl.cipso_size > CIPSO_V4_MAX_REM_LVLS)
  147. goto add_std_failure;
  148. doi_def->map.std->lvl.cipso = kcalloc(doi_def->map.std->lvl.cipso_size,
  149. sizeof(u32),
  150. GFP_KERNEL);
  151. if (doi_def->map.std->lvl.cipso == NULL) {
  152. ret_val = -ENOMEM;
  153. goto add_std_failure;
  154. }
  155. num_cats = netlbl_getinc_u32(&msg, &msg_len);
  156. doi_def->map.std->cat.local_size = netlbl_getinc_u32(&msg, &msg_len);
  157. if (doi_def->map.std->cat.local_size > CIPSO_V4_MAX_LOC_CATS)
  158. goto add_std_failure;
  159. doi_def->map.std->cat.local = kcalloc(doi_def->map.std->cat.local_size,
  160. sizeof(u32),
  161. GFP_KERNEL);
  162. if (doi_def->map.std->cat.local == NULL) {
  163. ret_val = -ENOMEM;
  164. goto add_std_failure;
  165. }
  166. doi_def->map.std->cat.cipso_size = netlbl_getinc_u16(&msg, &msg_len);
  167. if (doi_def->map.std->cat.cipso_size > CIPSO_V4_MAX_REM_CATS)
  168. goto add_std_failure;
  169. doi_def->map.std->cat.cipso = kcalloc(doi_def->map.std->cat.cipso_size,
  170. sizeof(u32),
  171. GFP_KERNEL);
  172. if (doi_def->map.std->cat.cipso == NULL) {
  173. ret_val = -ENOMEM;
  174. goto add_std_failure;
  175. }
  176. if (msg_len <
  177. num_lvls * (NETLBL_LEN_U32 + NETLBL_LEN_U8) +
  178. num_cats * (NETLBL_LEN_U32 + NETLBL_LEN_U16))
  179. goto add_std_failure;
  180. for (iter = 0; iter < doi_def->map.std->lvl.cipso_size; iter++)
  181. doi_def->map.std->lvl.cipso[iter] = CIPSO_V4_INV_LVL;
  182. for (iter = 0; iter < doi_def->map.std->lvl.local_size; iter++)
  183. doi_def->map.std->lvl.local[iter] = CIPSO_V4_INV_LVL;
  184. for (iter = 0; iter < doi_def->map.std->cat.cipso_size; iter++)
  185. doi_def->map.std->cat.cipso[iter] = CIPSO_V4_INV_CAT;
  186. for (iter = 0; iter < doi_def->map.std->cat.local_size; iter++)
  187. doi_def->map.std->cat.local[iter] = CIPSO_V4_INV_CAT;
  188. for (iter = 0; iter < num_lvls; iter++) {
  189. tmp_val_a = netlbl_getinc_u32(&msg, &msg_len);
  190. tmp_val_b = netlbl_getinc_u8(&msg, &msg_len);
  191. if (tmp_val_a >= doi_def->map.std->lvl.local_size ||
  192. tmp_val_b >= doi_def->map.std->lvl.cipso_size)
  193. goto add_std_failure;
  194. doi_def->map.std->lvl.cipso[tmp_val_b] = tmp_val_a;
  195. doi_def->map.std->lvl.local[tmp_val_a] = tmp_val_b;
  196. }
  197. for (iter = 0; iter < num_cats; iter++) {
  198. tmp_val_a = netlbl_getinc_u32(&msg, &msg_len);
  199. tmp_val_b = netlbl_getinc_u16(&msg, &msg_len);
  200. if (tmp_val_a >= doi_def->map.std->cat.local_size ||
  201. tmp_val_b >= doi_def->map.std->cat.cipso_size)
  202. goto add_std_failure;
  203. doi_def->map.std->cat.cipso[tmp_val_b] = tmp_val_a;
  204. doi_def->map.std->cat.local[tmp_val_a] = tmp_val_b;
  205. }
  206. doi_def->doi = doi;
  207. ret_val = cipso_v4_doi_add(doi_def);
  208. if (ret_val != 0)
  209. goto add_std_failure;
  210. return 0;
  211. add_std_failure:
  212. if (doi_def)
  213. netlbl_cipsov4_doi_free(&doi_def->rcu);
  214. return ret_val;
  215. }
  216. /**
  217. * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
  218. * @doi: the DOI value
  219. * @msg: the ADD message data
  220. * @msg_size: the size of the ADD message buffer
  221. *
  222. * Description:
  223. * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
  224. * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
  225. * error.
  226. *
  227. */
  228. static int netlbl_cipsov4_add_pass(u32 doi,
  229. struct nlattr *msg,
  230. size_t msg_size)
  231. {
  232. int ret_val = -EINVAL;
  233. int msg_len = msg_size;
  234. u32 num_tags;
  235. struct cipso_v4_doi *doi_def = NULL;
  236. u32 iter;
  237. if (msg_len < NETLBL_LEN_U32)
  238. goto add_pass_failure;
  239. num_tags = netlbl_getinc_u32(&msg, &msg_len);
  240. if (num_tags == 0 || num_tags > CIPSO_V4_TAG_MAXCNT)
  241. goto add_pass_failure;
  242. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  243. if (doi_def == NULL) {
  244. ret_val = -ENOMEM;
  245. goto add_pass_failure;
  246. }
  247. doi_def->type = CIPSO_V4_MAP_PASS;
  248. for (iter = 0; iter < num_tags; iter++) {
  249. if (msg_len < NETLBL_LEN_U8)
  250. goto add_pass_failure;
  251. doi_def->tags[iter] = netlbl_getinc_u8(&msg, &msg_len);
  252. switch (doi_def->tags[iter]) {
  253. case CIPSO_V4_TAG_RBITMAP:
  254. break;
  255. default:
  256. goto add_pass_failure;
  257. }
  258. }
  259. if (iter < CIPSO_V4_TAG_MAXCNT)
  260. doi_def->tags[iter] = CIPSO_V4_TAG_INVALID;
  261. doi_def->doi = doi;
  262. ret_val = cipso_v4_doi_add(doi_def);
  263. if (ret_val != 0)
  264. goto add_pass_failure;
  265. return 0;
  266. add_pass_failure:
  267. if (doi_def)
  268. netlbl_cipsov4_doi_free(&doi_def->rcu);
  269. return ret_val;
  270. }
  271. /**
  272. * netlbl_cipsov4_add - Handle an ADD message
  273. * @skb: the NETLINK buffer
  274. * @info: the Generic NETLINK info block
  275. *
  276. * Description:
  277. * Create a new DOI definition based on the given ADD message and add it to the
  278. * CIPSO V4 engine. Returns zero on success, negative values on failure.
  279. *
  280. */
  281. static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
  282. {
  283. int ret_val = -EINVAL;
  284. u32 doi;
  285. u32 map_type;
  286. int msg_len = netlbl_netlink_payload_len(skb);
  287. struct nlattr *msg = netlbl_netlink_payload_data(skb);
  288. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  289. if (ret_val != 0)
  290. goto add_return;
  291. if (msg_len < 2 * NETLBL_LEN_U32)
  292. goto add_return;
  293. doi = netlbl_getinc_u32(&msg, &msg_len);
  294. map_type = netlbl_getinc_u32(&msg, &msg_len);
  295. switch (map_type) {
  296. case CIPSO_V4_MAP_STD:
  297. ret_val = netlbl_cipsov4_add_std(doi, msg, msg_len);
  298. break;
  299. case CIPSO_V4_MAP_PASS:
  300. ret_val = netlbl_cipsov4_add_pass(doi, msg, msg_len);
  301. break;
  302. }
  303. add_return:
  304. netlbl_netlink_send_ack(info,
  305. netlbl_cipsov4_gnl_family.id,
  306. NLBL_CIPSOV4_C_ACK,
  307. -ret_val);
  308. return ret_val;
  309. }
  310. /**
  311. * netlbl_cipsov4_list - Handle a LIST message
  312. * @skb: the NETLINK buffer
  313. * @info: the Generic NETLINK info block
  314. *
  315. * Description:
  316. * Process a user generated LIST message and respond accordingly. Returns
  317. * zero on success and negative values on error.
  318. *
  319. */
  320. static int netlbl_cipsov4_list(struct sk_buff *skb, struct genl_info *info)
  321. {
  322. int ret_val = -EINVAL;
  323. u32 doi;
  324. struct nlattr *msg = netlbl_netlink_payload_data(skb);
  325. struct sk_buff *ans_skb;
  326. if (netlbl_netlink_payload_len(skb) != NETLBL_LEN_U32)
  327. goto list_failure;
  328. doi = nla_get_u32(msg);
  329. ans_skb = cipso_v4_doi_dump(doi, NLMSG_SPACE(GENL_HDRLEN));
  330. if (ans_skb == NULL) {
  331. ret_val = -ENOMEM;
  332. goto list_failure;
  333. }
  334. netlbl_netlink_hdr_push(ans_skb,
  335. info->snd_pid,
  336. 0,
  337. netlbl_cipsov4_gnl_family.id,
  338. NLBL_CIPSOV4_C_LIST);
  339. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  340. if (ret_val != 0)
  341. goto list_failure;
  342. return 0;
  343. list_failure:
  344. netlbl_netlink_send_ack(info,
  345. netlbl_cipsov4_gnl_family.id,
  346. NLBL_CIPSOV4_C_ACK,
  347. -ret_val);
  348. return ret_val;
  349. }
  350. /**
  351. * netlbl_cipsov4_listall - Handle a LISTALL message
  352. * @skb: the NETLINK buffer
  353. * @info: the Generic NETLINK info block
  354. *
  355. * Description:
  356. * Process a user generated LISTALL message and respond accordingly. Returns
  357. * zero on success and negative values on error.
  358. *
  359. */
  360. static int netlbl_cipsov4_listall(struct sk_buff *skb, struct genl_info *info)
  361. {
  362. int ret_val = -EINVAL;
  363. struct sk_buff *ans_skb;
  364. ans_skb = cipso_v4_doi_dump_all(NLMSG_SPACE(GENL_HDRLEN));
  365. if (ans_skb == NULL) {
  366. ret_val = -ENOMEM;
  367. goto listall_failure;
  368. }
  369. netlbl_netlink_hdr_push(ans_skb,
  370. info->snd_pid,
  371. 0,
  372. netlbl_cipsov4_gnl_family.id,
  373. NLBL_CIPSOV4_C_LISTALL);
  374. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  375. if (ret_val != 0)
  376. goto listall_failure;
  377. return 0;
  378. listall_failure:
  379. netlbl_netlink_send_ack(info,
  380. netlbl_cipsov4_gnl_family.id,
  381. NLBL_CIPSOV4_C_ACK,
  382. -ret_val);
  383. return ret_val;
  384. }
  385. /**
  386. * netlbl_cipsov4_remove - Handle a REMOVE message
  387. * @skb: the NETLINK buffer
  388. * @info: the Generic NETLINK info block
  389. *
  390. * Description:
  391. * Process a user generated REMOVE message and respond accordingly. Returns
  392. * zero on success, negative values on failure.
  393. *
  394. */
  395. static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
  396. {
  397. int ret_val;
  398. u32 doi;
  399. struct nlattr *msg = netlbl_netlink_payload_data(skb);
  400. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  401. if (ret_val != 0)
  402. goto remove_return;
  403. if (netlbl_netlink_payload_len(skb) != NETLBL_LEN_U32) {
  404. ret_val = -EINVAL;
  405. goto remove_return;
  406. }
  407. doi = nla_get_u32(msg);
  408. ret_val = cipso_v4_doi_remove(doi, netlbl_cipsov4_doi_free);
  409. remove_return:
  410. netlbl_netlink_send_ack(info,
  411. netlbl_cipsov4_gnl_family.id,
  412. NLBL_CIPSOV4_C_ACK,
  413. -ret_val);
  414. return ret_val;
  415. }
  416. /*
  417. * NetLabel Generic NETLINK Command Definitions
  418. */
  419. static struct genl_ops netlbl_cipsov4_genl_c_add = {
  420. .cmd = NLBL_CIPSOV4_C_ADD,
  421. .flags = 0,
  422. .doit = netlbl_cipsov4_add,
  423. .dumpit = NULL,
  424. };
  425. static struct genl_ops netlbl_cipsov4_genl_c_remove = {
  426. .cmd = NLBL_CIPSOV4_C_REMOVE,
  427. .flags = 0,
  428. .doit = netlbl_cipsov4_remove,
  429. .dumpit = NULL,
  430. };
  431. static struct genl_ops netlbl_cipsov4_genl_c_list = {
  432. .cmd = NLBL_CIPSOV4_C_LIST,
  433. .flags = 0,
  434. .doit = netlbl_cipsov4_list,
  435. .dumpit = NULL,
  436. };
  437. static struct genl_ops netlbl_cipsov4_genl_c_listall = {
  438. .cmd = NLBL_CIPSOV4_C_LISTALL,
  439. .flags = 0,
  440. .doit = netlbl_cipsov4_listall,
  441. .dumpit = NULL,
  442. };
  443. /*
  444. * NetLabel Generic NETLINK Protocol Functions
  445. */
  446. /**
  447. * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
  448. *
  449. * Description:
  450. * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
  451. * mechanism. Returns zero on success, negative values on failure.
  452. *
  453. */
  454. int netlbl_cipsov4_genl_init(void)
  455. {
  456. int ret_val;
  457. ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
  458. if (ret_val != 0)
  459. return ret_val;
  460. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  461. &netlbl_cipsov4_genl_c_add);
  462. if (ret_val != 0)
  463. return ret_val;
  464. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  465. &netlbl_cipsov4_genl_c_remove);
  466. if (ret_val != 0)
  467. return ret_val;
  468. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  469. &netlbl_cipsov4_genl_c_list);
  470. if (ret_val != 0)
  471. return ret_val;
  472. ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
  473. &netlbl_cipsov4_genl_c_listall);
  474. if (ret_val != 0)
  475. return ret_val;
  476. return 0;
  477. }