netlabel_calipso.c 19 KB

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