netlabel_mgmt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * NetLabel Management Support
  3. *
  4. * This file defines the management 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_domainhash.h"
  39. #include "netlabel_user.h"
  40. #include "netlabel_mgmt.h"
  41. /* NetLabel Generic NETLINK CIPSOv4 family */
  42. static struct genl_family netlbl_mgmt_gnl_family = {
  43. .id = GENL_ID_GENERATE,
  44. .hdrsize = 0,
  45. .name = NETLBL_NLTYPE_MGMT_NAME,
  46. .version = NETLBL_PROTO_VERSION,
  47. .maxattr = 0,
  48. };
  49. /*
  50. * NetLabel Command Handlers
  51. */
  52. /**
  53. * netlbl_mgmt_add - Handle an ADD message
  54. * @skb: the NETLINK buffer
  55. * @info: the Generic NETLINK info block
  56. *
  57. * Description:
  58. * Process a user generated ADD message and add the domains from the message
  59. * to the hash table. See netlabel.h for a description of the message format.
  60. * Returns zero on success, negative values on failure.
  61. *
  62. */
  63. static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
  64. {
  65. int ret_val = -EINVAL;
  66. struct nlattr *msg_ptr = netlbl_netlink_payload_data(skb);
  67. int msg_len = netlbl_netlink_payload_len(skb);
  68. u32 count;
  69. struct netlbl_dom_map *entry = NULL;
  70. u32 iter;
  71. u32 tmp_val;
  72. int tmp_size;
  73. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  74. if (ret_val != 0)
  75. goto add_failure;
  76. if (msg_len < NETLBL_LEN_U32)
  77. goto add_failure;
  78. count = netlbl_getinc_u32(&msg_ptr, &msg_len);
  79. for (iter = 0; iter < count && msg_len > 0; iter++, entry = NULL) {
  80. if (msg_len <= 0) {
  81. ret_val = -EINVAL;
  82. goto add_failure;
  83. }
  84. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  85. if (entry == NULL) {
  86. ret_val = -ENOMEM;
  87. goto add_failure;
  88. }
  89. tmp_size = nla_len(msg_ptr);
  90. if (tmp_size <= 0 || tmp_size > msg_len) {
  91. ret_val = -EINVAL;
  92. goto add_failure;
  93. }
  94. entry->domain = kmalloc(tmp_size, GFP_KERNEL);
  95. if (entry->domain == NULL) {
  96. ret_val = -ENOMEM;
  97. goto add_failure;
  98. }
  99. nla_strlcpy(entry->domain, msg_ptr, tmp_size);
  100. entry->domain[tmp_size - 1] = '\0';
  101. msg_ptr = nla_next(msg_ptr, &msg_len);
  102. if (msg_len < NETLBL_LEN_U32) {
  103. ret_val = -EINVAL;
  104. goto add_failure;
  105. }
  106. tmp_val = netlbl_getinc_u32(&msg_ptr, &msg_len);
  107. entry->type = tmp_val;
  108. switch (tmp_val) {
  109. case NETLBL_NLTYPE_UNLABELED:
  110. ret_val = netlbl_domhsh_add(entry);
  111. break;
  112. case NETLBL_NLTYPE_CIPSOV4:
  113. if (msg_len < NETLBL_LEN_U32) {
  114. ret_val = -EINVAL;
  115. goto add_failure;
  116. }
  117. tmp_val = netlbl_getinc_u32(&msg_ptr, &msg_len);
  118. /* We should be holding a rcu_read_lock() here
  119. * while we hold the result but since the entry
  120. * will always be deleted when the CIPSO DOI
  121. * is deleted we aren't going to keep the lock. */
  122. rcu_read_lock();
  123. entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
  124. if (entry->type_def.cipsov4 == NULL) {
  125. rcu_read_unlock();
  126. ret_val = -EINVAL;
  127. goto add_failure;
  128. }
  129. ret_val = netlbl_domhsh_add(entry);
  130. rcu_read_unlock();
  131. break;
  132. default:
  133. ret_val = -EINVAL;
  134. }
  135. if (ret_val != 0)
  136. goto add_failure;
  137. }
  138. netlbl_netlink_send_ack(info,
  139. netlbl_mgmt_gnl_family.id,
  140. NLBL_MGMT_C_ACK,
  141. NETLBL_E_OK);
  142. return 0;
  143. add_failure:
  144. if (entry)
  145. kfree(entry->domain);
  146. kfree(entry);
  147. netlbl_netlink_send_ack(info,
  148. netlbl_mgmt_gnl_family.id,
  149. NLBL_MGMT_C_ACK,
  150. -ret_val);
  151. return ret_val;
  152. }
  153. /**
  154. * netlbl_mgmt_remove - Handle a REMOVE message
  155. * @skb: the NETLINK buffer
  156. * @info: the Generic NETLINK info block
  157. *
  158. * Description:
  159. * Process a user generated REMOVE message and remove the specified domain
  160. * mappings. Returns zero on success, negative values on failure.
  161. *
  162. */
  163. static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
  164. {
  165. int ret_val = -EINVAL;
  166. struct nlattr *msg_ptr = netlbl_netlink_payload_data(skb);
  167. int msg_len = netlbl_netlink_payload_len(skb);
  168. u32 count;
  169. u32 iter;
  170. int tmp_size;
  171. unsigned char *domain;
  172. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  173. if (ret_val != 0)
  174. goto remove_return;
  175. if (msg_len < NETLBL_LEN_U32)
  176. goto remove_return;
  177. count = netlbl_getinc_u32(&msg_ptr, &msg_len);
  178. for (iter = 0; iter < count && msg_len > 0; iter++) {
  179. if (msg_len <= 0) {
  180. ret_val = -EINVAL;
  181. goto remove_return;
  182. }
  183. tmp_size = nla_len(msg_ptr);
  184. domain = nla_data(msg_ptr);
  185. if (tmp_size <= 0 || tmp_size > msg_len ||
  186. domain[tmp_size - 1] != '\0') {
  187. ret_val = -EINVAL;
  188. goto remove_return;
  189. }
  190. ret_val = netlbl_domhsh_remove(domain);
  191. if (ret_val != 0)
  192. goto remove_return;
  193. msg_ptr = nla_next(msg_ptr, &msg_len);
  194. }
  195. ret_val = 0;
  196. remove_return:
  197. netlbl_netlink_send_ack(info,
  198. netlbl_mgmt_gnl_family.id,
  199. NLBL_MGMT_C_ACK,
  200. -ret_val);
  201. return ret_val;
  202. }
  203. /**
  204. * netlbl_mgmt_list - Handle a LIST message
  205. * @skb: the NETLINK buffer
  206. * @info: the Generic NETLINK info block
  207. *
  208. * Description:
  209. * Process a user generated LIST message and dumps the domain hash table in a
  210. * form suitable for use in a kernel generated LIST message. Returns zero on
  211. * success, negative values on failure.
  212. *
  213. */
  214. static int netlbl_mgmt_list(struct sk_buff *skb, struct genl_info *info)
  215. {
  216. int ret_val = -ENOMEM;
  217. struct sk_buff *ans_skb;
  218. ans_skb = netlbl_domhsh_dump(NLMSG_SPACE(GENL_HDRLEN));
  219. if (ans_skb == NULL)
  220. goto list_failure;
  221. netlbl_netlink_hdr_push(ans_skb,
  222. info->snd_pid,
  223. 0,
  224. netlbl_mgmt_gnl_family.id,
  225. NLBL_MGMT_C_LIST);
  226. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  227. if (ret_val != 0)
  228. goto list_failure;
  229. return 0;
  230. list_failure:
  231. netlbl_netlink_send_ack(info,
  232. netlbl_mgmt_gnl_family.id,
  233. NLBL_MGMT_C_ACK,
  234. -ret_val);
  235. return ret_val;
  236. }
  237. /**
  238. * netlbl_mgmt_adddef - Handle an ADDDEF message
  239. * @skb: the NETLINK buffer
  240. * @info: the Generic NETLINK info block
  241. *
  242. * Description:
  243. * Process a user generated ADDDEF message and respond accordingly. Returns
  244. * zero on success, negative values on failure.
  245. *
  246. */
  247. static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
  248. {
  249. int ret_val = -EINVAL;
  250. struct nlattr *msg_ptr = netlbl_netlink_payload_data(skb);
  251. int msg_len = netlbl_netlink_payload_len(skb);
  252. struct netlbl_dom_map *entry = NULL;
  253. u32 tmp_val;
  254. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  255. if (ret_val != 0)
  256. goto adddef_failure;
  257. if (msg_len < NETLBL_LEN_U32)
  258. goto adddef_failure;
  259. tmp_val = netlbl_getinc_u32(&msg_ptr, &msg_len);
  260. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  261. if (entry == NULL) {
  262. ret_val = -ENOMEM;
  263. goto adddef_failure;
  264. }
  265. entry->type = tmp_val;
  266. switch (entry->type) {
  267. case NETLBL_NLTYPE_UNLABELED:
  268. ret_val = netlbl_domhsh_add_default(entry);
  269. break;
  270. case NETLBL_NLTYPE_CIPSOV4:
  271. if (msg_len < NETLBL_LEN_U32) {
  272. ret_val = -EINVAL;
  273. goto adddef_failure;
  274. }
  275. tmp_val = netlbl_getinc_u32(&msg_ptr, &msg_len);
  276. /* We should be holding a rcu_read_lock here while we
  277. * hold the result but since the entry will always be
  278. * deleted when the CIPSO DOI is deleted we are going
  279. * to skip the lock. */
  280. rcu_read_lock();
  281. entry->type_def.cipsov4 = cipso_v4_doi_getdef(tmp_val);
  282. if (entry->type_def.cipsov4 == NULL) {
  283. rcu_read_unlock();
  284. ret_val = -EINVAL;
  285. goto adddef_failure;
  286. }
  287. ret_val = netlbl_domhsh_add_default(entry);
  288. rcu_read_unlock();
  289. break;
  290. default:
  291. ret_val = -EINVAL;
  292. }
  293. if (ret_val != 0)
  294. goto adddef_failure;
  295. netlbl_netlink_send_ack(info,
  296. netlbl_mgmt_gnl_family.id,
  297. NLBL_MGMT_C_ACK,
  298. NETLBL_E_OK);
  299. return 0;
  300. adddef_failure:
  301. kfree(entry);
  302. netlbl_netlink_send_ack(info,
  303. netlbl_mgmt_gnl_family.id,
  304. NLBL_MGMT_C_ACK,
  305. -ret_val);
  306. return ret_val;
  307. }
  308. /**
  309. * netlbl_mgmt_removedef - Handle a REMOVEDEF message
  310. * @skb: the NETLINK buffer
  311. * @info: the Generic NETLINK info block
  312. *
  313. * Description:
  314. * Process a user generated REMOVEDEF message and remove the default domain
  315. * mapping. Returns zero on success, negative values on failure.
  316. *
  317. */
  318. static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
  319. {
  320. int ret_val;
  321. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  322. if (ret_val != 0)
  323. goto removedef_return;
  324. ret_val = netlbl_domhsh_remove_default();
  325. removedef_return:
  326. netlbl_netlink_send_ack(info,
  327. netlbl_mgmt_gnl_family.id,
  328. NLBL_MGMT_C_ACK,
  329. -ret_val);
  330. return ret_val;
  331. }
  332. /**
  333. * netlbl_mgmt_listdef - Handle a LISTDEF message
  334. * @skb: the NETLINK buffer
  335. * @info: the Generic NETLINK info block
  336. *
  337. * Description:
  338. * Process a user generated LISTDEF message and dumps the default domain
  339. * mapping in a form suitable for use in a kernel generated LISTDEF message.
  340. * Returns zero on success, negative values on failure.
  341. *
  342. */
  343. static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
  344. {
  345. int ret_val = -ENOMEM;
  346. struct sk_buff *ans_skb;
  347. ans_skb = netlbl_domhsh_dump_default(NLMSG_SPACE(GENL_HDRLEN));
  348. if (ans_skb == NULL)
  349. goto listdef_failure;
  350. netlbl_netlink_hdr_push(ans_skb,
  351. info->snd_pid,
  352. 0,
  353. netlbl_mgmt_gnl_family.id,
  354. NLBL_MGMT_C_LISTDEF);
  355. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  356. if (ret_val != 0)
  357. goto listdef_failure;
  358. return 0;
  359. listdef_failure:
  360. netlbl_netlink_send_ack(info,
  361. netlbl_mgmt_gnl_family.id,
  362. NLBL_MGMT_C_ACK,
  363. -ret_val);
  364. return ret_val;
  365. }
  366. /**
  367. * netlbl_mgmt_modules - Handle a MODULES message
  368. * @skb: the NETLINK buffer
  369. * @info: the Generic NETLINK info block
  370. *
  371. * Description:
  372. * Process a user generated MODULES message and respond accordingly.
  373. *
  374. */
  375. static int netlbl_mgmt_modules(struct sk_buff *skb, struct genl_info *info)
  376. {
  377. int ret_val = -ENOMEM;
  378. size_t data_size;
  379. u32 mod_count;
  380. struct sk_buff *ans_skb = NULL;
  381. /* unlabeled + cipsov4 */
  382. mod_count = 2;
  383. data_size = GENL_HDRLEN + NETLBL_LEN_U32 + mod_count * NETLBL_LEN_U32;
  384. ans_skb = netlbl_netlink_alloc_skb(0, data_size, GFP_KERNEL);
  385. if (ans_skb == NULL)
  386. goto modules_failure;
  387. if (netlbl_netlink_hdr_put(ans_skb,
  388. info->snd_pid,
  389. 0,
  390. netlbl_mgmt_gnl_family.id,
  391. NLBL_MGMT_C_MODULES) == NULL)
  392. goto modules_failure;
  393. ret_val = nla_put_u32(ans_skb, NLA_U32, mod_count);
  394. if (ret_val != 0)
  395. goto modules_failure;
  396. ret_val = nla_put_u32(ans_skb, NLA_U32, NETLBL_NLTYPE_UNLABELED);
  397. if (ret_val != 0)
  398. goto modules_failure;
  399. ret_val = nla_put_u32(ans_skb, NLA_U32, NETLBL_NLTYPE_CIPSOV4);
  400. if (ret_val != 0)
  401. goto modules_failure;
  402. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  403. if (ret_val != 0)
  404. goto modules_failure;
  405. return 0;
  406. modules_failure:
  407. kfree_skb(ans_skb);
  408. netlbl_netlink_send_ack(info,
  409. netlbl_mgmt_gnl_family.id,
  410. NLBL_MGMT_C_ACK,
  411. -ret_val);
  412. return ret_val;
  413. }
  414. /**
  415. * netlbl_mgmt_version - Handle a VERSION message
  416. * @skb: the NETLINK buffer
  417. * @info: the Generic NETLINK info block
  418. *
  419. * Description:
  420. * Process a user generated VERSION message and respond accordingly. Returns
  421. * zero on success, negative values on failure.
  422. *
  423. */
  424. static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
  425. {
  426. int ret_val = -ENOMEM;
  427. struct sk_buff *ans_skb = NULL;
  428. ans_skb = netlbl_netlink_alloc_skb(0,
  429. GENL_HDRLEN + NETLBL_LEN_U32,
  430. GFP_KERNEL);
  431. if (ans_skb == NULL)
  432. goto version_failure;
  433. if (netlbl_netlink_hdr_put(ans_skb,
  434. info->snd_pid,
  435. 0,
  436. netlbl_mgmt_gnl_family.id,
  437. NLBL_MGMT_C_VERSION) == NULL)
  438. goto version_failure;
  439. ret_val = nla_put_u32(ans_skb, NLA_U32, NETLBL_PROTO_VERSION);
  440. if (ret_val != 0)
  441. goto version_failure;
  442. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  443. if (ret_val != 0)
  444. goto version_failure;
  445. return 0;
  446. version_failure:
  447. kfree_skb(ans_skb);
  448. netlbl_netlink_send_ack(info,
  449. netlbl_mgmt_gnl_family.id,
  450. NLBL_MGMT_C_ACK,
  451. -ret_val);
  452. return ret_val;
  453. }
  454. /*
  455. * NetLabel Generic NETLINK Command Definitions
  456. */
  457. static struct genl_ops netlbl_mgmt_genl_c_add = {
  458. .cmd = NLBL_MGMT_C_ADD,
  459. .flags = 0,
  460. .doit = netlbl_mgmt_add,
  461. .dumpit = NULL,
  462. };
  463. static struct genl_ops netlbl_mgmt_genl_c_remove = {
  464. .cmd = NLBL_MGMT_C_REMOVE,
  465. .flags = 0,
  466. .doit = netlbl_mgmt_remove,
  467. .dumpit = NULL,
  468. };
  469. static struct genl_ops netlbl_mgmt_genl_c_list = {
  470. .cmd = NLBL_MGMT_C_LIST,
  471. .flags = 0,
  472. .doit = netlbl_mgmt_list,
  473. .dumpit = NULL,
  474. };
  475. static struct genl_ops netlbl_mgmt_genl_c_adddef = {
  476. .cmd = NLBL_MGMT_C_ADDDEF,
  477. .flags = 0,
  478. .doit = netlbl_mgmt_adddef,
  479. .dumpit = NULL,
  480. };
  481. static struct genl_ops netlbl_mgmt_genl_c_removedef = {
  482. .cmd = NLBL_MGMT_C_REMOVEDEF,
  483. .flags = 0,
  484. .doit = netlbl_mgmt_removedef,
  485. .dumpit = NULL,
  486. };
  487. static struct genl_ops netlbl_mgmt_genl_c_listdef = {
  488. .cmd = NLBL_MGMT_C_LISTDEF,
  489. .flags = 0,
  490. .doit = netlbl_mgmt_listdef,
  491. .dumpit = NULL,
  492. };
  493. static struct genl_ops netlbl_mgmt_genl_c_modules = {
  494. .cmd = NLBL_MGMT_C_MODULES,
  495. .flags = 0,
  496. .doit = netlbl_mgmt_modules,
  497. .dumpit = NULL,
  498. };
  499. static struct genl_ops netlbl_mgmt_genl_c_version = {
  500. .cmd = NLBL_MGMT_C_VERSION,
  501. .flags = 0,
  502. .doit = netlbl_mgmt_version,
  503. .dumpit = NULL,
  504. };
  505. /*
  506. * NetLabel Generic NETLINK Protocol Functions
  507. */
  508. /**
  509. * netlbl_mgmt_genl_init - Register the NetLabel management component
  510. *
  511. * Description:
  512. * Register the NetLabel management component with the Generic NETLINK
  513. * mechanism. Returns zero on success, negative values on failure.
  514. *
  515. */
  516. int netlbl_mgmt_genl_init(void)
  517. {
  518. int ret_val;
  519. ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
  520. if (ret_val != 0)
  521. return ret_val;
  522. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  523. &netlbl_mgmt_genl_c_add);
  524. if (ret_val != 0)
  525. return ret_val;
  526. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  527. &netlbl_mgmt_genl_c_remove);
  528. if (ret_val != 0)
  529. return ret_val;
  530. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  531. &netlbl_mgmt_genl_c_list);
  532. if (ret_val != 0)
  533. return ret_val;
  534. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  535. &netlbl_mgmt_genl_c_adddef);
  536. if (ret_val != 0)
  537. return ret_val;
  538. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  539. &netlbl_mgmt_genl_c_removedef);
  540. if (ret_val != 0)
  541. return ret_val;
  542. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  543. &netlbl_mgmt_genl_c_listdef);
  544. if (ret_val != 0)
  545. return ret_val;
  546. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  547. &netlbl_mgmt_genl_c_modules);
  548. if (ret_val != 0)
  549. return ret_val;
  550. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  551. &netlbl_mgmt_genl_c_version);
  552. if (ret_val != 0)
  553. return ret_val;
  554. return 0;
  555. }