netlabel_calipso.c 19 KB

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