cipso_ipv4.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. */
  32. #ifndef _CIPSO_IPV4_H
  33. #define _CIPSO_IPV4_H
  34. #include <linux/types.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/list.h>
  37. #include <linux/net.h>
  38. #include <linux/skbuff.h>
  39. #include <net/netlabel.h>
  40. #include <net/request_sock.h>
  41. #include <linux/atomic.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. atomic_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. * Helper Functions
  111. */
  112. #define CIPSO_V4_OPTEXIST(x) (IPCB(x)->opt.cipso != 0)
  113. #define CIPSO_V4_OPTPTR(x) (skb_network_header(x) + IPCB(x)->opt.cipso)
  114. /*
  115. * DOI List Functions
  116. */
  117. #ifdef CONFIG_NETLABEL
  118. int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  119. struct netlbl_audit *audit_info);
  120. void cipso_v4_doi_free(struct cipso_v4_doi *doi_def);
  121. int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info);
  122. struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi);
  123. void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def);
  124. int cipso_v4_doi_walk(u32 *skip_cnt,
  125. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  126. void *cb_arg);
  127. #else
  128. static inline int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
  129. struct netlbl_audit *audit_info)
  130. {
  131. return -ENOSYS;
  132. }
  133. static inline void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
  134. {
  135. return;
  136. }
  137. static inline int cipso_v4_doi_remove(u32 doi,
  138. struct netlbl_audit *audit_info)
  139. {
  140. return 0;
  141. }
  142. static inline struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
  143. {
  144. return NULL;
  145. }
  146. static inline int cipso_v4_doi_walk(u32 *skip_cnt,
  147. int (*callback) (struct cipso_v4_doi *doi_def, void *arg),
  148. void *cb_arg)
  149. {
  150. return 0;
  151. }
  152. static inline int cipso_v4_doi_domhsh_add(struct cipso_v4_doi *doi_def,
  153. const char *domain)
  154. {
  155. return -ENOSYS;
  156. }
  157. static inline int cipso_v4_doi_domhsh_remove(struct cipso_v4_doi *doi_def,
  158. const char *domain)
  159. {
  160. return 0;
  161. }
  162. #endif /* CONFIG_NETLABEL */
  163. /*
  164. * Label Mapping Cache Functions
  165. */
  166. #ifdef CONFIG_NETLABEL
  167. void cipso_v4_cache_invalidate(void);
  168. int cipso_v4_cache_add(const struct sk_buff *skb,
  169. const struct netlbl_lsm_secattr *secattr);
  170. #else
  171. static inline void cipso_v4_cache_invalidate(void)
  172. {
  173. return;
  174. }
  175. static inline int cipso_v4_cache_add(const struct sk_buff *skb,
  176. const struct netlbl_lsm_secattr *secattr)
  177. {
  178. return 0;
  179. }
  180. #endif /* CONFIG_NETLABEL */
  181. /*
  182. * Protocol Handling Functions
  183. */
  184. #ifdef CONFIG_NETLABEL
  185. void cipso_v4_error(struct sk_buff *skb, int error, u32 gateway);
  186. int cipso_v4_sock_setattr(struct sock *sk,
  187. const struct cipso_v4_doi *doi_def,
  188. const struct netlbl_lsm_secattr *secattr);
  189. void cipso_v4_sock_delattr(struct sock *sk);
  190. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
  191. int cipso_v4_req_setattr(struct request_sock *req,
  192. const struct cipso_v4_doi *doi_def,
  193. const struct netlbl_lsm_secattr *secattr);
  194. void cipso_v4_req_delattr(struct request_sock *req);
  195. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  196. const struct cipso_v4_doi *doi_def,
  197. const struct netlbl_lsm_secattr *secattr);
  198. int cipso_v4_skbuff_delattr(struct sk_buff *skb);
  199. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  200. struct netlbl_lsm_secattr *secattr);
  201. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option);
  202. #else
  203. static inline void cipso_v4_error(struct sk_buff *skb,
  204. int error,
  205. u32 gateway)
  206. {
  207. return;
  208. }
  209. static inline int cipso_v4_sock_setattr(struct sock *sk,
  210. const struct cipso_v4_doi *doi_def,
  211. const struct netlbl_lsm_secattr *secattr)
  212. {
  213. return -ENOSYS;
  214. }
  215. static inline void cipso_v4_sock_delattr(struct sock *sk)
  216. {
  217. }
  218. static inline int cipso_v4_sock_getattr(struct sock *sk,
  219. struct netlbl_lsm_secattr *secattr)
  220. {
  221. return -ENOSYS;
  222. }
  223. static inline int cipso_v4_req_setattr(struct request_sock *req,
  224. const struct cipso_v4_doi *doi_def,
  225. const struct netlbl_lsm_secattr *secattr)
  226. {
  227. return -ENOSYS;
  228. }
  229. static inline void cipso_v4_req_delattr(struct request_sock *req)
  230. {
  231. return;
  232. }
  233. static inline int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  234. const struct cipso_v4_doi *doi_def,
  235. const struct netlbl_lsm_secattr *secattr)
  236. {
  237. return -ENOSYS;
  238. }
  239. static inline int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  240. {
  241. return -ENOSYS;
  242. }
  243. static inline int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  244. struct netlbl_lsm_secattr *secattr)
  245. {
  246. return -ENOSYS;
  247. }
  248. static inline int cipso_v4_validate(const struct sk_buff *skb,
  249. unsigned char **option)
  250. {
  251. unsigned char *opt = *option;
  252. unsigned char err_offset = 0;
  253. u8 opt_len = opt[1];
  254. u8 opt_iter;
  255. u8 tag_len;
  256. if (opt_len < 8) {
  257. err_offset = 1;
  258. goto out;
  259. }
  260. if (get_unaligned_be32(&opt[2]) == 0) {
  261. err_offset = 2;
  262. goto out;
  263. }
  264. for (opt_iter = 6; opt_iter < opt_len;) {
  265. tag_len = opt[opt_iter + 1];
  266. if ((tag_len == 0) || (opt[opt_iter + 1] > (opt_len - opt_iter))) {
  267. err_offset = opt_iter + 1;
  268. goto out;
  269. }
  270. opt_iter += tag_len;
  271. }
  272. out:
  273. *option = opt + err_offset;
  274. return err_offset;
  275. }
  276. #endif /* CONFIG_NETLABEL */
  277. #endif /* _CIPSO_IPV4_H */