netlabel_mgmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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, 2008
  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 <linux/in.h>
  34. #include <linux/in6.h>
  35. #include <net/sock.h>
  36. #include <net/netlink.h>
  37. #include <net/genetlink.h>
  38. #include <net/ip.h>
  39. #include <net/ipv6.h>
  40. #include <net/netlabel.h>
  41. #include <net/cipso_ipv4.h>
  42. #include <asm/atomic.h>
  43. #include "netlabel_domainhash.h"
  44. #include "netlabel_user.h"
  45. #include "netlabel_mgmt.h"
  46. /* NetLabel configured protocol counter */
  47. atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0);
  48. /* Argument struct for netlbl_domhsh_walk() */
  49. struct netlbl_domhsh_walk_arg {
  50. struct netlink_callback *nl_cb;
  51. struct sk_buff *skb;
  52. u32 seq;
  53. };
  54. /* NetLabel Generic NETLINK CIPSOv4 family */
  55. static struct genl_family netlbl_mgmt_gnl_family = {
  56. .id = GENL_ID_GENERATE,
  57. .hdrsize = 0,
  58. .name = NETLBL_NLTYPE_MGMT_NAME,
  59. .version = NETLBL_PROTO_VERSION,
  60. .maxattr = NLBL_MGMT_A_MAX,
  61. };
  62. /* NetLabel Netlink attribute policy */
  63. static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
  64. [NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING },
  65. [NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 },
  66. [NLBL_MGMT_A_VERSION] = { .type = NLA_U32 },
  67. [NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 },
  68. };
  69. /*
  70. * Helper Functions
  71. */
  72. /**
  73. * netlbl_mgmt_add - Handle an ADD message
  74. * @info: the Generic NETLINK info block
  75. * @audit_info: NetLabel audit information
  76. *
  77. * Description:
  78. * Helper function for the ADD and ADDDEF messages to add the domain mappings
  79. * from the message to the hash table. See netlabel.h for a description of the
  80. * message format. Returns zero on success, negative values on failure.
  81. *
  82. */
  83. static int netlbl_mgmt_add_common(struct genl_info *info,
  84. struct netlbl_audit *audit_info)
  85. {
  86. int ret_val = -EINVAL;
  87. struct netlbl_dom_map *entry = NULL;
  88. struct netlbl_domaddr_map *addrmap = NULL;
  89. struct cipso_v4_doi *cipsov4 = NULL;
  90. u32 tmp_val;
  91. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  92. if (entry == NULL) {
  93. ret_val = -ENOMEM;
  94. goto add_failure;
  95. }
  96. entry->type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
  97. if (info->attrs[NLBL_MGMT_A_DOMAIN]) {
  98. size_t tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
  99. entry->domain = kmalloc(tmp_size, GFP_KERNEL);
  100. if (entry->domain == NULL) {
  101. ret_val = -ENOMEM;
  102. goto add_failure;
  103. }
  104. nla_strlcpy(entry->domain,
  105. info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
  106. }
  107. /* NOTE: internally we allow/use a entry->type value of
  108. * NETLBL_NLTYPE_ADDRSELECT but we don't currently allow users
  109. * to pass that as a protocol value because we need to know the
  110. * "real" protocol */
  111. switch (entry->type) {
  112. case NETLBL_NLTYPE_UNLABELED:
  113. break;
  114. case NETLBL_NLTYPE_CIPSOV4:
  115. if (!info->attrs[NLBL_MGMT_A_CV4DOI])
  116. goto add_failure;
  117. tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
  118. cipsov4 = cipso_v4_doi_getdef(tmp_val);
  119. if (cipsov4 == NULL)
  120. goto add_failure;
  121. entry->type_def.cipsov4 = cipsov4;
  122. break;
  123. default:
  124. goto add_failure;
  125. }
  126. if (info->attrs[NLBL_MGMT_A_IPV4ADDR]) {
  127. struct in_addr *addr;
  128. struct in_addr *mask;
  129. struct netlbl_domaddr4_map *map;
  130. addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
  131. if (addrmap == NULL) {
  132. ret_val = -ENOMEM;
  133. goto add_failure;
  134. }
  135. INIT_LIST_HEAD(&addrmap->list4);
  136. INIT_LIST_HEAD(&addrmap->list6);
  137. if (nla_len(info->attrs[NLBL_MGMT_A_IPV4ADDR]) !=
  138. sizeof(struct in_addr)) {
  139. ret_val = -EINVAL;
  140. goto add_failure;
  141. }
  142. if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) !=
  143. sizeof(struct in_addr)) {
  144. ret_val = -EINVAL;
  145. goto add_failure;
  146. }
  147. addr = nla_data(info->attrs[NLBL_MGMT_A_IPV4ADDR]);
  148. mask = nla_data(info->attrs[NLBL_MGMT_A_IPV4MASK]);
  149. map = kzalloc(sizeof(*map), GFP_KERNEL);
  150. if (map == NULL) {
  151. ret_val = -ENOMEM;
  152. goto add_failure;
  153. }
  154. map->list.addr = addr->s_addr & mask->s_addr;
  155. map->list.mask = mask->s_addr;
  156. map->list.valid = 1;
  157. map->type = entry->type;
  158. if (cipsov4)
  159. map->type_def.cipsov4 = cipsov4;
  160. ret_val = netlbl_af4list_add(&map->list, &addrmap->list4);
  161. if (ret_val != 0) {
  162. kfree(map);
  163. goto add_failure;
  164. }
  165. entry->type = NETLBL_NLTYPE_ADDRSELECT;
  166. entry->type_def.addrsel = addrmap;
  167. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  168. } else if (info->attrs[NLBL_MGMT_A_IPV6ADDR]) {
  169. struct in6_addr *addr;
  170. struct in6_addr *mask;
  171. struct netlbl_domaddr6_map *map;
  172. addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
  173. if (addrmap == NULL) {
  174. ret_val = -ENOMEM;
  175. goto add_failure;
  176. }
  177. INIT_LIST_HEAD(&addrmap->list4);
  178. INIT_LIST_HEAD(&addrmap->list6);
  179. if (nla_len(info->attrs[NLBL_MGMT_A_IPV6ADDR]) !=
  180. sizeof(struct in6_addr)) {
  181. ret_val = -EINVAL;
  182. goto add_failure;
  183. }
  184. if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) !=
  185. sizeof(struct in6_addr)) {
  186. ret_val = -EINVAL;
  187. goto add_failure;
  188. }
  189. addr = nla_data(info->attrs[NLBL_MGMT_A_IPV6ADDR]);
  190. mask = nla_data(info->attrs[NLBL_MGMT_A_IPV6MASK]);
  191. map = kzalloc(sizeof(*map), GFP_KERNEL);
  192. if (map == NULL) {
  193. ret_val = -ENOMEM;
  194. goto add_failure;
  195. }
  196. ipv6_addr_copy(&map->list.addr, addr);
  197. map->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  198. map->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  199. map->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  200. map->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
  201. ipv6_addr_copy(&map->list.mask, mask);
  202. map->list.valid = 1;
  203. map->type = entry->type;
  204. ret_val = netlbl_af6list_add(&map->list, &addrmap->list6);
  205. if (ret_val != 0) {
  206. kfree(map);
  207. goto add_failure;
  208. }
  209. entry->type = NETLBL_NLTYPE_ADDRSELECT;
  210. entry->type_def.addrsel = addrmap;
  211. #endif /* IPv6 */
  212. }
  213. ret_val = netlbl_domhsh_add(entry, audit_info);
  214. if (ret_val != 0)
  215. goto add_failure;
  216. return 0;
  217. add_failure:
  218. if (cipsov4)
  219. cipso_v4_doi_putdef(cipsov4);
  220. if (entry)
  221. kfree(entry->domain);
  222. kfree(addrmap);
  223. kfree(entry);
  224. return ret_val;
  225. }
  226. /**
  227. * netlbl_mgmt_listentry - List a NetLabel/LSM domain map entry
  228. * @skb: the NETLINK buffer
  229. * @entry: the map entry
  230. *
  231. * Description:
  232. * This function is a helper function used by the LISTALL and LISTDEF command
  233. * handlers. The caller is responsibile for ensuring that the RCU read lock
  234. * is held. Returns zero on success, negative values on failure.
  235. *
  236. */
  237. static int netlbl_mgmt_listentry(struct sk_buff *skb,
  238. struct netlbl_dom_map *entry)
  239. {
  240. int ret_val = 0;
  241. struct nlattr *nla_a;
  242. struct nlattr *nla_b;
  243. struct netlbl_af4list *iter4;
  244. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  245. struct netlbl_af6list *iter6;
  246. #endif
  247. if (entry->domain != NULL) {
  248. ret_val = nla_put_string(skb,
  249. NLBL_MGMT_A_DOMAIN, entry->domain);
  250. if (ret_val != 0)
  251. return ret_val;
  252. }
  253. switch (entry->type) {
  254. case NETLBL_NLTYPE_ADDRSELECT:
  255. nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST);
  256. if (nla_a == NULL)
  257. return -ENOMEM;
  258. netlbl_af4list_foreach_rcu(iter4,
  259. &entry->type_def.addrsel->list4) {
  260. struct netlbl_domaddr4_map *map4;
  261. struct in_addr addr_struct;
  262. nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
  263. if (nla_b == NULL)
  264. return -ENOMEM;
  265. addr_struct.s_addr = iter4->addr;
  266. ret_val = nla_put(skb, NLBL_MGMT_A_IPV4ADDR,
  267. sizeof(struct in_addr),
  268. &addr_struct);
  269. if (ret_val != 0)
  270. return ret_val;
  271. addr_struct.s_addr = iter4->mask;
  272. ret_val = nla_put(skb, NLBL_MGMT_A_IPV4MASK,
  273. sizeof(struct in_addr),
  274. &addr_struct);
  275. if (ret_val != 0)
  276. return ret_val;
  277. map4 = netlbl_domhsh_addr4_entry(iter4);
  278. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
  279. map4->type);
  280. if (ret_val != 0)
  281. return ret_val;
  282. switch (map4->type) {
  283. case NETLBL_NLTYPE_CIPSOV4:
  284. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  285. map4->type_def.cipsov4->doi);
  286. if (ret_val != 0)
  287. return ret_val;
  288. break;
  289. }
  290. nla_nest_end(skb, nla_b);
  291. }
  292. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  293. netlbl_af6list_foreach_rcu(iter6,
  294. &entry->type_def.addrsel->list6) {
  295. struct netlbl_domaddr6_map *map6;
  296. nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
  297. if (nla_b == NULL)
  298. return -ENOMEM;
  299. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6ADDR,
  300. sizeof(struct in6_addr),
  301. &iter6->addr);
  302. if (ret_val != 0)
  303. return ret_val;
  304. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6MASK,
  305. sizeof(struct in6_addr),
  306. &iter6->mask);
  307. if (ret_val != 0)
  308. return ret_val;
  309. map6 = netlbl_domhsh_addr6_entry(iter6);
  310. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
  311. map6->type);
  312. if (ret_val != 0)
  313. return ret_val;
  314. nla_nest_end(skb, nla_b);
  315. }
  316. #endif /* IPv6 */
  317. nla_nest_end(skb, nla_a);
  318. break;
  319. case NETLBL_NLTYPE_UNLABELED:
  320. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, entry->type);
  321. break;
  322. case NETLBL_NLTYPE_CIPSOV4:
  323. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, entry->type);
  324. if (ret_val != 0)
  325. return ret_val;
  326. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  327. entry->type_def.cipsov4->doi);
  328. break;
  329. }
  330. return ret_val;
  331. }
  332. /*
  333. * NetLabel Command Handlers
  334. */
  335. /**
  336. * netlbl_mgmt_add - Handle an ADD message
  337. * @skb: the NETLINK buffer
  338. * @info: the Generic NETLINK info block
  339. *
  340. * Description:
  341. * Process a user generated ADD message and add the domains from the message
  342. * to the hash table. See netlabel.h for a description of the message format.
  343. * Returns zero on success, negative values on failure.
  344. *
  345. */
  346. static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
  347. {
  348. struct netlbl_audit audit_info;
  349. if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) ||
  350. (!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  351. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  352. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  353. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  354. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  355. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  356. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  357. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  358. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  359. return -EINVAL;
  360. netlbl_netlink_auditinfo(skb, &audit_info);
  361. return netlbl_mgmt_add_common(info, &audit_info);
  362. }
  363. /**
  364. * netlbl_mgmt_remove - Handle a REMOVE message
  365. * @skb: the NETLINK buffer
  366. * @info: the Generic NETLINK info block
  367. *
  368. * Description:
  369. * Process a user generated REMOVE message and remove the specified domain
  370. * mappings. Returns zero on success, negative values on failure.
  371. *
  372. */
  373. static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
  374. {
  375. char *domain;
  376. struct netlbl_audit audit_info;
  377. if (!info->attrs[NLBL_MGMT_A_DOMAIN])
  378. return -EINVAL;
  379. netlbl_netlink_auditinfo(skb, &audit_info);
  380. domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
  381. return netlbl_domhsh_remove(domain, &audit_info);
  382. }
  383. /**
  384. * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
  385. * @entry: the domain mapping hash table entry
  386. * @arg: the netlbl_domhsh_walk_arg structure
  387. *
  388. * Description:
  389. * This function is designed to be used as a callback to the
  390. * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
  391. * message. Returns the size of the message on success, negative values on
  392. * failure.
  393. *
  394. */
  395. static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
  396. {
  397. int ret_val = -ENOMEM;
  398. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  399. void *data;
  400. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  401. cb_arg->seq, &netlbl_mgmt_gnl_family,
  402. NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
  403. if (data == NULL)
  404. goto listall_cb_failure;
  405. ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
  406. if (ret_val != 0)
  407. goto listall_cb_failure;
  408. cb_arg->seq++;
  409. return genlmsg_end(cb_arg->skb, data);
  410. listall_cb_failure:
  411. genlmsg_cancel(cb_arg->skb, data);
  412. return ret_val;
  413. }
  414. /**
  415. * netlbl_mgmt_listall - Handle a LISTALL message
  416. * @skb: the NETLINK buffer
  417. * @cb: the NETLINK callback
  418. *
  419. * Description:
  420. * Process a user generated LISTALL message and dumps the domain hash table in
  421. * a form suitable for use in a kernel generated LISTALL message. Returns zero
  422. * on success, negative values on failure.
  423. *
  424. */
  425. static int netlbl_mgmt_listall(struct sk_buff *skb,
  426. struct netlink_callback *cb)
  427. {
  428. struct netlbl_domhsh_walk_arg cb_arg;
  429. u32 skip_bkt = cb->args[0];
  430. u32 skip_chain = cb->args[1];
  431. cb_arg.nl_cb = cb;
  432. cb_arg.skb = skb;
  433. cb_arg.seq = cb->nlh->nlmsg_seq;
  434. netlbl_domhsh_walk(&skip_bkt,
  435. &skip_chain,
  436. netlbl_mgmt_listall_cb,
  437. &cb_arg);
  438. cb->args[0] = skip_bkt;
  439. cb->args[1] = skip_chain;
  440. return skb->len;
  441. }
  442. /**
  443. * netlbl_mgmt_adddef - Handle an ADDDEF message
  444. * @skb: the NETLINK buffer
  445. * @info: the Generic NETLINK info block
  446. *
  447. * Description:
  448. * Process a user generated ADDDEF message and respond accordingly. Returns
  449. * zero on success, negative values on failure.
  450. *
  451. */
  452. static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
  453. {
  454. struct netlbl_audit audit_info;
  455. if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  456. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  457. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  458. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  459. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  460. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  461. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  462. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  463. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  464. return -EINVAL;
  465. netlbl_netlink_auditinfo(skb, &audit_info);
  466. return netlbl_mgmt_add_common(info, &audit_info);
  467. }
  468. /**
  469. * netlbl_mgmt_removedef - Handle a REMOVEDEF message
  470. * @skb: the NETLINK buffer
  471. * @info: the Generic NETLINK info block
  472. *
  473. * Description:
  474. * Process a user generated REMOVEDEF message and remove the default domain
  475. * mapping. Returns zero on success, negative values on failure.
  476. *
  477. */
  478. static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
  479. {
  480. struct netlbl_audit audit_info;
  481. netlbl_netlink_auditinfo(skb, &audit_info);
  482. return netlbl_domhsh_remove_default(&audit_info);
  483. }
  484. /**
  485. * netlbl_mgmt_listdef - Handle a LISTDEF message
  486. * @skb: the NETLINK buffer
  487. * @info: the Generic NETLINK info block
  488. *
  489. * Description:
  490. * Process a user generated LISTDEF message and dumps the default domain
  491. * mapping in a form suitable for use in a kernel generated LISTDEF message.
  492. * Returns zero on success, negative values on failure.
  493. *
  494. */
  495. static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
  496. {
  497. int ret_val = -ENOMEM;
  498. struct sk_buff *ans_skb = NULL;
  499. void *data;
  500. struct netlbl_dom_map *entry;
  501. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  502. if (ans_skb == NULL)
  503. return -ENOMEM;
  504. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  505. 0, NLBL_MGMT_C_LISTDEF);
  506. if (data == NULL)
  507. goto listdef_failure;
  508. rcu_read_lock();
  509. entry = netlbl_domhsh_getentry(NULL);
  510. if (entry == NULL) {
  511. ret_val = -ENOENT;
  512. goto listdef_failure_lock;
  513. }
  514. ret_val = netlbl_mgmt_listentry(ans_skb, entry);
  515. rcu_read_unlock();
  516. if (ret_val != 0)
  517. goto listdef_failure;
  518. genlmsg_end(ans_skb, data);
  519. return genlmsg_reply(ans_skb, info);
  520. listdef_failure_lock:
  521. rcu_read_unlock();
  522. listdef_failure:
  523. kfree_skb(ans_skb);
  524. return ret_val;
  525. }
  526. /**
  527. * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
  528. * @skb: the skb to write to
  529. * @cb: the NETLINK callback
  530. * @protocol: the NetLabel protocol to use in the message
  531. *
  532. * Description:
  533. * This function is to be used in conjunction with netlbl_mgmt_protocols() to
  534. * answer a application's PROTOCOLS message. Returns the size of the message
  535. * on success, negative values on failure.
  536. *
  537. */
  538. static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
  539. struct netlink_callback *cb,
  540. u32 protocol)
  541. {
  542. int ret_val = -ENOMEM;
  543. void *data;
  544. data = genlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  545. &netlbl_mgmt_gnl_family, NLM_F_MULTI,
  546. NLBL_MGMT_C_PROTOCOLS);
  547. if (data == NULL)
  548. goto protocols_cb_failure;
  549. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
  550. if (ret_val != 0)
  551. goto protocols_cb_failure;
  552. return genlmsg_end(skb, data);
  553. protocols_cb_failure:
  554. genlmsg_cancel(skb, data);
  555. return ret_val;
  556. }
  557. /**
  558. * netlbl_mgmt_protocols - Handle a PROTOCOLS message
  559. * @skb: the NETLINK buffer
  560. * @cb: the NETLINK callback
  561. *
  562. * Description:
  563. * Process a user generated PROTOCOLS message and respond accordingly.
  564. *
  565. */
  566. static int netlbl_mgmt_protocols(struct sk_buff *skb,
  567. struct netlink_callback *cb)
  568. {
  569. u32 protos_sent = cb->args[0];
  570. if (protos_sent == 0) {
  571. if (netlbl_mgmt_protocols_cb(skb,
  572. cb,
  573. NETLBL_NLTYPE_UNLABELED) < 0)
  574. goto protocols_return;
  575. protos_sent++;
  576. }
  577. if (protos_sent == 1) {
  578. if (netlbl_mgmt_protocols_cb(skb,
  579. cb,
  580. NETLBL_NLTYPE_CIPSOV4) < 0)
  581. goto protocols_return;
  582. protos_sent++;
  583. }
  584. protocols_return:
  585. cb->args[0] = protos_sent;
  586. return skb->len;
  587. }
  588. /**
  589. * netlbl_mgmt_version - Handle a VERSION message
  590. * @skb: the NETLINK buffer
  591. * @info: the Generic NETLINK info block
  592. *
  593. * Description:
  594. * Process a user generated VERSION message and respond accordingly. Returns
  595. * zero on success, negative values on failure.
  596. *
  597. */
  598. static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
  599. {
  600. int ret_val = -ENOMEM;
  601. struct sk_buff *ans_skb = NULL;
  602. void *data;
  603. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  604. if (ans_skb == NULL)
  605. return -ENOMEM;
  606. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  607. 0, NLBL_MGMT_C_VERSION);
  608. if (data == NULL)
  609. goto version_failure;
  610. ret_val = nla_put_u32(ans_skb,
  611. NLBL_MGMT_A_VERSION,
  612. NETLBL_PROTO_VERSION);
  613. if (ret_val != 0)
  614. goto version_failure;
  615. genlmsg_end(ans_skb, data);
  616. return genlmsg_reply(ans_skb, info);
  617. version_failure:
  618. kfree_skb(ans_skb);
  619. return ret_val;
  620. }
  621. /*
  622. * NetLabel Generic NETLINK Command Definitions
  623. */
  624. static struct genl_ops netlbl_mgmt_genl_ops[] = {
  625. {
  626. .cmd = NLBL_MGMT_C_ADD,
  627. .flags = GENL_ADMIN_PERM,
  628. .policy = netlbl_mgmt_genl_policy,
  629. .doit = netlbl_mgmt_add,
  630. .dumpit = NULL,
  631. },
  632. {
  633. .cmd = NLBL_MGMT_C_REMOVE,
  634. .flags = GENL_ADMIN_PERM,
  635. .policy = netlbl_mgmt_genl_policy,
  636. .doit = netlbl_mgmt_remove,
  637. .dumpit = NULL,
  638. },
  639. {
  640. .cmd = NLBL_MGMT_C_LISTALL,
  641. .flags = 0,
  642. .policy = netlbl_mgmt_genl_policy,
  643. .doit = NULL,
  644. .dumpit = netlbl_mgmt_listall,
  645. },
  646. {
  647. .cmd = NLBL_MGMT_C_ADDDEF,
  648. .flags = GENL_ADMIN_PERM,
  649. .policy = netlbl_mgmt_genl_policy,
  650. .doit = netlbl_mgmt_adddef,
  651. .dumpit = NULL,
  652. },
  653. {
  654. .cmd = NLBL_MGMT_C_REMOVEDEF,
  655. .flags = GENL_ADMIN_PERM,
  656. .policy = netlbl_mgmt_genl_policy,
  657. .doit = netlbl_mgmt_removedef,
  658. .dumpit = NULL,
  659. },
  660. {
  661. .cmd = NLBL_MGMT_C_LISTDEF,
  662. .flags = 0,
  663. .policy = netlbl_mgmt_genl_policy,
  664. .doit = netlbl_mgmt_listdef,
  665. .dumpit = NULL,
  666. },
  667. {
  668. .cmd = NLBL_MGMT_C_PROTOCOLS,
  669. .flags = 0,
  670. .policy = netlbl_mgmt_genl_policy,
  671. .doit = NULL,
  672. .dumpit = netlbl_mgmt_protocols,
  673. },
  674. {
  675. .cmd = NLBL_MGMT_C_VERSION,
  676. .flags = 0,
  677. .policy = netlbl_mgmt_genl_policy,
  678. .doit = netlbl_mgmt_version,
  679. .dumpit = NULL,
  680. },
  681. };
  682. /*
  683. * NetLabel Generic NETLINK Protocol Functions
  684. */
  685. /**
  686. * netlbl_mgmt_genl_init - Register the NetLabel management component
  687. *
  688. * Description:
  689. * Register the NetLabel management component with the Generic NETLINK
  690. * mechanism. Returns zero on success, negative values on failure.
  691. *
  692. */
  693. int __init netlbl_mgmt_genl_init(void)
  694. {
  695. int ret_val, i;
  696. ret_val = genl_register_family(&netlbl_mgmt_gnl_family);
  697. if (ret_val != 0)
  698. return ret_val;
  699. for (i = 0; i < ARRAY_SIZE(netlbl_mgmt_genl_ops); i++) {
  700. ret_val = genl_register_ops(&netlbl_mgmt_gnl_family,
  701. &netlbl_mgmt_genl_ops[i]);
  702. if (ret_val != 0)
  703. return ret_val;
  704. }
  705. return 0;
  706. }