cipso_ipv4.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * CIPSO - Commercial IP Security Option
  3. *
  4. * This is an implementation of the CIPSO 2.2 protocol as specified in
  5. * draft-ietf-cipso-ipsecurity-01.txt with additional tag types as found in
  6. * FIPS-188, copies of both documents can be found in the Documentation
  7. * directory. While CIPSO never became a full IETF RFC standard many vendors
  8. * have chosen to adopt the protocol and over the years it has become a
  9. * de-facto standard for labeled networking.
  10. *
  11. * Author: Paul Moore <paul@paul-moore.com>
  12. *
  13. */
  14. /*
  15. * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  25. * the GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. #ifndef _CIPSO_IPV4_H
  32. #define _CIPSO_IPV4_H
  33. #include <linux/types.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/list.h>
  36. #include <linux/net.h>
  37. #include <linux/skbuff.h>
  38. #include <net/netlabel.h>
  39. #include <net/request_sock.h>
  40. #include <linux/atomic.h>
  41. #include <linux/refcount.h>
  42. #include <asm/unaligned.h>
  43. /* known doi values */
  44. #define CIPSO_V4_DOI_UNKNOWN 0x00000000
  45. /* standard tag types */
  46. #define CIPSO_V4_TAG_INVALID 0
  47. #define CIPSO_V4_TAG_RBITMAP 1
  48. #define CIPSO_V4_TAG_ENUM 2
  49. #define CIPSO_V4_TAG_RANGE 5
  50. #define CIPSO_V4_TAG_PBITMAP 6
  51. #define CIPSO_V4_TAG_FREEFORM 7
  52. /* non-standard tag types (tags > 127) */
  53. #define CIPSO_V4_TAG_LOCAL 128
  54. /* doi mapping types */
  55. #define CIPSO_V4_MAP_UNKNOWN 0
  56. #define CIPSO_V4_MAP_TRANS 1
  57. #define CIPSO_V4_MAP_PASS 2
  58. #define CIPSO_V4_MAP_LOCAL 3
  59. /* limits */
  60. #define CIPSO_V4_MAX_REM_LVLS 255
  61. #define CIPSO_V4_INV_LVL 0x80000000
  62. #define CIPSO_V4_MAX_LOC_LVLS (CIPSO_V4_INV_LVL - 1)
  63. #define CIPSO_V4_MAX_REM_CATS 65534
  64. #define CIPSO_V4_INV_CAT 0x80000000
  65. #define CIPSO_V4_MAX_LOC_CATS (CIPSO_V4_INV_CAT - 1)
  66. /*
  67. * CIPSO DOI definitions
  68. */
  69. /* DOI definition struct */
  70. #define CIPSO_V4_TAG_MAXCNT 5
  71. struct cipso_v4_doi {
  72. u32 doi;
  73. u32 type;
  74. union {
  75. struct cipso_v4_std_map_tbl *std;
  76. } map;
  77. u8 tags[CIPSO_V4_TAG_MAXCNT];
  78. refcount_t refcount;
  79. struct list_head list;
  80. struct rcu_head rcu;
  81. };
  82. /* Standard CIPSO mapping table */
  83. /* NOTE: the highest order bit (i.e. 0x80000000) is an 'invalid' flag, if the
  84. * bit is set then consider that value as unspecified, meaning the
  85. * mapping for that particular level/category is invalid */
  86. struct cipso_v4_std_map_tbl {
  87. struct {
  88. u32 *cipso;
  89. u32 *local;
  90. u32 cipso_size;
  91. u32 local_size;
  92. } lvl;
  93. struct {
  94. u32 *cipso;
  95. u32 *local;
  96. u32 cipso_size;
  97. u32 local_size;
  98. } cat;
  99. };
  100. /*
  101. * Sysctl Variables
  102. */
  103. #ifdef CONFIG_NETLABEL
  104. extern int cipso_v4_cache_enabled;
  105. extern int cipso_v4_cache_bucketsize;
  106. extern int cipso_v4_rbm_optfmt;
  107. extern int cipso_v4_rbm_strictvalid;
  108. #endif
  109. /*
  110. * DOI List Functions
  111. */
  112. #ifdef CONFIG_NETLABEL
  113. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  114. struct netlbl_audit *audit_info);
  115. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def);
  116. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info);
  117. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi);
  118. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def);
  119. int cipso_v4_doi_walk(u32 *skip_cnt,
  120. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  121. void *cb_arg);
  122. #else
  123. static inline int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  124. struct netlbl_audit *audit_info)
  125. {
  126. return -ENOSYS;
  127. }
  128. static inline void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  129. {
  130. return;
  131. }
  132. static inline int cipso_v4_doi_remove(u32 doi,
  133. struct netlbl_audit *audit_info)
  134. {
  135. return 0;
  136. }
  137. static inline struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  138. {
  139. return NULL;
  140. }
  141. static inline int cipso_v4_doi_walk(u32 *skip_cnt,
  142. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  143. void *cb_arg)
  144. {
  145. return 0;
  146. }
  147. static inline int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def,
  148. const char *domain)
  149. {
  150. return -ENOSYS;
  151. }
  152. static inline int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  153. const char *domain)
  154. {
  155. return 0;
  156. }
  157. #endif /* CONFIG_NETLABEL */
  158. /*
  159. * Label Mapping Cache Functions
  160. */
  161. #ifdef CONFIG_NETLABEL
  162. void cipso_v4_cache_invalidate(void);
  163. int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  164. const struct netlbl_lsm_secattr *secattr);
  165. #else
  166. static inline void cipso_v4_cache_invalidate(void)
  167. {
  168. return;
  169. }
  170. static inline int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  171. const struct netlbl_lsm_secattr *secattr)
  172. {
  173. return 0;
  174. }
  175. #endif /* CONFIG_NETLABEL */
  176. /*
  177. * Protocol Handling Functions
  178. */
  179. #ifdef CONFIG_NETLABEL
  180. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway);
  181. int cipso_v4_getattr(const unsigned char *cipso,
  182. struct netlbl_lsm_secattr *secattr);
  183. int cipso_v4_sock_setattr(struct sock *sk,
  184. const struct cipso_v4_doi *doi_def,
  185. const struct netlbl_lsm_secattr *secattr);
  186. void cipso_v4_sock_delattr(struct sock *sk);
  187. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
  188. int cipso_v4_req_setattr(struct request_sock *req,
  189. const struct cipso_v4_doi *doi_def,
  190. const struct netlbl_lsm_secattr *secattr);
  191. void cipso_v4_req_delattr(struct request_sock *req);
  192. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  193. const struct cipso_v4_doi *doi_def,
  194. const struct netlbl_lsm_secattr *secattr);
  195. int cipso_v4_skbuff_delattr(struct sk_buff *skb);
  196. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  197. struct netlbl_lsm_secattr *secattr);
  198. unsigned char *cipso_v4_optptr(const struct sk_buff *skb);
  199. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option);
  200. #else
  201. static inline void cipso_v4_error(struct sk_buff *skb,
  202. int error,
  203. u32 gateway)
  204. {
  205. return;
  206. }
  207. static inline int cipso_v4_getattr(const unsigned char *cipso,
  208. struct netlbl_lsm_secattr *secattr)
  209. {
  210. return -ENOSYS;
  211. }
  212. static inline int cipso_v4_sock_setattr(struct sock *sk,
  213. const struct cipso_v4_doi *doi_def,
  214. const struct netlbl_lsm_secattr *secattr)
  215. {
  216. return -ENOSYS;
  217. }
  218. static inline void cipso_v4_sock_delattr(struct sock *sk)
  219. {
  220. }
  221. static inline int cipso_v4_sock_getattr(struct sock *sk,
  222. struct netlbl_lsm_secattr *secattr)
  223. {
  224. return -ENOSYS;
  225. }
  226. static inline int cipso_v4_req_setattr(struct request_sock *req,
  227. const struct cipso_v4_doi *doi_def,
  228. const struct netlbl_lsm_secattr *secattr)
  229. {
  230. return -ENOSYS;
  231. }
  232. static inline void cipso_v4_req_delattr(struct request_sock *req)
  233. {
  234. return;
  235. }
  236. static inline int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  237. const struct cipso_v4_doi *doi_def,
  238. const struct netlbl_lsm_secattr *secattr)
  239. {
  240. return -ENOSYS;
  241. }
  242. static inline int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  243. {
  244. return -ENOSYS;
  245. }
  246. static inline int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  247. struct netlbl_lsm_secattr *secattr)
  248. {
  249. return -ENOSYS;
  250. }
  251. static inline unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  252. {
  253. return NULL;
  254. }
  255. static inline int cipso_v4_validate(const struct sk_buff *skb,
  256. unsigned char **option)
  257. {
  258. unsigned char *opt = *option;
  259. unsigned char err_offset = 0;
  260. u8 opt_len = opt[1];
  261. u8 opt_iter;
  262. u8 tag_len;
  263. if (opt_len < 8) {
  264. err_offset = 1;
  265. goto out;
  266. }
  267. if (get_unaligned_be32(&opt[2]) == 0) {
  268. err_offset = 2;
  269. goto out;
  270. }
  271. for (opt_iter = 6; opt_iter < opt_len;) {
  272. if (opt_iter + 1 == opt_len) {
  273. err_offset = opt_iter;
  274. goto out;
  275. }
  276. tag_len = opt[opt_iter + 1];
  277. if ((tag_len == 0) || (tag_len > (opt_len - opt_iter))) {
  278. err_offset = opt_iter + 1;
  279. goto out;
  280. }
  281. opt_iter += tag_len;
  282. }
  283. out:
  284. *option = opt + err_offset;
  285. return err_offset;
  286. }
  287. #endif /* CONFIG_NETLABEL */
  288. #endif /* _CIPSO_IPV4_H */