netlabel_mgmt.c 15 KB

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