netlabel_mgmt.c 20 KB

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