netlabel_mgmt.c 20 KB

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