tcp_ao.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. struct rcu_head rcu;
  120. };
  121. #ifdef CONFIG_TCP_MD5SIG
  122. #include <linux/jump_label.h>
  123. extern struct static_key_false_deferred tcp_md5_needed;
  124. #define static_branch_tcp_md5() static_branch_unlikely(&tcp_md5_needed.key)
  125. #else
  126. #define static_branch_tcp_md5() false
  127. #endif
  128. #ifdef CONFIG_TCP_AO
  129. /* TCP-AO structures and functions */
  130. #include <linux/jump_label.h>
  131. extern struct static_key_false_deferred tcp_ao_needed;
  132. #define static_branch_tcp_ao() static_branch_unlikely(&tcp_ao_needed.key)
  133. #else
  134. #define static_branch_tcp_ao() false
  135. #endif
  136. #ifdef CONFIG_TCP_AO
  137. /* TCP-AO structures and functions */
  138. struct tcp4_ao_context {
  139. __be32 saddr;
  140. __be32 daddr;
  141. __be16 sport;
  142. __be16 dport;
  143. __be32 sisn;
  144. __be32 disn;
  145. };
  146. struct tcp6_ao_context {
  147. struct in6_addr saddr;
  148. struct in6_addr daddr;
  149. __be16 sport;
  150. __be16 dport;
  151. __be32 sisn;
  152. __be32 disn;
  153. };
  154. struct tcp_sigpool;
  155. /* Established states are fast-path and there always is current_key/rnext_key */
  156. #define TCP_AO_ESTABLISHED (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | \
  157. TCPF_CLOSE_WAIT | TCPF_LAST_ACK | TCPF_CLOSING)
  158. int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
  159. struct tcp_ao_key *key, struct tcphdr *th,
  160. __u8 *hash_location);
  161. int tcp_ao_hash_skb(unsigned short int family,
  162. char *ao_hash, struct tcp_ao_key *key,
  163. const struct sock *sk, const struct sk_buff *skb,
  164. const u8 *tkey, int hash_offset, u32 sne);
  165. int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
  166. sockptr_t optval, int optlen);
  167. struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
  168. struct tcp_ao_info *ao,
  169. int sndid, int rcvid);
  170. int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
  171. struct request_sock *req, struct sk_buff *skb,
  172. int family);
  173. int tcp_ao_calc_traffic_key(struct tcp_ao_key *mkt, u8 *key, void *ctx,
  174. unsigned int len, struct tcp_sigpool *hp);
  175. void tcp_ao_destroy_sock(struct sock *sk, bool twsk);
  176. void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp);
  177. bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code);
  178. int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  179. int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  180. int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen);
  181. int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen);
  182. enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
  183. const struct sk_buff *skb, unsigned short int family,
  184. const struct request_sock *req, int l3index,
  185. const struct tcp_ao_hdr *aoh);
  186. u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq);
  187. struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
  188. const union tcp_ao_addr *addr,
  189. int family, int sndid, int rcvid);
  190. int tcp_ao_hash_hdr(unsigned short family, char *ao_hash,
  191. struct tcp_ao_key *key, const u8 *tkey,
  192. const union tcp_ao_addr *daddr,
  193. const union tcp_ao_addr *saddr,
  194. const struct tcphdr *th, u32 sne);
  195. int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
  196. const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
  197. struct tcp_ao_key **key, char **traffic_key,
  198. bool *allocated_traffic_key, u8 *keyid, u32 *sne);
  199. /* ipv4 specific functions */
  200. int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
  201. struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
  202. int sndid, int rcvid);
  203. int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *mkt,
  204. struct request_sock *req, const struct sk_buff *skb,
  205. int hash_offset, u32 sne);
  206. int tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  207. const struct sock *sk,
  208. __be32 sisn, __be32 disn, bool send);
  209. int tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
  210. struct request_sock *req);
  211. struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
  212. struct request_sock *req,
  213. int sndid, int rcvid);
  214. int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
  215. const struct sock *sk, const struct sk_buff *skb,
  216. const u8 *tkey, int hash_offset, u32 sne);
  217. /* ipv6 specific functions */
  218. int tcp_v6_ao_hash_pseudoheader(struct tcp_sigpool *hp,
  219. const struct in6_addr *daddr,
  220. const struct in6_addr *saddr, int nbytes);
  221. int tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
  222. const struct sk_buff *skb, __be32 sisn, __be32 disn);
  223. int tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
  224. const struct sock *sk, __be32 sisn,
  225. __be32 disn, bool send);
  226. int tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
  227. struct request_sock *req);
  228. struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk,
  229. struct sock *addr_sk, int sndid, int rcvid);
  230. struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk,
  231. struct request_sock *req,
  232. int sndid, int rcvid);
  233. int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
  234. const struct sock *sk, const struct sk_buff *skb,
  235. const u8 *tkey, int hash_offset, u32 sne);
  236. int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
  237. int tcp_v6_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
  238. struct request_sock *req, const struct sk_buff *skb,
  239. int hash_offset, u32 sne);
  240. void tcp_ao_established(struct sock *sk);
  241. void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb);
  242. void tcp_ao_connect_init(struct sock *sk);
  243. void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
  244. struct request_sock *req, unsigned short int family);
  245. #else /* CONFIG_TCP_AO */
  246. static inline int tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
  247. struct tcp_ao_key *key, struct tcphdr *th,
  248. __u8 *hash_location)
  249. {
  250. return 0;
  251. }
  252. static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
  253. struct request_sock *req, unsigned short int family)
  254. {
  255. }
  256. static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int family,
  257. int type, int code)
  258. {
  259. return false;
  260. }
  261. static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
  262. const struct sk_buff *skb, unsigned short int family,
  263. const struct request_sock *req, int l3index,
  264. const struct tcp_ao_hdr *aoh)
  265. {
  266. return SKB_NOT_DROPPED_YET;
  267. }
  268. static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk,
  269. int l3index, const union tcp_ao_addr *addr,
  270. int family, int sndid, int rcvid)
  271. {
  272. return NULL;
  273. }
  274. static inline void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
  275. {
  276. }
  277. static inline void tcp_ao_established(struct sock *sk)
  278. {
  279. }
  280. static inline void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
  281. {
  282. }
  283. static inline void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw,
  284. struct tcp_sock *tp)
  285. {
  286. }
  287. static inline void tcp_ao_connect_init(struct sock *sk)
  288. {
  289. }
  290. static inline int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  291. {
  292. return -ENOPROTOOPT;
  293. }
  294. static inline int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
  295. {
  296. return -ENOPROTOOPT;
  297. }
  298. static inline int tcp_ao_get_repair(struct sock *sk,
  299. sockptr_t optval, sockptr_t optlen)
  300. {
  301. return -ENOPROTOOPT;
  302. }
  303. static inline int tcp_ao_set_repair(struct sock *sk,
  304. sockptr_t optval, unsigned int optlen)
  305. {
  306. return -ENOPROTOOPT;
  307. }
  308. #endif
  309. #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
  310. int tcp_do_parse_auth_options(const struct tcphdr *th,
  311. const u8 **md5_hash, const u8 **ao_hash);
  312. #else
  313. static inline int tcp_do_parse_auth_options(const struct tcphdr *th,
  314. const u8 **md5_hash, const u8 **ao_hash)
  315. {
  316. *md5_hash = NULL;
  317. *ao_hash = NULL;
  318. return 0;
  319. }
  320. #endif
  321. #endif /* _TCP_AO_H */