cipso_ipv4.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <paul@paul-moore.com>
  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 <linux/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. bool sk_locked);
  163. void cipso_v4_sock_delattr(struct sock *sk);
  164. int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
  165. int cipso_v4_req_setattr(struct request_sock *req,
  166. const struct cipso_v4_doi *doi_def,
  167. const struct netlbl_lsm_secattr *secattr);
  168. void cipso_v4_req_delattr(struct request_sock *req);
  169. int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  170. const struct cipso_v4_doi *doi_def,
  171. const struct netlbl_lsm_secattr *secattr);
  172. int cipso_v4_skbuff_delattr(struct sk_buff *skb);
  173. int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  174. struct netlbl_lsm_secattr *secattr);
  175. unsigned char *cipso_v4_optptr(const struct sk_buff *skb);
  176. int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option);
  177. #else
  178. static inline void cipso_v4_error(struct sk_buff *skb,
  179. int error,
  180. u32 gateway)
  181. {
  182. return;
  183. }
  184. static inline int cipso_v4_getattr(const unsigned char *cipso,
  185. struct netlbl_lsm_secattr *secattr)
  186. {
  187. return -ENOSYS;
  188. }
  189. static inline int cipso_v4_sock_setattr(struct sock *sk,
  190. const struct cipso_v4_doi *doi_def,
  191. const struct netlbl_lsm_secattr *secattr,
  192. bool sk_locked)
  193. {
  194. return -ENOSYS;
  195. }
  196. static inline void cipso_v4_sock_delattr(struct sock *sk)
  197. {
  198. }
  199. static inline int cipso_v4_sock_getattr(struct sock *sk,
  200. struct netlbl_lsm_secattr *secattr)
  201. {
  202. return -ENOSYS;
  203. }
  204. static inline int cipso_v4_req_setattr(struct request_sock *req,
  205. const struct cipso_v4_doi *doi_def,
  206. const struct netlbl_lsm_secattr *secattr)
  207. {
  208. return -ENOSYS;
  209. }
  210. static inline void cipso_v4_req_delattr(struct request_sock *req)
  211. {
  212. return;
  213. }
  214. static inline int cipso_v4_skbuff_setattr(struct sk_buff *skb,
  215. const struct cipso_v4_doi *doi_def,
  216. const struct netlbl_lsm_secattr *secattr)
  217. {
  218. return -ENOSYS;
  219. }
  220. static inline int cipso_v4_skbuff_delattr(struct sk_buff *skb)
  221. {
  222. return -ENOSYS;
  223. }
  224. static inline int cipso_v4_skbuff_getattr(const struct sk_buff *skb,
  225. struct netlbl_lsm_secattr *secattr)
  226. {
  227. return -ENOSYS;
  228. }
  229. static inline unsigned char *cipso_v4_optptr(const struct sk_buff *skb)
  230. {
  231. return NULL;
  232. }
  233. static inline int cipso_v4_validate(const struct sk_buff *skb,
  234. unsigned char **option)
  235. {
  236. unsigned char *opt = *option;
  237. unsigned char err_offset = 0;
  238. u8 opt_len = opt[1];
  239. u8 opt_iter;
  240. u8 tag_len;
  241. if (opt_len < 8) {
  242. err_offset = 1;
  243. goto out;
  244. }
  245. if (get_unaligned_be32(&opt[2]) == 0) {
  246. err_offset = 2;
  247. goto out;
  248. }
  249. for (opt_iter = 6; opt_iter < opt_len;) {
  250. if (opt_iter + 1 == opt_len) {
  251. err_offset = opt_iter;
  252. goto out;
  253. }
  254. tag_len = opt[opt_iter + 1];
  255. if ((tag_len == 0) || (tag_len > (opt_len - opt_iter))) {
  256. err_offset = opt_iter + 1;
  257. goto out;
  258. }
  259. opt_iter += tag_len;
  260. }
  261. out:
  262. *option = opt + err_offset;
  263. return err_offset;
  264. }
  265. #endif /* CONFIG_NETLABEL */
  266. #endif /* _CIPSO_IPV4_H */