netlabel_unlabeled.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * NetLabel Unlabeled Support
  3. *
  4. * This file defines functions for dealing with unlabeled packets for the
  5. * NetLabel system. The NetLabel system manages static and dynamic label
  6. * mappings for network protocols such 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/types.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/list.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/socket.h>
  34. #include <linux/string.h>
  35. #include <linux/skbuff.h>
  36. #include <net/sock.h>
  37. #include <net/netlink.h>
  38. #include <net/genetlink.h>
  39. #include <net/netlabel.h>
  40. #include <asm/bug.h>
  41. #include "netlabel_user.h"
  42. #include "netlabel_domainhash.h"
  43. #include "netlabel_unlabeled.h"
  44. /* Accept unlabeled packets flag */
  45. static atomic_t netlabel_unlabel_accept_flg = ATOMIC_INIT(0);
  46. /* NetLabel Generic NETLINK CIPSOv4 family */
  47. static struct genl_family netlbl_unlabel_gnl_family = {
  48. .id = GENL_ID_GENERATE,
  49. .hdrsize = 0,
  50. .name = NETLBL_NLTYPE_UNLABELED_NAME,
  51. .version = NETLBL_PROTO_VERSION,
  52. .maxattr = 0,
  53. };
  54. /*
  55. * NetLabel Command Handlers
  56. */
  57. /**
  58. * netlbl_unlabel_accept - Handle an ACCEPT message
  59. * @skb: the NETLINK buffer
  60. * @info: the Generic NETLINK info block
  61. *
  62. * Description:
  63. * Process a user generated ACCEPT message and set the accept flag accordingly.
  64. * Returns zero on success, negative values on failure.
  65. *
  66. */
  67. static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  68. {
  69. int ret_val;
  70. struct nlattr *data = netlbl_netlink_payload_data(skb);
  71. u32 value;
  72. ret_val = netlbl_netlink_cap_check(skb, CAP_NET_ADMIN);
  73. if (ret_val != 0)
  74. return ret_val;
  75. if (netlbl_netlink_payload_len(skb) == NETLBL_LEN_U32) {
  76. value = nla_get_u32(data);
  77. if (value == 1 || value == 0) {
  78. atomic_set(&netlabel_unlabel_accept_flg, value);
  79. netlbl_netlink_send_ack(info,
  80. netlbl_unlabel_gnl_family.id,
  81. NLBL_UNLABEL_C_ACK,
  82. NETLBL_E_OK);
  83. return 0;
  84. }
  85. }
  86. netlbl_netlink_send_ack(info,
  87. netlbl_unlabel_gnl_family.id,
  88. NLBL_UNLABEL_C_ACK,
  89. EINVAL);
  90. return -EINVAL;
  91. }
  92. /**
  93. * netlbl_unlabel_list - Handle a LIST message
  94. * @skb: the NETLINK buffer
  95. * @info: the Generic NETLINK info block
  96. *
  97. * Description:
  98. * Process a user generated LIST message and respond with the current status.
  99. * Returns zero on success, negative values on failure.
  100. *
  101. */
  102. static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  103. {
  104. int ret_val = -ENOMEM;
  105. struct sk_buff *ans_skb;
  106. ans_skb = netlbl_netlink_alloc_skb(0,
  107. GENL_HDRLEN + NETLBL_LEN_U32,
  108. GFP_KERNEL);
  109. if (ans_skb == NULL)
  110. goto list_failure;
  111. if (netlbl_netlink_hdr_put(ans_skb,
  112. info->snd_pid,
  113. 0,
  114. netlbl_unlabel_gnl_family.id,
  115. NLBL_UNLABEL_C_LIST) == NULL)
  116. goto list_failure;
  117. ret_val = nla_put_u32(ans_skb,
  118. NLA_U32,
  119. atomic_read(&netlabel_unlabel_accept_flg));
  120. if (ret_val != 0)
  121. goto list_failure;
  122. ret_val = netlbl_netlink_snd(ans_skb, info->snd_pid);
  123. if (ret_val != 0)
  124. goto list_failure;
  125. return 0;
  126. list_failure:
  127. netlbl_netlink_send_ack(info,
  128. netlbl_unlabel_gnl_family.id,
  129. NLBL_UNLABEL_C_ACK,
  130. -ret_val);
  131. return ret_val;
  132. }
  133. /*
  134. * NetLabel Generic NETLINK Command Definitions
  135. */
  136. static struct genl_ops netlbl_unlabel_genl_c_accept = {
  137. .cmd = NLBL_UNLABEL_C_ACCEPT,
  138. .flags = 0,
  139. .doit = netlbl_unlabel_accept,
  140. .dumpit = NULL,
  141. };
  142. static struct genl_ops netlbl_unlabel_genl_c_list = {
  143. .cmd = NLBL_UNLABEL_C_LIST,
  144. .flags = 0,
  145. .doit = netlbl_unlabel_list,
  146. .dumpit = NULL,
  147. };
  148. /*
  149. * NetLabel Generic NETLINK Protocol Functions
  150. */
  151. /**
  152. * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
  153. *
  154. * Description:
  155. * Register the unlabeled packet NetLabel component with the Generic NETLINK
  156. * mechanism. Returns zero on success, negative values on failure.
  157. *
  158. */
  159. int netlbl_unlabel_genl_init(void)
  160. {
  161. int ret_val;
  162. ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
  163. if (ret_val != 0)
  164. return ret_val;
  165. ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
  166. &netlbl_unlabel_genl_c_accept);
  167. if (ret_val != 0)
  168. return ret_val;
  169. ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
  170. &netlbl_unlabel_genl_c_list);
  171. if (ret_val != 0)
  172. return ret_val;
  173. return 0;
  174. }
  175. /*
  176. * NetLabel KAPI Hooks
  177. */
  178. /**
  179. * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
  180. * @secattr: the security attributes
  181. *
  182. * Description:
  183. * Determine the security attributes, if any, for an unlabled packet and return
  184. * them in @secattr. Returns zero on success and negative values on failure.
  185. *
  186. */
  187. int netlbl_unlabel_getattr(struct netlbl_lsm_secattr *secattr)
  188. {
  189. if (atomic_read(&netlabel_unlabel_accept_flg) == 1) {
  190. memset(secattr, 0, sizeof(*secattr));
  191. return 0;
  192. }
  193. return -ENOMSG;
  194. }
  195. /**
  196. * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
  197. *
  198. * Description:
  199. * Set the default NetLabel configuration to allow incoming unlabeled packets
  200. * and to send unlabeled network traffic by default.
  201. *
  202. */
  203. int netlbl_unlabel_defconf(void)
  204. {
  205. int ret_val;
  206. struct netlbl_dom_map *entry;
  207. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  208. if (entry == NULL)
  209. return -ENOMEM;
  210. entry->type = NETLBL_NLTYPE_UNLABELED;
  211. ret_val = netlbl_domhsh_add_default(entry);
  212. if (ret_val != 0)
  213. return ret_val;
  214. atomic_set(&netlabel_unlabel_accept_flg, 1);
  215. return 0;
  216. }