netlabel_kapi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_socket_getattr - Determine the security attributes of a socket
  81. * @sock: the socket
  82. * @secattr: the security attributes
  83. *
  84. * Description:
  85. * Examines the given socket to see any NetLabel style labeling has been
  86. * applied to the socket, 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_socket_getattr(const struct socket *sock,
  92. struct netlbl_lsm_secattr *secattr)
  93. {
  94. int ret_val;
  95. ret_val = cipso_v4_socket_getattr(sock, secattr);
  96. if (ret_val == 0)
  97. return 0;
  98. return netlbl_unlabel_getattr(secattr);
  99. }
  100. /**
  101. * netlbl_skbuff_getattr - Determine the security attributes of a packet
  102. * @skb: the packet
  103. * @secattr: the security attributes
  104. *
  105. * Description:
  106. * Examines the given packet to see if a recognized form of packet labeling
  107. * is present, if so it parses the packet label and returns the security
  108. * attributes in @secattr. Returns zero on success, negative values on
  109. * failure.
  110. *
  111. */
  112. int netlbl_skbuff_getattr(const struct sk_buff *skb,
  113. struct netlbl_lsm_secattr *secattr)
  114. {
  115. int ret_val;
  116. ret_val = cipso_v4_skbuff_getattr(skb, secattr);
  117. if (ret_val == 0)
  118. return 0;
  119. return netlbl_unlabel_getattr(secattr);
  120. }
  121. /**
  122. * netlbl_skbuff_err - Handle a LSM error on a sk_buff
  123. * @skb: the packet
  124. * @error: the error code
  125. *
  126. * Description:
  127. * Deal with a LSM problem when handling the packet in @skb, typically this is
  128. * a permission denied problem (-EACCES). The correct action is determined
  129. * according to the packet's labeling protocol.
  130. *
  131. */
  132. void netlbl_skbuff_err(struct sk_buff *skb, int error)
  133. {
  134. if (CIPSO_V4_OPTEXIST(skb))
  135. cipso_v4_error(skb, error, 0);
  136. }
  137. /**
  138. * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
  139. *
  140. * Description:
  141. * For all of the NetLabel protocols that support some form of label mapping
  142. * cache, invalidate the cache. Returns zero on success, negative values on
  143. * error.
  144. *
  145. */
  146. void netlbl_cache_invalidate(void)
  147. {
  148. cipso_v4_cache_invalidate();
  149. }
  150. /**
  151. * netlbl_cache_add - Add an entry to a NetLabel protocol cache
  152. * @skb: the packet
  153. * @secattr: the packet's security attributes
  154. *
  155. * Description:
  156. * Add the LSM security attributes for the given packet to the underlying
  157. * NetLabel protocol's label mapping cache. Returns zero on success, negative
  158. * values on error.
  159. *
  160. */
  161. int netlbl_cache_add(const struct sk_buff *skb,
  162. const struct netlbl_lsm_secattr *secattr)
  163. {
  164. if (secattr->cache.data == NULL)
  165. return -ENOMSG;
  166. if (CIPSO_V4_OPTEXIST(skb))
  167. return cipso_v4_cache_add(skb, secattr);
  168. return -ENOMSG;
  169. }
  170. /*
  171. * Setup Functions
  172. */
  173. /**
  174. * netlbl_init - Initialize NetLabel
  175. *
  176. * Description:
  177. * Perform the required NetLabel initialization before first use.
  178. *
  179. */
  180. static int __init netlbl_init(void)
  181. {
  182. int ret_val;
  183. printk(KERN_INFO "NetLabel: Initializing\n");
  184. printk(KERN_INFO "NetLabel: domain hash size = %u\n",
  185. (1 << NETLBL_DOMHSH_BITSIZE));
  186. printk(KERN_INFO "NetLabel: protocols ="
  187. " UNLABELED"
  188. " CIPSOv4"
  189. "\n");
  190. ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
  191. if (ret_val != 0)
  192. goto init_failure;
  193. ret_val = netlbl_netlink_init();
  194. if (ret_val != 0)
  195. goto init_failure;
  196. ret_val = netlbl_unlabel_defconf();
  197. if (ret_val != 0)
  198. goto init_failure;
  199. printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
  200. return 0;
  201. init_failure:
  202. panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
  203. }
  204. subsys_initcall(netlbl_init);