cipso_ipv4.h 7.4 KB

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