netlabel_user.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * NetLabel NETLINK Interface
  3. *
  4. * This file defines the NETLINK interface for the NetLabel system. The
  5. * NetLabel system manages static and dynamic label mappings for network
  6. * 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/init.h>
  30. #include <linux/types.h>
  31. #include <linux/list.h>
  32. #include <linux/socket.h>
  33. #include <net/sock.h>
  34. #include <net/netlink.h>
  35. #include <net/genetlink.h>
  36. #include <net/netlabel.h>
  37. #include <asm/bug.h>
  38. #include "netlabel_mgmt.h"
  39. #include "netlabel_unlabeled.h"
  40. #include "netlabel_cipso_v4.h"
  41. #include "netlabel_user.h"
  42. /*
  43. * NetLabel NETLINK Setup Functions
  44. */
  45. /**
  46. * netlbl_netlink_init - Initialize the NETLINK communication channel
  47. *
  48. * Description:
  49. * Call out to the NetLabel components so they can register their families and
  50. * commands with the Generic NETLINK mechanism. Returns zero on success and
  51. * non-zero on failure.
  52. *
  53. */
  54. int netlbl_netlink_init(void)
  55. {
  56. int ret_val;
  57. ret_val = netlbl_mgmt_genl_init();
  58. if (ret_val != 0)
  59. return ret_val;
  60. ret_val = netlbl_cipsov4_genl_init();
  61. if (ret_val != 0)
  62. return ret_val;
  63. ret_val = netlbl_unlabel_genl_init();
  64. if (ret_val != 0)
  65. return ret_val;
  66. return 0;
  67. }
  68. /*
  69. * NetLabel Common Protocol Functions
  70. */
  71. /**
  72. * netlbl_netlink_send_ack - Send an ACK message
  73. * @info: the generic NETLINK information
  74. * @genl_family: the generic NETLINK family ID value
  75. * @ack_cmd: the generic NETLINK family ACK command value
  76. * @ret_code: return code to use
  77. *
  78. * Description:
  79. * This function sends an ACK message to the sender of the NETLINK message
  80. * specified by @info.
  81. *
  82. */
  83. void netlbl_netlink_send_ack(const struct genl_info *info,
  84. u32 genl_family,
  85. u8 ack_cmd,
  86. u32 ret_code)
  87. {
  88. size_t data_size;
  89. struct sk_buff *skb;
  90. data_size = GENL_HDRLEN + 2 * NETLBL_LEN_U32;
  91. skb = netlbl_netlink_alloc_skb(0, data_size, GFP_KERNEL);
  92. if (skb == NULL)
  93. return;
  94. if (netlbl_netlink_hdr_put(skb,
  95. info->snd_pid,
  96. 0,
  97. genl_family,
  98. ack_cmd) == NULL)
  99. goto send_ack_failure;
  100. if (nla_put_u32(skb, NLA_U32, info->snd_seq) != 0)
  101. goto send_ack_failure;
  102. if (nla_put_u32(skb, NLA_U32, ret_code) != 0)
  103. goto send_ack_failure;
  104. netlbl_netlink_snd(skb, info->snd_pid);
  105. return;
  106. send_ack_failure:
  107. kfree_skb(skb);
  108. }
  109. /*
  110. * NETLINK I/O Functions
  111. */
  112. /**
  113. * netlbl_netlink_snd - Send a NetLabel message
  114. * @skb: NetLabel message
  115. * @pid: destination PID
  116. *
  117. * Description:
  118. * Sends a unicast NetLabel message over the NETLINK socket.
  119. *
  120. */
  121. int netlbl_netlink_snd(struct sk_buff *skb, u32 pid)
  122. {
  123. return genlmsg_unicast(skb, pid);
  124. }
  125. /**
  126. * netlbl_netlink_snd - Send a NetLabel message
  127. * @skb: NetLabel message
  128. * @pid: sending PID
  129. * @group: multicast group id
  130. *
  131. * Description:
  132. * Sends a multicast NetLabel message over the NETLINK socket to all members
  133. * of @group except @pid.
  134. *
  135. */
  136. int netlbl_netlink_snd_multicast(struct sk_buff *skb, u32 pid, u32 group)
  137. {
  138. return genlmsg_multicast(skb, pid, group, GFP_KERNEL);
  139. }