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