netlabel_kapi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * NetLabel Kernel API
  3. *
  4. * This file defines the kernel API for the NetLabel system. The NetLabel
  5. * system manages static and dynamic label mappings for network protocols such
  6. * as CIPSO and RIPSO.
  7. *
  8. * Author: Paul Moore <paul.moore@hp.com>
  9. *
  10. */
  11. /*
  12. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  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, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <net/ip.h>
  32. #include <net/netlabel.h>
  33. #include <net/cipso_ipv4.h>
  34. #include <asm/bug.h>
  35. #include "netlabel_domainhash.h"
  36. #include "netlabel_unlabeled.h"
  37. #include "netlabel_user.h"
  38. /*
  39. * LSM Functions
  40. */
  41. /**
  42. * netlbl_socket_setattr - Label a socket using the correct protocol
  43. * @sock: the socket to label
  44. * @secattr: the security attributes
  45. *
  46. * Description:
  47. * Attach the correct label to the given socket using the security attributes
  48. * specified in @secattr. This function requires exclusive access to
  49. * @sock->sk, which means it either needs to be in the process of being
  50. * created or locked via lock_sock(sock->sk). Returns zero on success,
  51. * negative values on failure.
  52. *
  53. */
  54. int netlbl_socket_setattr(const struct socket *sock,
  55. const struct netlbl_lsm_secattr *secattr)
  56. {
  57. int ret_val = -ENOENT;
  58. struct netlbl_dom_map *dom_entry;
  59. rcu_read_lock();
  60. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  61. if (dom_entry == NULL)
  62. goto socket_setattr_return;
  63. switch (dom_entry->type) {
  64. case NETLBL_NLTYPE_CIPSOV4:
  65. ret_val = cipso_v4_socket_setattr(sock,
  66. dom_entry->type_def.cipsov4,
  67. secattr);
  68. break;
  69. case NETLBL_NLTYPE_UNLABELED:
  70. ret_val = 0;
  71. break;
  72. default:
  73. ret_val = -ENOENT;
  74. }
  75. socket_setattr_return:
  76. rcu_read_unlock();
  77. return ret_val;
  78. }
  79. /**
  80. * netlbl_sock_getattr - Determine the security attributes of a sock
  81. * @sk: the sock
  82. * @secattr: the security attributes
  83. *
  84. * Description:
  85. * Examines the given sock to see any NetLabel style labeling has been
  86. * applied to the sock, if so it parses the socket label and returns the
  87. * security attributes in @secattr. Returns zero on success, negative values
  88. * on failure.
  89. *
  90. */
  91. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  92. {
  93. int ret_val;
  94. ret_val = cipso_v4_sock_getattr(sk, secattr);
  95. if (ret_val == 0)
  96. return 0;
  97. return netlbl_unlabel_getattr(secattr);
  98. }
  99. /**
  100. * netlbl_socket_getattr - Determine the security attributes of a socket
  101. * @sock: the socket
  102. * @secattr: the security attributes
  103. *
  104. * Description:
  105. * Examines the given socket to see any NetLabel style labeling has been
  106. * applied to the socket, if so it parses the socket label and returns the
  107. * security attributes in @secattr. Returns zero on success, negative values
  108. * on failure.
  109. *
  110. */
  111. int netlbl_socket_getattr(const struct socket *sock,
  112. struct netlbl_lsm_secattr *secattr)
  113. {
  114. int ret_val;
  115. ret_val = cipso_v4_socket_getattr(sock, secattr);
  116. if (ret_val == 0)
  117. return 0;
  118. return netlbl_unlabel_getattr(secattr);
  119. }
  120. /**
  121. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  122. * @skb: the packet
  123. * @secattr: the security attributes
  124. *
  125. * Description:
  126. * Examines the given packet to see if a recognized form of packet labeling
  127. * is present, if so it parses the packet label and returns the security
  128. * attributes in @secattr. Returns zero on success, negative values on
  129. * failure.
  130. *
  131. */
  132. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  133. struct netlbl_lsm_secattr *secattr)
  134. {
  135. int ret_val;
  136. ret_val = cipso_v4_skbuff_getattr(skb, secattr);
  137. if (ret_val == 0)
  138. return 0;
  139. return netlbl_unlabel_getattr(secattr);
  140. }
  141. /**
  142. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  143. * @skb: the packet
  144. * @error: the error code
  145. *
  146. * Description:
  147. * Deal with a LSM problem when handling the packet in @skb, typically this is
  148. * a permission denied problem (-EACCES). The correct action is determined
  149. * according to the packet's labeling protocol.
  150. *
  151. */
  152. void netlbl_skbuff_err(struct sk_buff *skb, int error)
  153. {
  154. if (CIPSO_V4_OPTEXIST(skb))
  155. cipso_v4_error(skb, error, 0);
  156. }
  157. /**
  158. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  159. *
  160. * Description:
  161. * For all of the NetLabel protocols that support some form of label mapping
  162. * cache, invalidate the cache. Returns zero on success, negative values on
  163. * error.
  164. *
  165. */
  166. void netlbl_cache_invalidate(void)
  167. {
  168. cipso_v4_cache_invalidate();
  169. }
  170. /**
  171. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  172. * @skb: the packet
  173. * @secattr: the packet's security attributes
  174. *
  175. * Description:
  176. * Add the LSM security attributes for the given packet to the underlying
  177. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  178. * values on error.
  179. *
  180. */
  181. int netlbl_cache_add(const struct sk_buff *skb,
  182. const struct netlbl_lsm_secattr *secattr)
  183. {
  184. if (secattr->cache.data == NULL)
  185. return -ENOMSG;
  186. if (CIPSO_V4_OPTEXIST(skb))
  187. return cipso_v4_cache_add(skb, secattr);
  188. return -ENOMSG;
  189. }
  190. /*
  191. * Setup Functions
  192. */
  193. /**
  194. * netlbl_init - Initialize NetLabel
  195. *
  196. * Description:
  197. * Perform the required NetLabel initialization before first use.
  198. *
  199. */
  200. static int __init netlbl_init(void)
  201. {
  202. int ret_val;
  203. printk(KERN_INFO "NetLabel: Initializing\n");
  204. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  205. (1 << NETLBL_DOMHSH_BITSIZE));
  206. printk(KERN_INFO "NetLabel: protocols ="
  207. " UNLABELED"
  208. " CIPSOv4"
  209. "\n");
  210. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  211. if (ret_val != 0)
  212. goto init_failure;
  213. ret_val = netlbl_netlink_init();
  214. if (ret_val != 0)
  215. goto init_failure;
  216. ret_val = netlbl_unlabel_defconf();
  217. if (ret_val != 0)
  218. goto init_failure;
  219. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  220. return 0;
  221. init_failure:
  222. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  223. }
  224. subsys_initcall(netlbl_init);