netlabel_mgmt.c 22 KB

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