tcp_ao.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _TCP_AO_H
  3. #define _TCP_AO_H
  4. #define TCP_AO_KEY_ALIGN 1
  5. #define __tcp_ao_key_align __aligned(TCP_AO_KEY_ALIGN)
  6. union tcp_ao_addr {
  7. struct in_addr a4;
  8. #if IS_ENABLED(CONFIG_IPV6)
  9. struct in6_addr a6;
  10. #endif
  11. };
  12. struct tcp_ao_hdr {
  13. u8 kind;
  14. u8 length;
  15. u8 keyid;
  16. u8 rnext_keyid;
  17. };
  18. static inline u8 tcp_ao_hdr_maclen(const struct tcp_ao_hdr *aoh)
  19. {
  20. return aoh->length - sizeof(struct tcp_ao_hdr);
  21. }
  22. struct tcp_ao_counters {
  23. atomic64_t pkt_good;
  24. atomic64_t pkt_bad;
  25. atomic64_t key_not_found;
  26. atomic64_t ao_required;
  27. atomic64_t dropped_icmp;
  28. };
  29. struct tcp_ao_key {
  30. struct hlist_node node;
  31. union tcp_ao_addr addr;
  32. u8 key[TCP_AO_MAXKEYLEN] __tcp_ao_key_align;
  33. unsigned int tcp_sigpool_id;
  34. unsigned int digest_size;
  35. int l3index;
  36. u8 prefixlen;
  37. u8 family;
  38. u8 keylen;
  39. u8 keyflags;
  40. u8 sndid;
  41. u8 rcvid;
  42. u8 maclen;
  43. struct rcu_head rcu;
  44. atomic64_t pkt_good;
  45. atomic64_t pkt_bad;
  46. u8 traffic_keys[];
  47. };
  48. static inline u8 *rcv_other_key(struct tcp_ao_key *key)
  49. {
  50. return key->traffic_keys;
  51. }
  52. static inline u8 *snd_other_key(struct tcp_ao_key *key)
  53. {
  54. return key->traffic_keys + key->digest_size;
  55. }
  56. static inline int tcp_ao_maclen(const struct tcp_ao_key *key)
  57. {
  58. return key->maclen;
  59. }
  60. /* Use tcp_ao_len_aligned() for TCP header calculations */
  61. static inline int tcp_ao_len(const struct tcp_ao_key *key)
  62. {
  63. return tcp_ao_maclen(key) + sizeof(struct tcp_ao_hdr);
  64. }
  65. static inline int tcp_ao_len_aligned(const struct tcp_ao_key *key)
  66. {
  67. return round_up(tcp_ao_len(key), 4);
  68. }
  69. static inline unsigned int tcp_ao_digest_size(struct tcp_ao_key *key)
  70. {
  71. return key->digest_size;
  72. }
  73. static inline int tcp_ao_sizeof_key(const struct tcp_ao_key *key)
  74. {
  75. return sizeof(struct tcp_ao_key) + (key->digest_size << 1);
  76. }
  77. struct tcp_ao_info {
  78. /* List of tcp_ao_key's */
  79. struct hlist_head head;
  80. /* current_key and rnext_key are maintained on sockets
  81. * in TCP_AO_ESTABLISHED states.
  82. * Their purpose is to cache keys on established connections,
  83. * saving needless lookups. Never dereference any of them from
  84. * listen sockets.
  85. * ::current_key may change in RX to the key that was requested by
  86. * the peer, please use READ_ONCE()/WRITE_ONCE() in order to avoid
  87. * load/store tearing.
  88. * Do the same for ::rnext_key, if you don't hold socket lock
  89. * (it's changed only by userspace request in setsockopt()).
  90. */
  91. struct tcp_ao_key *current_key;
  92. struct tcp_ao_key *rnext_key;
  93. struct tcp_ao_counters counters;
  94. u32 ao_required :1,
  95. accept_icmps :1,
  96. __unused :30;
  97. __be32 lisn;
  98. __be32 risn;
  99. /* Sequence Number Extension (SNE) are upper 4 bytes for SEQ,
  100. * that protect TCP-AO connection from replayed old TCP segments.
  101. * See RFC5925 (6.2).
  102. * In order to get correct SNE, there's a helper tcp_ao_compute_sne().
  103. * It needs SEQ basis to understand whereabouts are lower SEQ numbers.
  104. * According to that basis vector, it can provide incremented SNE
  105. * when SEQ rolls over or provide decremented SNE when there's
  106. * a retransmitted segment from before-rolling over.
  107. * - for request sockets such basis is rcv_isn/snt_isn, which seems
  108. * good enough as it's unexpected to receive 4 Gbytes on reqsk.
  109. * - for full sockets the basis is rcv_nxt/snd_una. snd_una is
  110. * taken instead of snd_nxt as currently it's easier to track
  111. * in tcp_snd_una_update(), rather than updating SNE in all
  112. * WRITE_ONCE(tp->snd_nxt, ...)
  113. * - for time-wait sockets the basis is tw_rcv_nxt/tw_snd_nxt.
  114. * tw_snd_nxt is not expected to change, while tw_rcv_nxt may.
  115. */
  116. u32 snd_sne;
  117. u32 rcv_sne;
  118. refcount_t refcnt; /* Protects twsk destruction */
  119. };
  120. #ifdef CONFIG_TCP_MD5SIG
  121. #include <linux/jump_label.h>
  122. extern struct static_key_false_deferred tcp_md5_needed;
  123. #define static_branch_tcp_md5() static_branch_unlikely(&tcp_md5_needed.key)
  124. #else
  125. #define static_branch_tcp_md5() false
  126. #endif
  127. #ifdef CONFIG_TCP_AO
  128. /* TCP-AO structures and functions */
  129. #include <linux/jump_label.h>
  130. extern struct static_key_false_deferred tcp_ao_needed;
  131. #define static_branch_tcp_ao() static_branch_unlikely(&tcp_ao_needed.key)
  132. #else
  133. #define static_branch_tcp_ao() false
  134. #endif
  135. #ifdef CONFIG_TCP_AO
  136. /* TCP-AO structures and functions */
  137. struct tcp4_ao_context {
  138. __be32 saddr;
  139. __be32 daddr;
  140. __be16 sport;
  141. __be16 dport;
  142. __be32 sisn;
  143. __be32 disn;
  144. };
  145. struct tcp6_ao_context {
  146. struct in6_addr saddr;
  147. struct in6_addr daddr;
  148. __be16 sport;
  149. __be16 dport;
  150. __be32 sisn;
  151. __be32 disn;
  152. };
  153. struct tcp_sigpool;
  154. /* Established states are fast-path and there always is current_key/rnext_key */
  155. #define TCP_AO_ESTABLISHED (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | \
  156. TCPF_CLOSE_WAIT | TCPF_LAST_ACK | TCPF_CLOSING)
  157. int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
  158. struct tcp_ao_key *key, struct tcphdr *th,
  159. __u8 *hash_location);
  160. int tcp_ao_hash_skb(unsigned short int family,
  161. char *ao_hash, struct tcp_ao_key *key,
  162. const struct sock *sk, const struct sk_buff *skb,
  163. const u8 *tkey, int hash_offset, u32 sne);
  164. int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
  165. sockptr_t optval, int optlen);
  166. struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
  167. struct tcp_ao_info *ao,
  168. int sndid, int rcvid);
  169. int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
  170. struct request_sock *req, struct sk_buff *skb,
  171. int family);
  172. int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
  173. unsigned int len, struct tcp_sigpool *hp);
  174. void tcp_ao_destroy_sock(struct sock *sk, bool twsk);
  175. void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp);
  176. bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code);
  177. int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  178. int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  179. int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  180. int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen);
  181. enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
  182. const struct sk_buff *skb, unsigned short int family,
  183. const struct request_sock *req, int l3index,
  184. const struct tcp_ao_hdr *aoh);
  185. u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq);
  186. struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
  187. const union tcp_ao_addr *addr,
  188. int family, int sndid, int rcvid);
  189. int tcp_ao_hash_hdr(unsigned short family, char *ao_hash,
  190. struct tcp_ao_key *key, const u8 *tkey,
  191. const union tcp_ao_addr *daddr,
  192. const union tcp_ao_addr *saddr,
  193. const struct tcphdr *th, u32 sne);
  194. int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
  195. const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
  196. struct tcp_ao_key **key, char **traffic_key,
  197. bool *allocated_traffic_key, u8 *keyid, u32 *sne);
  198. /* ipv4 specific functions */
  199. int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
  200. struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
  201. int sndid, int rcvid);
  202. int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *mkt,
  203. struct request_sock *req, const struct sk_buff *skb,
  204. int hash_offset, u32 sne);
  205. int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  206. const struct sock *sk,
  207. __be32 sisn, __be32 disn, bool send);
  208. int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
  209. struct request_sock *req);
  210. struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
  211. struct request_sock *req,
  212. int sndid, int rcvid);
  213. int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
  214. const struct sock *sk, const struct sk_buff *skb,
  215. const u8 *tkey, int hash_offset, u32 sne);
  216. /* ipv6 specific functions */
  217. int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp,
  218. const struct in6_addr *daddr,
  219. const struct in6_addr *saddr, int nbytes);
  220. int tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
  221. const struct sk_buff *skb, __be32 sisn, __be32 disn);
  222. int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  223. const struct sock *sk, __be32 sisn,
  224. __be32 disn, bool send);
  225. int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
  226. struct request_sock *req);
  227. struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk,
  228. struct sock *addr_sk, int sndid, int rcvid);
  229. struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk,
  230. struct request_sock *req,
  231. int sndid, int rcvid);
  232. int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
  233. const struct sock *sk, const struct sk_buff *skb,
  234. const u8 *tkey, int hash_offset, u32 sne);
  235. int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
  236. int tcp_v6_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
  237. struct request_sock *req, const struct sk_buff *skb,
  238. int hash_offset, u32 sne);
  239. void tcp_ao_established(struct sock *sk);
  240. void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb);
  241. void tcp_ao_connect_init(struct sock *sk);
  242. void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
  243. struct request_sock *req, unsigned short int family);
  244. #else /* CONFIG_TCP_AO */
  245. static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
  246. struct tcp_ao_key *key, struct tcphdr *th,
  247. __u8 *hash_location)
  248. {
  249. return 0;
  250. }
  251. static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
  252. struct request_sock *req, unsigned short int family)
  253. {
  254. }
  255. static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int family,
  256. int type, int code)
  257. {
  258. return false;
  259. }
  260. static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
  261. const struct sk_buff *skb, unsigned short int family,
  262. const struct request_sock *req, int l3index,
  263. const struct tcp_ao_hdr *aoh)
  264. {
  265. return SKB_NOT_DROPPED_YET;
  266. }
  267. static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk,
  268. int l3index, const union tcp_ao_addr *addr,
  269. int family, int sndid, int rcvid)
  270. {
  271. return NULL;
  272. }
  273. static inline void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
  274. {
  275. }
  276. static inline void tcp_ao_established(struct sock *sk)
  277. {
  278. }
  279. static inline void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
  280. {
  281. }
  282. static inline void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw,
  283. struct tcp_sock *tp)
  284. {
  285. }
  286. static inline void tcp_ao_connect_init(struct sock *sk)
  287. {
  288. }
  289. static inline int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  290. {
  291. return -ENOPROTOOPT;
  292. }
  293. static inline int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  294. {
  295. return -ENOPROTOOPT;
  296. }
  297. static inline int tcp_ao_get_repair(struct sock *sk,
  298. sockptr_t optval, sockptr_t optlen)
  299. {
  300. return -ENOPROTOOPT;
  301. }
  302. static inline int tcp_ao_set_repair(struct sock *sk,
  303. sockptr_t optval, unsigned int optlen)
  304. {
  305. return -ENOPROTOOPT;
  306. }
  307. #endif
  308. #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
  309. int tcp_do_parse_auth_options(const struct tcphdr *th,
  310. const u8 **md5_hash, const u8 **ao_hash);
  311. #else
  312. static inline int tcp_do_parse_auth_options(const struct tcphdr *th,
  313. const u8 **md5_hash, const u8 **ao_hash)
  314. {
  315. *md5_hash = NULL;
  316. *ao_hash = NULL;
  317. return 0;
  318. }
  319. #endif
  320. #endif /* _TCP_AO_H */