netlabel_kapi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if ((secattr->flags & NETLBL_SECATTR_DOMAIN) == 0)
  60. return -ENOENT;
  61. rcu_read_lock();
  62. dom_entry = netlbl_domhsh_getentry(secattr->domain);
  63. if (dom_entry == NULL)
  64. goto socket_setattr_return;
  65. switch (dom_entry->type) {
  66. case NETLBL_NLTYPE_CIPSOV4:
  67. ret_val = cipso_v4_socket_setattr(sock,
  68. dom_entry->type_def.cipsov4,
  69. secattr);
  70. break;
  71. case NETLBL_NLTYPE_UNLABELED:
  72. ret_val = 0;
  73. break;
  74. default:
  75. ret_val = -ENOENT;
  76. }
  77. socket_setattr_return:
  78. rcu_read_unlock();
  79. return ret_val;
  80. }
  81. /**
  82. * netlbl_sock_getattr - Determine the security attributes of a sock
  83. * @sk: the sock
  84. * @secattr: the security attributes
  85. *
  86. * Description:
  87. * Examines the given sock to see any NetLabel style labeling has been
  88. * applied to the sock, if so it parses the socket label and returns the
  89. * security attributes in @secattr. Returns zero on success, negative values
  90. * on failure.
  91. *
  92. */
  93. int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
  94. {
  95. int ret_val;
  96. ret_val = cipso_v4_sock_getattr(sk, secattr);
  97. if (ret_val == 0)
  98. return 0;
  99. return netlbl_unlabel_getattr(secattr);
  100. }
  101. /**
  102. * netlbl_socket_getattr - Determine the security attributes of a socket
  103. * @sock: the socket
  104. * @secattr: the security attributes
  105. *
  106. * Description:
  107. * Examines the given socket to see any NetLabel style labeling has been
  108. * applied to the socket, if so it parses the socket label and returns the
  109. * security attributes in @secattr. Returns zero on success, negative values
  110. * on failure.
  111. *
  112. */
  113. int netlbl_socket_getattr(const struct socket *sock,
  114. struct netlbl_lsm_secattr *secattr)
  115. {
  116. int ret_val;
  117. ret_val = cipso_v4_socket_getattr(sock, secattr);
  118. if (ret_val == 0)
  119. return 0;
  120. return netlbl_unlabel_getattr(secattr);
  121. }
  122. /**
  123. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  124. * @skb: the packet
  125. * @secattr: the security attributes
  126. *
  127. * Description:
  128. * Examines the given packet to see if a recognized form of packet labeling
  129. * is present, if so it parses the packet label and returns the security
  130. * attributes in @secattr. Returns zero on success, negative values on
  131. * failure.
  132. *
  133. */
  134. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  135. struct netlbl_lsm_secattr *secattr)
  136. {
  137. int ret_val;
  138. ret_val = cipso_v4_skbuff_getattr(skb, secattr);
  139. if (ret_val == 0)
  140. return 0;
  141. return netlbl_unlabel_getattr(secattr);
  142. }
  143. /**
  144. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  145. * @skb: the packet
  146. * @error: the error code
  147. *
  148. * Description:
  149. * Deal with a LSM problem when handling the packet in @skb, typically this is
  150. * a permission denied problem (-EACCES). The correct action is determined
  151. * according to the packet's labeling protocol.
  152. *
  153. */
  154. void netlbl_skbuff_err(struct sk_buff *skb, int error)
  155. {
  156. if (CIPSO_V4_OPTEXIST(skb))
  157. cipso_v4_error(skb, error, 0);
  158. }
  159. /**
  160. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  161. *
  162. * Description:
  163. * For all of the NetLabel protocols that support some form of label mapping
  164. * cache, invalidate the cache. Returns zero on success, negative values on
  165. * error.
  166. *
  167. */
  168. void netlbl_cache_invalidate(void)
  169. {
  170. cipso_v4_cache_invalidate();
  171. }
  172. /**
  173. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  174. * @skb: the packet
  175. * @secattr: the packet's security attributes
  176. *
  177. * Description:
  178. * Add the LSM security attributes for the given packet to the underlying
  179. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  180. * values on error.
  181. *
  182. */
  183. int netlbl_cache_add(const struct sk_buff *skb,
  184. const struct netlbl_lsm_secattr *secattr)
  185. {
  186. if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
  187. return -ENOMSG;
  188. if (CIPSO_V4_OPTEXIST(skb))
  189. return cipso_v4_cache_add(skb, secattr);
  190. return -ENOMSG;
  191. }
  192. /*
  193. * Setup Functions
  194. */
  195. /**
  196. * netlbl_init - Initialize NetLabel
  197. *
  198. * Description:
  199. * Perform the required NetLabel initialization before first use.
  200. *
  201. */
  202. static int __init netlbl_init(void)
  203. {
  204. int ret_val;
  205. printk(KERN_INFO "NetLabel: Initializing\n");
  206. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  207. (1 << NETLBL_DOMHSH_BITSIZE));
  208. printk(KERN_INFO "NetLabel: protocols ="
  209. " UNLABELED"
  210. " CIPSOv4"
  211. "\n");
  212. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  213. if (ret_val != 0)
  214. goto init_failure;
  215. ret_val = netlbl_netlink_init();
  216. if (ret_val != 0)
  217. goto init_failure;
  218. ret_val = netlbl_unlabel_defconf();
  219. if (ret_val != 0)
  220. goto init_failure;
  221. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  222. return 0;
  223. init_failure:
  224. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  225. }
  226. subsys_initcall(netlbl_init);