cipso_ipv4.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <asm/unaligned.h>
  42. /* known doi values */
  43. #define CIPSO_V4_DOI_UNKNOWN 0x00000000
  44. /* standard tag types */
  45. #define CIPSO_V4_TAG_INVALID 0
  46. #define CIPSO_V4_TAG_RBITMAP 1
  47. #define CIPSO_V4_TAG_ENUM 2
  48. #define CIPSO_V4_TAG_RANGE 5
  49. #define CIPSO_V4_TAG_PBITMAP 6
  50. #define CIPSO_V4_TAG_FREEFORM 7
  51. /* non-standard tag types (tags > 127) */
  52. #define CIPSO_V4_TAG_LOCAL 128
  53. /* doi mapping types */
  54. #define CIPSO_V4_MAP_UNKNOWN 0
  55. #define CIPSO_V4_MAP_TRANS 1
  56. #define CIPSO_V4_MAP_PASS 2
  57. #define CIPSO_V4_MAP_LOCAL 3
  58. /* limits */
  59. #define CIPSO_V4_MAX_REM_LVLS 255
  60. #define CIPSO_V4_INV_LVL 0x80000000
  61. #define CIPSO_V4_MAX_LOC_LVLS (CIPSO_V4_INV_LVL - 1)
  62. #define CIPSO_V4_MAX_REM_CATS 65534
  63. #define CIPSO_V4_INV_CAT 0x80000000
  64. #define CIPSO_V4_MAX_LOC_CATS (CIPSO_V4_INV_CAT - 1)
  65. /*
  66. * CIPSO DOI definitions
  67. */
  68. /* DOI definition struct */
  69. #define CIPSO_V4_TAG_MAXCNT 5
  70. struct cipso_v4_doi {
  71. u32 doi;
  72. u32 type;
  73. union {
  74. struct cipso_v4_std_map_tbl *std;
  75. } map;
  76. u8 tags[CIPSO_V4_TAG_MAXCNT];
  77. atomic_t refcount;
  78. struct list_head list;
  79. struct rcu_head rcu;
  80. };
  81. /* Standard CIPSO mapping table */
  82. /* NOTE: the highest order bit (i.e. 0x80000000) is an 'invalid' flag, if the
  83. * bit is set then consider that value as unspecified, meaning the
  84. * mapping for that particular level/category is invalid */
  85. struct cipso_v4_std_map_tbl {
  86. struct {
  87. u32 *cipso;
  88. u32 *local;
  89. u32 cipso_size;
  90. u32 local_size;
  91. } lvl;
  92. struct {
  93. u32 *cipso;
  94. u32 *local;
  95. u32 cipso_size;
  96. u32 local_size;
  97. } cat;
  98. };
  99. /*
  100. * Sysctl Variables
  101. */
  102. #ifdef CONFIG_NETLABEL
  103. extern int cipso_v4_cache_enabled;
  104. extern int cipso_v4_cache_bucketsize;
  105. extern int cipso_v4_rbm_optfmt;
  106. extern int cipso_v4_rbm_strictvalid;
  107. #endif
  108. /*
  109. * DOI List Functions
  110. */
  111. #ifdef CONFIG_NETLABEL
  112. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  113. struct netlbl_audit *audit_info);
  114. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def);
  115. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info);
  116. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi);
  117. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def);
  118. int cipso_v4_doi_walk(u32 *skip_cnt,
  119. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  120. void *cb_arg);
  121. #else
  122. static inline int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  123. struct netlbl_audit *audit_info)
  124. {
  125. return -ENOSYS;
  126. }
  127. static inline void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  128. {
  129. return;
  130. }
  131. static inline int cipso_v4_doi_remove(u32 doi,
  132. struct netlbl_audit *audit_info)
  133. {
  134. return 0;
  135. }
  136. static inline struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  137. {
  138. return NULL;
  139. }
  140. static inline int cipso_v4_doi_walk(u32 *skip_cnt,
  141. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  142. void *cb_arg)
  143. {
  144. return 0;
  145. }
  146. static inline int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def,
  147. const char *domain)
  148. {
  149. return -ENOSYS;
  150. }
  151. static inline int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  152. const char *domain)
  153. {
  154. return 0;
  155. }
  156. #endif /* CONFIG_NETLABEL */
  157. /*
  158. * Label Mapping Cache Functions
  159. */
  160. #ifdef CONFIG_NETLABEL
  161. void cipso_v4_cache_invalidate(void);
  162. int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  163. const struct netlbl_lsm_secattr *secattr);
  164. #else
  165. static inline void cipso_v4_cache_invalidate(void)
  166. {
  167. return;
  168. }
  169. static inline int cipso_v4_cache_add(const unsigned char *cipso_ptr,
  170. const struct netlbl_lsm_secattr *secattr)
  171. {
  172. return 0;
  173. }
  174. #endif /* CONFIG_NETLABEL */
  175. /*
  176. * Protocol Handling Functions
  177. */
  178. #ifdef CONFIG_NETLABEL
  179. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway);
  180. int cipso_v4_getattr(const unsigned char *cipso,
  181. struct netlbl_lsm_secattr *secattr);
  182. int cipso_v4_sock_setattr(struct sock *sk,
  183. const struct cipso_v4_doi *doi_def,
  184. const struct netlbl_lsm_secattr *secattr);
  185. void cipso_v4_sock_delattr(struct sock *sk);
  186. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
  187. int cipso_v4_req_setattr(struct request_sock *req,
  188. const struct cipso_v4_doi *doi_def,
  189. const struct netlbl_lsm_secattr *secattr);
  190. void cipso_v4_req_delattr(struct request_sock *req);
  191. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  192. const struct cipso_v4_doi *doi_def,
  193. const struct netlbl_lsm_secattr *secattr);
  194. int cipso_v4_skbuff_delattr(struct sk_buff *skb);
  195. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  196. struct netlbl_lsm_secattr *secattr);
  197. unsigned char *cipso_v4_optptr(const struct sk_buff *skb);
  198. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option);
  199. #else
  200. static inline void cipso_v4_error(struct sk_buff *skb,
  201. int error,
  202. u32 gateway)
  203. {
  204. return;
  205. }
  206. static inline int cipso_v4_getattr(const unsigned char *cipso,
  207. struct netlbl_lsm_secattr *secattr)
  208. {
  209. return -ENOSYS;
  210. }
  211. static inline int cipso_v4_sock_setattr(struct sock *sk,
  212. const struct cipso_v4_doi *doi_def,
  213. const struct netlbl_lsm_secattr *secattr)
  214. {
  215. return -ENOSYS;
  216. }
  217. static inline void cipso_v4_sock_delattr(struct sock *sk)
  218. {
  219. }
  220. static inline int cipso_v4_sock_getattr(struct sock *sk,
  221. struct netlbl_lsm_secattr *secattr)
  222. {
  223. return -ENOSYS;
  224. }
  225. static inline int cipso_v4_req_setattr(struct request_sock *req,
  226. const struct cipso_v4_doi *doi_def,
  227. const struct netlbl_lsm_secattr *secattr)
  228. {
  229. return -ENOSYS;
  230. }
  231. static inline void cipso_v4_req_delattr(struct request_sock *req)
  232. {
  233. return;
  234. }
  235. static inline int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  236. const struct cipso_v4_doi *doi_def,
  237. const struct netlbl_lsm_secattr *secattr)
  238. {
  239. return -ENOSYS;
  240. }
  241. static inline int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  242. {
  243. return -ENOSYS;
  244. }
  245. static inline int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  246. struct netlbl_lsm_secattr *secattr)
  247. {
  248. return -ENOSYS;
  249. }
  250. static inline unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  251. {
  252. return NULL;
  253. }
  254. static inline int cipso_v4_validate(const struct sk_buff *skb,
  255. unsigned char **option)
  256. {
  257. unsigned char *opt = *option;
  258. unsigned char err_offset = 0;
  259. u8 opt_len = opt[1];
  260. u8 opt_iter;
  261. u8 tag_len;
  262. if (opt_len < 8) {
  263. err_offset = 1;
  264. goto out;
  265. }
  266. if (get_unaligned_be32(&opt[2]) == 0) {
  267. err_offset = 2;
  268. goto out;
  269. }
  270. for (opt_iter = 6; opt_iter < opt_len;) {
  271. if (opt_iter + 1 == opt_len) {
  272. err_offset = opt_iter;
  273. goto out;
  274. }
  275. tag_len = opt[opt_iter + 1];
  276. if ((tag_len == 0) || (tag_len > (opt_len - opt_iter))) {
  277. err_offset = opt_iter + 1;
  278. goto out;
  279. }
  280. opt_iter += tag_len;
  281. }
  282. out:
  283. *option = opt + err_offset;
  284. return err_offset;
  285. }
  286. #endif /* CONFIG_NETLABEL */
  287. #endif /* _CIPSO_IPV4_H */