netlabel_calipso.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * NetLabel CALIPSO/IPv6 Support
  3. *
  4. * This file defines the CALIPSO/IPv6 functions for the NetLabel system. The
  5. * NetLabel system manages static and dynamic label mappings for network
  6. * protocols such as CIPSO and CALIPSO.
  7. *
  8. * Authors: Paul Moore <paul@paul-moore.com>
  9. * Huw Davies <huw@codeweavers.com>
  10. *
  11. */
  12. /* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  13. * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  23. * the GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. #include <linux/types.h>
  30. #include <linux/socket.h>
  31. #include <linux/string.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/audit.h>
  34. #include <linux/slab.h>
  35. #include <net/sock.h>
  36. #include <net/netlink.h>
  37. #include <net/genetlink.h>
  38. #include <net/netlabel.h>
  39. #include <net/calipso.h>
  40. #include <linux/atomic.h>
  41. #include "netlabel_user.h"
  42. #include "netlabel_calipso.h"
  43. #include "netlabel_mgmt.h"
  44. #include "netlabel_domainhash.h"
  45. /* Argument struct for calipso_doi_walk() */
  46. struct netlbl_calipso_doiwalk_arg {
  47. struct netlink_callback *nl_cb;
  48. struct sk_buff *skb;
  49. u32 seq;
  50. };
  51. /* Argument struct for netlbl_domhsh_walk() */
  52. struct netlbl_domhsh_walk_arg {
  53. struct netlbl_audit *audit_info;
  54. u32 doi;
  55. };
  56. /* NetLabel Generic NETLINK CALIPSO family */
  57. static struct genl_family netlbl_calipso_gnl_family;
  58. /* NetLabel Netlink attribute policy */
  59. static const struct nla_policy calipso_genl_policy[NLBL_CALIPSO_A_MAX + 1] = {
  60. [NLBL_CALIPSO_A_DOI] = { .type = NLA_U32 },
  61. [NLBL_CALIPSO_A_MTYPE] = { .type = NLA_U32 },
  62. };
  63. /* NetLabel Command Handlers
  64. */
  65. /**
  66. * netlbl_calipso_add_pass - Adds a CALIPSO pass DOI definition
  67. * @info: the Generic NETLINK info block
  68. * @audit_info: NetLabel audit information
  69. *
  70. * Description:
  71. * Create a new CALIPSO_MAP_PASS DOI definition based on the given ADD message
  72. * and add it to the CALIPSO engine. Return zero on success and non-zero on
  73. * error.
  74. *
  75. */
  76. static int netlbl_calipso_add_pass(struct genl_info *info,
  77. struct netlbl_audit *audit_info)
  78. {
  79. int ret_val;
  80. struct calipso_doi *doi_def = NULL;
  81. doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
  82. if (!doi_def)
  83. return -ENOMEM;
  84. doi_def->type = CALIPSO_MAP_PASS;
  85. doi_def->doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
  86. ret_val = calipso_doi_add(doi_def, audit_info);
  87. if (ret_val != 0)
  88. calipso_doi_free(doi_def);
  89. return ret_val;
  90. }
  91. /**
  92. * netlbl_calipso_add - Handle an ADD message
  93. * @skb: the NETLINK buffer
  94. * @info: the Generic NETLINK info block
  95. *
  96. * Description:
  97. * Create a new DOI definition based on the given ADD message and add it to the
  98. * CALIPSO engine. Returns zero on success, negative values on failure.
  99. *
  100. */
  101. static int netlbl_calipso_add(struct sk_buff *skb, struct genl_info *info)
  102. {
  103. int ret_val = -EINVAL;
  104. struct netlbl_audit audit_info;
  105. if (!info->attrs[NLBL_CALIPSO_A_DOI] ||
  106. !info->attrs[NLBL_CALIPSO_A_MTYPE])
  107. return -EINVAL;
  108. netlbl_netlink_auditinfo(skb, &audit_info);
  109. switch (nla_get_u32(info->attrs[NLBL_CALIPSO_A_MTYPE])) {
  110. case CALIPSO_MAP_PASS:
  111. ret_val = netlbl_calipso_add_pass(info, &audit_info);
  112. break;
  113. }
  114. if (ret_val == 0)
  115. atomic_inc(&netlabel_mgmt_protocount);
  116. return ret_val;
  117. }
  118. /**
  119. * netlbl_calipso_list - Handle a LIST message
  120. * @skb: the NETLINK buffer
  121. * @info: the Generic NETLINK info block
  122. *
  123. * Description:
  124. * Process a user generated LIST message and respond accordingly.
  125. * Returns zero on success and negative values on error.
  126. *
  127. */
  128. static int netlbl_calipso_list(struct sk_buff *skb, struct genl_info *info)
  129. {
  130. int ret_val;
  131. struct sk_buff *ans_skb = NULL;
  132. void *data;
  133. u32 doi;
  134. struct calipso_doi *doi_def;
  135. if (!info->attrs[NLBL_CALIPSO_A_DOI]) {
  136. ret_val = -EINVAL;
  137. goto list_failure;
  138. }
  139. doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
  140. doi_def = calipso_doi_getdef(doi);
  141. if (!doi_def) {
  142. ret_val = -EINVAL;
  143. goto list_failure;
  144. }
  145. ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  146. if (!ans_skb) {
  147. ret_val = -ENOMEM;
  148. goto list_failure_put;
  149. }
  150. data = genlmsg_put_reply(ans_skb, info, &netlbl_calipso_gnl_family,
  151. 0, NLBL_CALIPSO_C_LIST);
  152. if (!data) {
  153. ret_val = -ENOMEM;
  154. goto list_failure_put;
  155. }
  156. ret_val = nla_put_u32(ans_skb, NLBL_CALIPSO_A_MTYPE, doi_def->type);
  157. if (ret_val != 0)
  158. goto list_failure_put;
  159. calipso_doi_putdef(doi_def);
  160. genlmsg_end(ans_skb, data);
  161. return genlmsg_reply(ans_skb, info);
  162. list_failure_put:
  163. calipso_doi_putdef(doi_def);
  164. list_failure:
  165. kfree_skb(ans_skb);
  166. return ret_val;
  167. }
  168. /**
  169. * netlbl_calipso_listall_cb - calipso_doi_walk() callback for LISTALL
  170. * @doi_def: the CALIPSO DOI definition
  171. * @arg: the netlbl_calipso_doiwalk_arg structure
  172. *
  173. * Description:
  174. * This function is designed to be used as a callback to the
  175. * calipso_doi_walk() function for use in generating a response for a LISTALL
  176. * message. Returns the size of the message on success, negative values on
  177. * failure.
  178. *
  179. */
  180. static int netlbl_calipso_listall_cb(struct calipso_doi *doi_def, void *arg)
  181. {
  182. int ret_val = -ENOMEM;
  183. struct netlbl_calipso_doiwalk_arg *cb_arg = arg;
  184. void *data;
  185. data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
  186. cb_arg->seq, &netlbl_calipso_gnl_family,
  187. NLM_F_MULTI, NLBL_CALIPSO_C_LISTALL);
  188. if (!data)
  189. goto listall_cb_failure;
  190. ret_val = nla_put_u32(cb_arg->skb, NLBL_CALIPSO_A_DOI, doi_def->doi);
  191. if (ret_val != 0)
  192. goto listall_cb_failure;
  193. ret_val = nla_put_u32(cb_arg->skb,
  194. NLBL_CALIPSO_A_MTYPE,
  195. doi_def->type);
  196. if (ret_val != 0)
  197. goto listall_cb_failure;
  198. genlmsg_end(cb_arg->skb, data);
  199. return 0;
  200. listall_cb_failure:
  201. genlmsg_cancel(cb_arg->skb, data);
  202. return ret_val;
  203. }
  204. /**
  205. * netlbl_calipso_listall - Handle a LISTALL message
  206. * @skb: the NETLINK buffer
  207. * @cb: the NETLINK callback
  208. *
  209. * Description:
  210. * Process a user generated LISTALL message and respond accordingly. Returns
  211. * zero on success and negative values on error.
  212. *
  213. */
  214. static int netlbl_calipso_listall(struct sk_buff *skb,
  215. struct netlink_callback *cb)
  216. {
  217. struct netlbl_calipso_doiwalk_arg cb_arg;
  218. u32 doi_skip = cb->args[0];
  219. cb_arg.nl_cb = cb;
  220. cb_arg.skb = skb;
  221. cb_arg.seq = cb->nlh->nlmsg_seq;
  222. calipso_doi_walk(&doi_skip, netlbl_calipso_listall_cb, &cb_arg);
  223. cb->args[0] = doi_skip;
  224. return skb->len;
  225. }
  226. /**
  227. * netlbl_calipso_remove_cb - netlbl_calipso_remove() callback for REMOVE
  228. * @entry: LSM domain mapping entry
  229. * @arg: the netlbl_domhsh_walk_arg structure
  230. *
  231. * Description:
  232. * This function is intended for use by netlbl_calipso_remove() as the callback
  233. * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
  234. * which are associated with the CALIPSO DOI specified in @arg. Returns zero on
  235. * success, negative values on failure.
  236. *
  237. */
  238. static int netlbl_calipso_remove_cb(struct netlbl_dom_map *entry, void *arg)
  239. {
  240. struct netlbl_domhsh_walk_arg *cb_arg = arg;
  241. if (entry->def.type == NETLBL_NLTYPE_CALIPSO &&
  242. entry->def.calipso->doi == cb_arg->doi)
  243. return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
  244. return 0;
  245. }
  246. /**
  247. * netlbl_calipso_remove - Handle a REMOVE message
  248. * @skb: the NETLINK buffer
  249. * @info: the Generic NETLINK info block
  250. *
  251. * Description:
  252. * Process a user generated REMOVE message and respond accordingly. Returns
  253. * zero on success, negative values on failure.
  254. *
  255. */
  256. static int netlbl_calipso_remove(struct sk_buff *skb, struct genl_info *info)
  257. {
  258. int ret_val = -EINVAL;
  259. struct netlbl_domhsh_walk_arg cb_arg;
  260. struct netlbl_audit audit_info;
  261. u32 skip_bkt = 0;
  262. u32 skip_chain = 0;
  263. if (!info->attrs[NLBL_CALIPSO_A_DOI])
  264. return -EINVAL;
  265. netlbl_netlink_auditinfo(skb, &audit_info);
  266. cb_arg.doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
  267. cb_arg.audit_info = &audit_info;
  268. ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
  269. netlbl_calipso_remove_cb, &cb_arg);
  270. if (ret_val == 0 || ret_val == -ENOENT) {
  271. ret_val = calipso_doi_remove(cb_arg.doi, &audit_info);
  272. if (ret_val == 0)
  273. atomic_dec(&netlabel_mgmt_protocount);
  274. }
  275. return ret_val;
  276. }
  277. /* NetLabel Generic NETLINK Command Definitions
  278. */
  279. static const struct genl_ops netlbl_calipso_ops[] = {
  280. {
  281. .cmd = NLBL_CALIPSO_C_ADD,
  282. .flags = GENL_ADMIN_PERM,
  283. .policy = calipso_genl_policy,
  284. .doit = netlbl_calipso_add,
  285. .dumpit = NULL,
  286. },
  287. {
  288. .cmd = NLBL_CALIPSO_C_REMOVE,
  289. .flags = GENL_ADMIN_PERM,
  290. .policy = calipso_genl_policy,
  291. .doit = netlbl_calipso_remove,
  292. .dumpit = NULL,
  293. },
  294. {
  295. .cmd = NLBL_CALIPSO_C_LIST,
  296. .flags = 0,
  297. .policy = calipso_genl_policy,
  298. .doit = netlbl_calipso_list,
  299. .dumpit = NULL,
  300. },
  301. {
  302. .cmd = NLBL_CALIPSO_C_LISTALL,
  303. .flags = 0,
  304. .policy = calipso_genl_policy,
  305. .doit = NULL,
  306. .dumpit = netlbl_calipso_listall,
  307. },
  308. };
  309. static struct genl_family netlbl_calipso_gnl_family __ro_after_init = {
  310. .hdrsize = 0,
  311. .name = NETLBL_NLTYPE_CALIPSO_NAME,
  312. .version = NETLBL_PROTO_VERSION,
  313. .maxattr = NLBL_CALIPSO_A_MAX,
  314. .module = THIS_MODULE,
  315. .ops = netlbl_calipso_ops,
  316. .n_ops = ARRAY_SIZE(netlbl_calipso_ops),
  317. };
  318. /* NetLabel Generic NETLINK Protocol Functions
  319. */
  320. /**
  321. * netlbl_calipso_genl_init - Register the CALIPSO NetLabel component
  322. *
  323. * Description:
  324. * Register the CALIPSO packet NetLabel component with the Generic NETLINK
  325. * mechanism. Returns zero on success, negative values on failure.
  326. *
  327. */
  328. int __init netlbl_calipso_genl_init(void)
  329. {
  330. return genl_register_family(&netlbl_calipso_gnl_family);
  331. }
  332. static const struct netlbl_calipso_ops *calipso_ops;
  333. /**
  334. * netlbl_calipso_ops_register - Register the CALIPSO operations
  335. *
  336. * Description:
  337. * Register the CALIPSO packet engine operations.
  338. *
  339. */
  340. const struct netlbl_calipso_ops *
  341. netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
  342. {
  343. return xchg(&calipso_ops, ops);
  344. }
  345. EXPORT_SYMBOL(netlbl_calipso_ops_register);
  346. static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
  347. {
  348. return READ_ONCE(calipso_ops);
  349. }
  350. /**
  351. * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
  352. * @doi_def: the DOI structure
  353. * @audit_info: NetLabel audit information
  354. *
  355. * Description:
  356. * The caller defines a new DOI for use by the CALIPSO engine and calls this
  357. * function to add it to the list of acceptable domains. The caller must
  358. * ensure that the mapping table specified in @doi_def->map meets all of the
  359. * requirements of the mapping type (see calipso.h for details). Returns
  360. * zero on success and non-zero on failure.
  361. *
  362. */
  363. int calipso_doi_add(struct calipso_doi *doi_def,
  364. struct netlbl_audit *audit_info)
  365. {
  366. int ret_val = -ENOMSG;
  367. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  368. if (ops)
  369. ret_val = ops->doi_add(doi_def, audit_info);
  370. return ret_val;
  371. }
  372. /**
  373. * calipso_doi_free - Frees a DOI definition
  374. * @doi_def: the DOI definition
  375. *
  376. * Description:
  377. * This function frees all of the memory associated with a DOI definition.
  378. *
  379. */
  380. void calipso_doi_free(struct calipso_doi *doi_def)
  381. {
  382. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  383. if (ops)
  384. ops->doi_free(doi_def);
  385. }
  386. /**
  387. * calipso_doi_remove - Remove an existing DOI from the CALIPSO protocol engine
  388. * @doi: the DOI value
  389. * @audit_secid: the LSM secid to use in the audit message
  390. *
  391. * Description:
  392. * Removes a DOI definition from the CALIPSO engine. The NetLabel routines will
  393. * be called to release their own LSM domain mappings as well as our own
  394. * domain list. Returns zero on success and negative values on failure.
  395. *
  396. */
  397. int calipso_doi_remove(u32 doi, struct netlbl_audit *audit_info)
  398. {
  399. int ret_val = -ENOMSG;
  400. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  401. if (ops)
  402. ret_val = ops->doi_remove(doi, audit_info);
  403. return ret_val;
  404. }
  405. /**
  406. * calipso_doi_getdef - Returns a reference to a valid DOI definition
  407. * @doi: the DOI value
  408. *
  409. * Description:
  410. * Searches for a valid DOI definition and if one is found it is returned to
  411. * the caller. Otherwise NULL is returned. The caller must ensure that
  412. * calipso_doi_putdef() is called when the caller is done.
  413. *
  414. */
  415. struct calipso_doi *calipso_doi_getdef(u32 doi)
  416. {
  417. struct calipso_doi *ret_val = NULL;
  418. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  419. if (ops)
  420. ret_val = ops->doi_getdef(doi);
  421. return ret_val;
  422. }
  423. /**
  424. * calipso_doi_putdef - Releases a reference for the given DOI definition
  425. * @doi_def: the DOI definition
  426. *
  427. * Description:
  428. * Releases a DOI definition reference obtained from calipso_doi_getdef().
  429. *
  430. */
  431. void calipso_doi_putdef(struct calipso_doi *doi_def)
  432. {
  433. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  434. if (ops)
  435. ops->doi_putdef(doi_def);
  436. }
  437. /**
  438. * calipso_doi_walk - Iterate through the DOI definitions
  439. * @skip_cnt: skip past this number of DOI definitions, updated
  440. * @callback: callback for each DOI definition
  441. * @cb_arg: argument for the callback function
  442. *
  443. * Description:
  444. * Iterate over the DOI definition list, skipping the first @skip_cnt entries.
  445. * For each entry call @callback, if @callback returns a negative value stop
  446. * 'walking' through the list and return. Updates the value in @skip_cnt upon
  447. * return. Returns zero on success, negative values on failure.
  448. *
  449. */
  450. int calipso_doi_walk(u32 *skip_cnt,
  451. int (*callback)(struct calipso_doi *doi_def, void *arg),
  452. void *cb_arg)
  453. {
  454. int ret_val = -ENOMSG;
  455. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  456. if (ops)
  457. ret_val = ops->doi_walk(skip_cnt, callback, cb_arg);
  458. return ret_val;
  459. }
  460. /**
  461. * calipso_sock_getattr - Get the security attributes from a sock
  462. * @sk: the sock
  463. * @secattr: the security attributes
  464. *
  465. * Description:
  466. * Query @sk to see if there is a CALIPSO option attached to the sock and if
  467. * there is return the CALIPSO security attributes in @secattr. This function
  468. * requires that @sk be locked, or privately held, but it does not do any
  469. * locking itself. Returns zero on success and negative values on failure.
  470. *
  471. */
  472. int calipso_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  473. {
  474. int ret_val = -ENOMSG;
  475. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  476. if (ops)
  477. ret_val = ops->sock_getattr(sk, secattr);
  478. return ret_val;
  479. }
  480. /**
  481. * calipso_sock_setattr - Add a CALIPSO option to a socket
  482. * @sk: the socket
  483. * @doi_def: the CALIPSO DOI to use
  484. * @secattr: the specific security attributes of the socket
  485. *
  486. * Description:
  487. * Set the CALIPSO option on the given socket using the DOI definition and
  488. * security attributes passed to the function. This function requires
  489. * exclusive access to @sk, which means it either needs to be in the
  490. * process of being created or locked. Returns zero on success and negative
  491. * values on failure.
  492. *
  493. */
  494. int calipso_sock_setattr(struct sock *sk,
  495. const struct calipso_doi *doi_def,
  496. const struct netlbl_lsm_secattr *secattr)
  497. {
  498. int ret_val = -ENOMSG;
  499. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  500. if (ops)
  501. ret_val = ops->sock_setattr(sk, doi_def, secattr);
  502. return ret_val;
  503. }
  504. /**
  505. * calipso_sock_delattr - Delete the CALIPSO option from a socket
  506. * @sk: the socket
  507. *
  508. * Description:
  509. * Removes the CALIPSO option from a socket, if present.
  510. *
  511. */
  512. void calipso_sock_delattr(struct sock *sk)
  513. {
  514. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  515. if (ops)
  516. ops->sock_delattr(sk);
  517. }
  518. /**
  519. * calipso_req_setattr - Add a CALIPSO option to a connection request socket
  520. * @req: the connection request socket
  521. * @doi_def: the CALIPSO DOI to use
  522. * @secattr: the specific security attributes of the socket
  523. *
  524. * Description:
  525. * Set the CALIPSO option on the given socket using the DOI definition and
  526. * security attributes passed to the function. Returns zero on success and
  527. * negative values on failure.
  528. *
  529. */
  530. int calipso_req_setattr(struct request_sock *req,
  531. const struct calipso_doi *doi_def,
  532. const struct netlbl_lsm_secattr *secattr)
  533. {
  534. int ret_val = -ENOMSG;
  535. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  536. if (ops)
  537. ret_val = ops->req_setattr(req, doi_def, secattr);
  538. return ret_val;
  539. }
  540. /**
  541. * calipso_req_delattr - Delete the CALIPSO option from a request socket
  542. * @reg: the request socket
  543. *
  544. * Description:
  545. * Removes the CALIPSO option from a request socket, if present.
  546. *
  547. */
  548. void calipso_req_delattr(struct request_sock *req)
  549. {
  550. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  551. if (ops)
  552. ops->req_delattr(req);
  553. }
  554. /**
  555. * calipso_optptr - Find the CALIPSO option in the packet
  556. * @skb: the packet
  557. *
  558. * Description:
  559. * Parse the packet's IP header looking for a CALIPSO option. Returns a pointer
  560. * to the start of the CALIPSO option on success, NULL if one if not found.
  561. *
  562. */
  563. unsigned char *calipso_optptr(const struct sk_buff *skb)
  564. {
  565. unsigned char *ret_val = NULL;
  566. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  567. if (ops)
  568. ret_val = ops->skbuff_optptr(skb);
  569. return ret_val;
  570. }
  571. /**
  572. * calipso_getattr - Get the security attributes from a memory block.
  573. * @calipso: the CALIPSO option
  574. * @secattr: the security attributes
  575. *
  576. * Description:
  577. * Inspect @calipso and return the security attributes in @secattr.
  578. * Returns zero on success and negative values on failure.
  579. *
  580. */
  581. int calipso_getattr(const unsigned char *calipso,
  582. struct netlbl_lsm_secattr *secattr)
  583. {
  584. int ret_val = -ENOMSG;
  585. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  586. if (ops)
  587. ret_val = ops->opt_getattr(calipso, secattr);
  588. return ret_val;
  589. }
  590. /**
  591. * calipso_skbuff_setattr - Set the CALIPSO option on a packet
  592. * @skb: the packet
  593. * @doi_def: the CALIPSO DOI to use
  594. * @secattr: the security attributes
  595. *
  596. * Description:
  597. * Set the CALIPSO option on the given packet based on the security attributes.
  598. * Returns a pointer to the IP header on success and NULL on failure.
  599. *
  600. */
  601. int calipso_skbuff_setattr(struct sk_buff *skb,
  602. const struct calipso_doi *doi_def,
  603. const struct netlbl_lsm_secattr *secattr)
  604. {
  605. int ret_val = -ENOMSG;
  606. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  607. if (ops)
  608. ret_val = ops->skbuff_setattr(skb, doi_def, secattr);
  609. return ret_val;
  610. }
  611. /**
  612. * calipso_skbuff_delattr - Delete any CALIPSO options from a packet
  613. * @skb: the packet
  614. *
  615. * Description:
  616. * Removes any and all CALIPSO options from the given packet. Returns zero on
  617. * success, negative values on failure.
  618. *
  619. */
  620. int calipso_skbuff_delattr(struct sk_buff *skb)
  621. {
  622. int ret_val = -ENOMSG;
  623. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  624. if (ops)
  625. ret_val = ops->skbuff_delattr(skb);
  626. return ret_val;
  627. }
  628. /**
  629. * calipso_cache_invalidate - Invalidates the current CALIPSO cache
  630. *
  631. * Description:
  632. * Invalidates and frees any entries in the CALIPSO cache. Returns zero on
  633. * success and negative values on failure.
  634. *
  635. */
  636. void calipso_cache_invalidate(void)
  637. {
  638. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  639. if (ops)
  640. ops->cache_invalidate();
  641. }
  642. /**
  643. * calipso_cache_add - Add an entry to the CALIPSO cache
  644. * @calipso_ptr: the CALIPSO option
  645. * @secattr: the packet's security attributes
  646. *
  647. * Description:
  648. * Add a new entry into the CALIPSO label mapping cache.
  649. * Returns zero on success, negative values on failure.
  650. *
  651. */
  652. int calipso_cache_add(const unsigned char *calipso_ptr,
  653. const struct netlbl_lsm_secattr *secattr)
  654. {
  655. int ret_val = -ENOMSG;
  656. const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
  657. if (ops)
  658. ret_val = ops->cache_add(calipso_ptr, secattr);
  659. return ret_val;
  660. }