netlabel_mgmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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->def.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->def.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->def.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->def.cipso = 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->def.type = entry->def.type;
  159. if (cipsov4)
  160. map->def.cipso = 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->def.type = NETLBL_NLTYPE_ADDRSELECT;
  167. entry->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->def.type = entry->def.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->def.type = NETLBL_NLTYPE_ADDRSELECT;
  211. entry->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->def.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, &entry->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->def.type);
  280. if (ret_val != 0)
  281. return ret_val;
  282. switch (map4->def.type) {
  283. case NETLBL_NLTYPE_CIPSOV4:
  284. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  285. map4->def.cipso->doi);
  286. if (ret_val != 0)
  287. return ret_val;
  288. break;
  289. }
  290. nla_nest_end(skb, nla_b);
  291. }
  292. #if IS_ENABLED(CONFIG_IPV6)
  293. netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
  294. struct netlbl_domaddr6_map *map6;
  295. nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
  296. if (nla_b == NULL)
  297. return -ENOMEM;
  298. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6ADDR,
  299. sizeof(struct in6_addr),
  300. &iter6->addr);
  301. if (ret_val != 0)
  302. return ret_val;
  303. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6MASK,
  304. sizeof(struct in6_addr),
  305. &iter6->mask);
  306. if (ret_val != 0)
  307. return ret_val;
  308. map6 = netlbl_domhsh_addr6_entry(iter6);
  309. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
  310. map6->def.type);
  311. if (ret_val != 0)
  312. return ret_val;
  313. nla_nest_end(skb, nla_b);
  314. }
  315. #endif /* IPv6 */
  316. nla_nest_end(skb, nla_a);
  317. break;
  318. case NETLBL_NLTYPE_UNLABELED:
  319. ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
  320. break;
  321. case NETLBL_NLTYPE_CIPSOV4:
  322. ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
  323. if (ret_val != 0)
  324. return ret_val;
  325. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  326. entry->def.cipso->doi);
  327. break;
  328. }
  329. return ret_val;
  330. }
  331. /*
  332. * NetLabel Command Handlers
  333. */
  334. /**
  335. * netlbl_mgmt_add - Handle an ADD message
  336. * @skb: the NETLINK buffer
  337. * @info: the Generic NETLINK info block
  338. *
  339. * Description:
  340. * Process a user generated ADD message and add the domains from the message
  341. * to the hash table. See netlabel.h for a description of the message format.
  342. * Returns zero on success, negative values on failure.
  343. *
  344. */
  345. static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
  346. {
  347. struct netlbl_audit audit_info;
  348. if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) ||
  349. (!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  350. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  351. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  352. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  353. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  354. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  355. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  356. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  357. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  358. return -EINVAL;
  359. netlbl_netlink_auditinfo(skb, &audit_info);
  360. return netlbl_mgmt_add_common(info, &audit_info);
  361. }
  362. /**
  363. * netlbl_mgmt_remove - Handle a REMOVE message
  364. * @skb: the NETLINK buffer
  365. * @info: the Generic NETLINK info block
  366. *
  367. * Description:
  368. * Process a user generated REMOVE message and remove the specified domain
  369. * mappings. Returns zero on success, negative values on failure.
  370. *
  371. */
  372. static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
  373. {
  374. char *domain;
  375. struct netlbl_audit audit_info;
  376. if (!info->attrs[NLBL_MGMT_A_DOMAIN])
  377. return -EINVAL;
  378. netlbl_netlink_auditinfo(skb, &audit_info);
  379. domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
  380. return netlbl_domhsh_remove(domain, &audit_info);
  381. }
  382. /**
  383. * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
  384. * @entry: the domain mapping hash table entry
  385. * @arg: the netlbl_domhsh_walk_arg structure
  386. *
  387. * Description:
  388. * This function is designed to be used as a callback to the
  389. * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
  390. * message. Returns the size of the message on success, negative values on
  391. * failure.
  392. *
  393. */
  394. static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
  395. {
  396. int ret_val = -ENOMEM;
  397. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  398. void *data;
  399. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
  400. cb_arg->seq, &netlbl_mgmt_gnl_family,
  401. NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
  402. if (data == NULL)
  403. goto listall_cb_failure;
  404. ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
  405. if (ret_val != 0)
  406. goto listall_cb_failure;
  407. cb_arg->seq++;
  408. return genlmsg_end(cb_arg->skb, data);
  409. listall_cb_failure:
  410. genlmsg_cancel(cb_arg->skb, data);
  411. return ret_val;
  412. }
  413. /**
  414. * netlbl_mgmt_listall - Handle a LISTALL message
  415. * @skb: the NETLINK buffer
  416. * @cb: the NETLINK callback
  417. *
  418. * Description:
  419. * Process a user generated LISTALL message and dumps the domain hash table in
  420. * a form suitable for use in a kernel generated LISTALL message. Returns zero
  421. * on success, negative values on failure.
  422. *
  423. */
  424. static int netlbl_mgmt_listall(struct sk_buff *skb,
  425. struct netlink_callback *cb)
  426. {
  427. struct netlbl_domhsh_walk_arg cb_arg;
  428. u32 skip_bkt = cb->args[0];
  429. u32 skip_chain = cb->args[1];
  430. cb_arg.nl_cb = cb;
  431. cb_arg.skb = skb;
  432. cb_arg.seq = cb->nlh->nlmsg_seq;
  433. netlbl_domhsh_walk(&skip_bkt,
  434. &skip_chain,
  435. netlbl_mgmt_listall_cb,
  436. &cb_arg);
  437. cb->args[0] = skip_bkt;
  438. cb->args[1] = skip_chain;
  439. return skb->len;
  440. }
  441. /**
  442. * netlbl_mgmt_adddef - Handle an ADDDEF message
  443. * @skb: the NETLINK buffer
  444. * @info: the Generic NETLINK info block
  445. *
  446. * Description:
  447. * Process a user generated ADDDEF message and respond accordingly. Returns
  448. * zero on success, negative values on failure.
  449. *
  450. */
  451. static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
  452. {
  453. struct netlbl_audit audit_info;
  454. if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  455. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  456. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  457. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  458. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  459. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  460. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  461. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  462. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  463. return -EINVAL;
  464. netlbl_netlink_auditinfo(skb, &audit_info);
  465. return netlbl_mgmt_add_common(info, &audit_info);
  466. }
  467. /**
  468. * netlbl_mgmt_removedef - Handle a REMOVEDEF message
  469. * @skb: the NETLINK buffer
  470. * @info: the Generic NETLINK info block
  471. *
  472. * Description:
  473. * Process a user generated REMOVEDEF message and remove the default domain
  474. * mapping. Returns zero on success, negative values on failure.
  475. *
  476. */
  477. static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
  478. {
  479. struct netlbl_audit audit_info;
  480. netlbl_netlink_auditinfo(skb, &audit_info);
  481. return netlbl_domhsh_remove_default(&audit_info);
  482. }
  483. /**
  484. * netlbl_mgmt_listdef - Handle a LISTDEF message
  485. * @skb: the NETLINK buffer
  486. * @info: the Generic NETLINK info block
  487. *
  488. * Description:
  489. * Process a user generated LISTDEF message and dumps the default domain
  490. * mapping in a form suitable for use in a kernel generated LISTDEF message.
  491. * Returns zero on success, negative values on failure.
  492. *
  493. */
  494. static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
  495. {
  496. int ret_val = -ENOMEM;
  497. struct sk_buff *ans_skb = NULL;
  498. void *data;
  499. struct netlbl_dom_map *entry;
  500. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  501. if (ans_skb == NULL)
  502. return -ENOMEM;
  503. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  504. 0, NLBL_MGMT_C_LISTDEF);
  505. if (data == NULL)
  506. goto listdef_failure;
  507. rcu_read_lock();
  508. entry = netlbl_domhsh_getentry(NULL);
  509. if (entry == NULL) {
  510. ret_val = -ENOENT;
  511. goto listdef_failure_lock;
  512. }
  513. ret_val = netlbl_mgmt_listentry(ans_skb, entry);
  514. rcu_read_unlock();
  515. if (ret_val != 0)
  516. goto listdef_failure;
  517. genlmsg_end(ans_skb, data);
  518. return genlmsg_reply(ans_skb, info);
  519. listdef_failure_lock:
  520. rcu_read_unlock();
  521. listdef_failure:
  522. kfree_skb(ans_skb);
  523. return ret_val;
  524. }
  525. /**
  526. * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
  527. * @skb: the skb to write to
  528. * @cb: the NETLINK callback
  529. * @protocol: the NetLabel protocol to use in the message
  530. *
  531. * Description:
  532. * This function is to be used in conjunction with netlbl_mgmt_protocols() to
  533. * answer a application's PROTOCOLS message. Returns the size of the message
  534. * on success, negative values on failure.
  535. *
  536. */
  537. static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
  538. struct netlink_callback *cb,
  539. u32 protocol)
  540. {
  541. int ret_val = -ENOMEM;
  542. void *data;
  543. data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  544. &netlbl_mgmt_gnl_family, NLM_F_MULTI,
  545. NLBL_MGMT_C_PROTOCOLS);
  546. if (data == NULL)
  547. goto protocols_cb_failure;
  548. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
  549. if (ret_val != 0)
  550. goto protocols_cb_failure;
  551. return genlmsg_end(skb, data);
  552. protocols_cb_failure:
  553. genlmsg_cancel(skb, data);
  554. return ret_val;
  555. }
  556. /**
  557. * netlbl_mgmt_protocols - Handle a PROTOCOLS message
  558. * @skb: the NETLINK buffer
  559. * @cb: the NETLINK callback
  560. *
  561. * Description:
  562. * Process a user generated PROTOCOLS message and respond accordingly.
  563. *
  564. */
  565. static int netlbl_mgmt_protocols(struct sk_buff *skb,
  566. struct netlink_callback *cb)
  567. {
  568. u32 protos_sent = cb->args[0];
  569. if (protos_sent == 0) {
  570. if (netlbl_mgmt_protocols_cb(skb,
  571. cb,
  572. NETLBL_NLTYPE_UNLABELED) < 0)
  573. goto protocols_return;
  574. protos_sent++;
  575. }
  576. if (protos_sent == 1) {
  577. if (netlbl_mgmt_protocols_cb(skb,
  578. cb,
  579. NETLBL_NLTYPE_CIPSOV4) < 0)
  580. goto protocols_return;
  581. protos_sent++;
  582. }
  583. protocols_return:
  584. cb->args[0] = protos_sent;
  585. return skb->len;
  586. }
  587. /**
  588. * netlbl_mgmt_version - Handle a VERSION message
  589. * @skb: the NETLINK buffer
  590. * @info: the Generic NETLINK info block
  591. *
  592. * Description:
  593. * Process a user generated VERSION message and respond accordingly. Returns
  594. * zero on success, negative values on failure.
  595. *
  596. */
  597. static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
  598. {
  599. int ret_val = -ENOMEM;
  600. struct sk_buff *ans_skb = NULL;
  601. void *data;
  602. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  603. if (ans_skb == NULL)
  604. return -ENOMEM;
  605. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  606. 0, NLBL_MGMT_C_VERSION);
  607. if (data == NULL)
  608. goto version_failure;
  609. ret_val = nla_put_u32(ans_skb,
  610. NLBL_MGMT_A_VERSION,
  611. NETLBL_PROTO_VERSION);
  612. if (ret_val != 0)
  613. goto version_failure;
  614. genlmsg_end(ans_skb, data);
  615. return genlmsg_reply(ans_skb, info);
  616. version_failure:
  617. kfree_skb(ans_skb);
  618. return ret_val;
  619. }
  620. /*
  621. * NetLabel Generic NETLINK Command Definitions
  622. */
  623. static struct genl_ops netlbl_mgmt_genl_ops[] = {
  624. {
  625. .cmd = NLBL_MGMT_C_ADD,
  626. .flags = GENL_ADMIN_PERM,
  627. .policy = netlbl_mgmt_genl_policy,
  628. .doit = netlbl_mgmt_add,
  629. .dumpit = NULL,
  630. },
  631. {
  632. .cmd = NLBL_MGMT_C_REMOVE,
  633. .flags = GENL_ADMIN_PERM,
  634. .policy = netlbl_mgmt_genl_policy,
  635. .doit = netlbl_mgmt_remove,
  636. .dumpit = NULL,
  637. },
  638. {
  639. .cmd = NLBL_MGMT_C_LISTALL,
  640. .flags = 0,
  641. .policy = netlbl_mgmt_genl_policy,
  642. .doit = NULL,
  643. .dumpit = netlbl_mgmt_listall,
  644. },
  645. {
  646. .cmd = NLBL_MGMT_C_ADDDEF,
  647. .flags = GENL_ADMIN_PERM,
  648. .policy = netlbl_mgmt_genl_policy,
  649. .doit = netlbl_mgmt_adddef,
  650. .dumpit = NULL,
  651. },
  652. {
  653. .cmd = NLBL_MGMT_C_REMOVEDEF,
  654. .flags = GENL_ADMIN_PERM,
  655. .policy = netlbl_mgmt_genl_policy,
  656. .doit = netlbl_mgmt_removedef,
  657. .dumpit = NULL,
  658. },
  659. {
  660. .cmd = NLBL_MGMT_C_LISTDEF,
  661. .flags = 0,
  662. .policy = netlbl_mgmt_genl_policy,
  663. .doit = netlbl_mgmt_listdef,
  664. .dumpit = NULL,
  665. },
  666. {
  667. .cmd = NLBL_MGMT_C_PROTOCOLS,
  668. .flags = 0,
  669. .policy = netlbl_mgmt_genl_policy,
  670. .doit = NULL,
  671. .dumpit = netlbl_mgmt_protocols,
  672. },
  673. {
  674. .cmd = NLBL_MGMT_C_VERSION,
  675. .flags = 0,
  676. .policy = netlbl_mgmt_genl_policy,
  677. .doit = netlbl_mgmt_version,
  678. .dumpit = NULL,
  679. },
  680. };
  681. /*
  682. * NetLabel Generic NETLINK Protocol Functions
  683. */
  684. /**
  685. * netlbl_mgmt_genl_init - Register the NetLabel management component
  686. *
  687. * Description:
  688. * Register the NetLabel management component with the Generic NETLINK
  689. * mechanism. Returns zero on success, negative values on failure.
  690. *
  691. */
  692. int __init netlbl_mgmt_genl_init(void)
  693. {
  694. return genl_register_family_with_ops(&netlbl_mgmt_gnl_family,
  695. netlbl_mgmt_genl_ops, ARRAY_SIZE(netlbl_mgmt_genl_ops));
  696. }