netlabel_mgmt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. return genlmsg_reply(ans_skb, info);
  346. listdef_failure_lock:
  347. rcu_read_unlock();
  348. listdef_failure:
  349. kfree_skb(ans_skb);
  350. return ret_val;
  351. }
  352. /**
  353. * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
  354. * @skb: the skb to write to
  355. * @seq: the NETLINK sequence number
  356. * @cb: the NETLINK callback
  357. * @protocol: the NetLabel protocol to use in the message
  358. *
  359. * Description:
  360. * This function is to be used in conjunction with netlbl_mgmt_protocols() to
  361. * answer a application's PROTOCOLS message. Returns the size of the message
  362. * on success, negative values on failure.
  363. *
  364. */
  365. static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
  366. struct netlink_callback *cb,
  367. u32 protocol)
  368. {
  369. int ret_val = -ENOMEM;
  370. void *data;
  371. data = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  372. &netlbl_mgmt_gnl_family, NLM_F_MULTI,
  373. NLBL_MGMT_C_PROTOCOLS);
  374. if (data == NULL)
  375. goto protocols_cb_failure;
  376. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
  377. if (ret_val != 0)
  378. goto protocols_cb_failure;
  379. return genlmsg_end(skb, data);
  380. protocols_cb_failure:
  381. genlmsg_cancel(skb, data);
  382. return ret_val;
  383. }
  384. /**
  385. * netlbl_mgmt_protocols - Handle a PROTOCOLS message
  386. * @skb: the NETLINK buffer
  387. * @cb: the NETLINK callback
  388. *
  389. * Description:
  390. * Process a user generated PROTOCOLS message and respond accordingly.
  391. *
  392. */
  393. static int netlbl_mgmt_protocols(struct sk_buff *skb,
  394. struct netlink_callback *cb)
  395. {
  396. u32 protos_sent = cb->args[0];
  397. if (protos_sent == 0) {
  398. if (netlbl_mgmt_protocols_cb(skb,
  399. cb,
  400. NETLBL_NLTYPE_UNLABELED) < 0)
  401. goto protocols_return;
  402. protos_sent++;
  403. }
  404. if (protos_sent == 1) {
  405. if (netlbl_mgmt_protocols_cb(skb,
  406. cb,
  407. NETLBL_NLTYPE_CIPSOV4) < 0)
  408. goto protocols_return;
  409. protos_sent++;
  410. }
  411. protocols_return:
  412. cb->args[0] = protos_sent;
  413. return skb->len;
  414. }
  415. /**
  416. * netlbl_mgmt_version - Handle a VERSION message
  417. * @skb: the NETLINK buffer
  418. * @info: the Generic NETLINK info block
  419. *
  420. * Description:
  421. * Process a user generated VERSION message and respond accordingly. Returns
  422. * zero on success, negative values on failure.
  423. *
  424. */
  425. static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
  426. {
  427. int ret_val = -ENOMEM;
  428. struct sk_buff *ans_skb = NULL;
  429. void *data;
  430. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  431. if (ans_skb == NULL)
  432. return -ENOMEM;
  433. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  434. 0, NLBL_MGMT_C_VERSION);
  435. if (data == NULL)
  436. goto version_failure;
  437. ret_val = nla_put_u32(ans_skb,
  438. NLBL_MGMT_A_VERSION,
  439. NETLBL_PROTO_VERSION);
  440. if (ret_val != 0)
  441. goto version_failure;
  442. genlmsg_end(ans_skb, data);
  443. return genlmsg_reply(ans_skb, info);
  444. version_failure:
  445. kfree_skb(ans_skb);
  446. return ret_val;
  447. }
  448. /*
  449. * NetLabel Generic NETLINK Command Definitions
  450. */
  451. static struct genl_ops netlbl_mgmt_genl_ops[] = {
  452. {
  453. .cmd = NLBL_MGMT_C_ADD,
  454. .flags = GENL_ADMIN_PERM,
  455. .policy = netlbl_mgmt_genl_policy,
  456. .doit = netlbl_mgmt_add,
  457. .dumpit = NULL,
  458. },
  459. {
  460. .cmd = NLBL_MGMT_C_REMOVE,
  461. .flags = GENL_ADMIN_PERM,
  462. .policy = netlbl_mgmt_genl_policy,
  463. .doit = netlbl_mgmt_remove,
  464. .dumpit = NULL,
  465. },
  466. {
  467. .cmd = NLBL_MGMT_C_LISTALL,
  468. .flags = 0,
  469. .policy = netlbl_mgmt_genl_policy,
  470. .doit = NULL,
  471. .dumpit = netlbl_mgmt_listall,
  472. },
  473. {
  474. .cmd = NLBL_MGMT_C_ADDDEF,
  475. .flags = GENL_ADMIN_PERM,
  476. .policy = netlbl_mgmt_genl_policy,
  477. .doit = netlbl_mgmt_adddef,
  478. .dumpit = NULL,
  479. },
  480. {
  481. .cmd = NLBL_MGMT_C_REMOVEDEF,
  482. .flags = GENL_ADMIN_PERM,
  483. .policy = netlbl_mgmt_genl_policy,
  484. .doit = netlbl_mgmt_removedef,
  485. .dumpit = NULL,
  486. },
  487. {
  488. .cmd = NLBL_MGMT_C_LISTDEF,
  489. .flags = 0,
  490. .policy = netlbl_mgmt_genl_policy,
  491. .doit = netlbl_mgmt_listdef,
  492. .dumpit = NULL,
  493. },
  494. {
  495. .cmd = NLBL_MGMT_C_PROTOCOLS,
  496. .flags = 0,
  497. .policy = netlbl_mgmt_genl_policy,
  498. .doit = NULL,
  499. .dumpit = netlbl_mgmt_protocols,
  500. },
  501. {
  502. .cmd = NLBL_MGMT_C_VERSION,
  503. .flags = 0,
  504. .policy = netlbl_mgmt_genl_policy,
  505. .doit = netlbl_mgmt_version,
  506. .dumpit = NULL,
  507. },
  508. };
  509. /*
  510. * NetLabel Generic NETLINK Protocol Functions
  511. */
  512. /**
  513. * netlbl_mgmt_genl_init - Register the NetLabel management component
  514. *
  515. * Description:
  516. * Register the NetLabel management component with the Generic NETLINK
  517. * mechanism. Returns zero on success, negative values on failure.
  518. *
  519. */
  520. int __init netlbl_mgmt_genl_init(void)
  521. {
  522. int ret_val, i;
  523. ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
  524. if (ret_val != 0)
  525. return ret_val;
  526. for (i = 0; i < ARRAY_SIZE(netlbl_mgmt_genl_ops); i++) {
  527. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  528. &netlbl_mgmt_genl_ops[i]);
  529. if (ret_val != 0)
  530. return ret_val;
  531. }
  532. return 0;
  533. }