netlabel_mgmt.c 20 KB

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