netlabel_mgmt.c 22 KB

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