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_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->def.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->def.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->def.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->def.cipso = 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->def.type = entry->def.type;
  158. if (cipsov4)
  159. map->def.cipso = 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->def.type = NETLBL_NLTYPE_ADDRSELECT;
  166. entry->def.addrsel = addrmap;
  167. #if IS_ENABLED(CONFIG_IPV6)
  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. 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. map->list.mask = *mask;
  202. map->list.valid = 1;
  203. map->def.type = entry->def.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->def.type = NETLBL_NLTYPE_ADDRSELECT;
  210. entry->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 responsible 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 IS_ENABLED(CONFIG_IPV6)
  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->def.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, &entry->def.addrsel->list4) {
  259. struct netlbl_domaddr4_map *map4;
  260. struct in_addr addr_struct;
  261. nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
  262. if (nla_b == NULL)
  263. return -ENOMEM;
  264. addr_struct.s_addr = iter4->addr;
  265. ret_val = nla_put(skb, NLBL_MGMT_A_IPV4ADDR,
  266. sizeof(struct in_addr),
  267. &addr_struct);
  268. if (ret_val != 0)
  269. return ret_val;
  270. addr_struct.s_addr = iter4->mask;
  271. ret_val = nla_put(skb, NLBL_MGMT_A_IPV4MASK,
  272. sizeof(struct in_addr),
  273. &addr_struct);
  274. if (ret_val != 0)
  275. return ret_val;
  276. map4 = netlbl_domhsh_addr4_entry(iter4);
  277. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
  278. map4->def.type);
  279. if (ret_val != 0)
  280. return ret_val;
  281. switch (map4->def.type) {
  282. case NETLBL_NLTYPE_CIPSOV4:
  283. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  284. map4->def.cipso->doi);
  285. if (ret_val != 0)
  286. return ret_val;
  287. break;
  288. }
  289. nla_nest_end(skb, nla_b);
  290. }
  291. #if IS_ENABLED(CONFIG_IPV6)
  292. netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
  293. struct netlbl_domaddr6_map *map6;
  294. nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
  295. if (nla_b == NULL)
  296. return -ENOMEM;
  297. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6ADDR,
  298. sizeof(struct in6_addr),
  299. &iter6->addr);
  300. if (ret_val != 0)
  301. return ret_val;
  302. ret_val = nla_put(skb, NLBL_MGMT_A_IPV6MASK,
  303. sizeof(struct in6_addr),
  304. &iter6->mask);
  305. if (ret_val != 0)
  306. return ret_val;
  307. map6 = netlbl_domhsh_addr6_entry(iter6);
  308. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
  309. map6->def.type);
  310. if (ret_val != 0)
  311. return ret_val;
  312. nla_nest_end(skb, nla_b);
  313. }
  314. #endif /* IPv6 */
  315. nla_nest_end(skb, nla_a);
  316. break;
  317. case NETLBL_NLTYPE_UNLABELED:
  318. ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
  319. break;
  320. case NETLBL_NLTYPE_CIPSOV4:
  321. ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
  322. if (ret_val != 0)
  323. return ret_val;
  324. ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
  325. entry->def.cipso->doi);
  326. break;
  327. }
  328. return ret_val;
  329. }
  330. /*
  331. * NetLabel Command Handlers
  332. */
  333. /**
  334. * netlbl_mgmt_add - Handle an ADD message
  335. * @skb: the NETLINK buffer
  336. * @info: the Generic NETLINK info block
  337. *
  338. * Description:
  339. * Process a user generated ADD message and add the domains from the message
  340. * to the hash table. See netlabel.h for a description of the message format.
  341. * Returns zero on success, negative values on failure.
  342. *
  343. */
  344. static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
  345. {
  346. struct netlbl_audit audit_info;
  347. if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) ||
  348. (!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  349. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  350. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  351. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  352. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  353. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  354. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  355. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  356. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  357. return -EINVAL;
  358. netlbl_netlink_auditinfo(skb, &audit_info);
  359. return netlbl_mgmt_add_common(info, &audit_info);
  360. }
  361. /**
  362. * netlbl_mgmt_remove - Handle a REMOVE message
  363. * @skb: the NETLINK buffer
  364. * @info: the Generic NETLINK info block
  365. *
  366. * Description:
  367. * Process a user generated REMOVE message and remove the specified domain
  368. * mappings. Returns zero on success, negative values on failure.
  369. *
  370. */
  371. static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
  372. {
  373. char *domain;
  374. struct netlbl_audit audit_info;
  375. if (!info->attrs[NLBL_MGMT_A_DOMAIN])
  376. return -EINVAL;
  377. netlbl_netlink_auditinfo(skb, &audit_info);
  378. domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
  379. return netlbl_domhsh_remove(domain, &audit_info);
  380. }
  381. /**
  382. * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
  383. * @entry: the domain mapping hash table entry
  384. * @arg: the netlbl_domhsh_walk_arg structure
  385. *
  386. * Description:
  387. * This function is designed to be used as a callback to the
  388. * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
  389. * message. Returns the size of the message on success, negative values on
  390. * failure.
  391. *
  392. */
  393. static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
  394. {
  395. int ret_val = -ENOMEM;
  396. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  397. void *data;
  398. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
  399. cb_arg->seq, &netlbl_mgmt_gnl_family,
  400. NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
  401. if (data == NULL)
  402. goto listall_cb_failure;
  403. ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
  404. if (ret_val != 0)
  405. goto listall_cb_failure;
  406. cb_arg->seq++;
  407. return genlmsg_end(cb_arg->skb, data);
  408. listall_cb_failure:
  409. genlmsg_cancel(cb_arg->skb, data);
  410. return ret_val;
  411. }
  412. /**
  413. * netlbl_mgmt_listall - Handle a LISTALL message
  414. * @skb: the NETLINK buffer
  415. * @cb: the NETLINK callback
  416. *
  417. * Description:
  418. * Process a user generated LISTALL message and dumps the domain hash table in
  419. * a form suitable for use in a kernel generated LISTALL message. Returns zero
  420. * on success, negative values on failure.
  421. *
  422. */
  423. static int netlbl_mgmt_listall(struct sk_buff *skb,
  424. struct netlink_callback *cb)
  425. {
  426. struct netlbl_domhsh_walk_arg cb_arg;
  427. u32 skip_bkt = cb->args[0];
  428. u32 skip_chain = cb->args[1];
  429. cb_arg.nl_cb = cb;
  430. cb_arg.skb = skb;
  431. cb_arg.seq = cb->nlh->nlmsg_seq;
  432. netlbl_domhsh_walk(&skip_bkt,
  433. &skip_chain,
  434. netlbl_mgmt_listall_cb,
  435. &cb_arg);
  436. cb->args[0] = skip_bkt;
  437. cb->args[1] = skip_chain;
  438. return skb->len;
  439. }
  440. /**
  441. * netlbl_mgmt_adddef - Handle an ADDDEF message
  442. * @skb: the NETLINK buffer
  443. * @info: the Generic NETLINK info block
  444. *
  445. * Description:
  446. * Process a user generated ADDDEF message and respond accordingly. Returns
  447. * zero on success, negative values on failure.
  448. *
  449. */
  450. static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
  451. {
  452. struct netlbl_audit audit_info;
  453. if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
  454. (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
  455. info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
  456. (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
  457. info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
  458. ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
  459. (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
  460. ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
  461. (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
  462. return -EINVAL;
  463. netlbl_netlink_auditinfo(skb, &audit_info);
  464. return netlbl_mgmt_add_common(info, &audit_info);
  465. }
  466. /**
  467. * netlbl_mgmt_removedef - Handle a REMOVEDEF message
  468. * @skb: the NETLINK buffer
  469. * @info: the Generic NETLINK info block
  470. *
  471. * Description:
  472. * Process a user generated REMOVEDEF message and remove the default domain
  473. * mapping. Returns zero on success, negative values on failure.
  474. *
  475. */
  476. static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
  477. {
  478. struct netlbl_audit audit_info;
  479. netlbl_netlink_auditinfo(skb, &audit_info);
  480. return netlbl_domhsh_remove_default(&audit_info);
  481. }
  482. /**
  483. * netlbl_mgmt_listdef - Handle a LISTDEF message
  484. * @skb: the NETLINK buffer
  485. * @info: the Generic NETLINK info block
  486. *
  487. * Description:
  488. * Process a user generated LISTDEF message and dumps the default domain
  489. * mapping in a form suitable for use in a kernel generated LISTDEF message.
  490. * Returns zero on success, negative values on failure.
  491. *
  492. */
  493. static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
  494. {
  495. int ret_val = -ENOMEM;
  496. struct sk_buff *ans_skb = NULL;
  497. void *data;
  498. struct netlbl_dom_map *entry;
  499. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  500. if (ans_skb == NULL)
  501. return -ENOMEM;
  502. data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
  503. 0, NLBL_MGMT_C_LISTDEF);
  504. if (data == NULL)
  505. goto listdef_failure;
  506. rcu_read_lock();
  507. entry = netlbl_domhsh_getentry(NULL);
  508. if (entry == NULL) {
  509. ret_val = -ENOENT;
  510. goto listdef_failure_lock;
  511. }
  512. ret_val = netlbl_mgmt_listentry(ans_skb, entry);
  513. rcu_read_unlock();
  514. if (ret_val != 0)
  515. goto listdef_failure;
  516. genlmsg_end(ans_skb, data);
  517. return genlmsg_reply(ans_skb, info);
  518. listdef_failure_lock:
  519. rcu_read_unlock();
  520. listdef_failure:
  521. kfree_skb(ans_skb);
  522. return ret_val;
  523. }
  524. /**
  525. * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
  526. * @skb: the skb to write to
  527. * @cb: the NETLINK callback
  528. * @protocol: the NetLabel protocol to use in the message
  529. *
  530. * Description:
  531. * This function is to be used in conjunction with netlbl_mgmt_protocols() to
  532. * answer a application's PROTOCOLS message. Returns the size of the message
  533. * on success, negative values on failure.
  534. *
  535. */
  536. static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
  537. struct netlink_callback *cb,
  538. u32 protocol)
  539. {
  540. int ret_val = -ENOMEM;
  541. void *data;
  542. data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  543. &netlbl_mgmt_gnl_family, NLM_F_MULTI,
  544. NLBL_MGMT_C_PROTOCOLS);
  545. if (data == NULL)
  546. goto protocols_cb_failure;
  547. ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
  548. if (ret_val != 0)
  549. goto protocols_cb_failure;
  550. return genlmsg_end(skb, data);
  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. }