xref: /linux/include/net/tcp_ao.h (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
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 
tcp_ao_hdr_maclen(const struct tcp_ao_hdr * aoh)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 
rcv_other_key(struct tcp_ao_key * key)70 static inline u8 *rcv_other_key(struct tcp_ao_key *key)
71 {
72 	return key->traffic_keys;
73 }
74 
snd_other_key(struct tcp_ao_key * key)75 static inline u8 *snd_other_key(struct tcp_ao_key *key)
76 {
77 	return key->traffic_keys + key->digest_size;
78 }
79 
tcp_ao_maclen(const struct tcp_ao_key * key)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 */
tcp_ao_len(const struct tcp_ao_key * key)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 
tcp_ao_len_aligned(const struct tcp_ao_key * key)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 
tcp_ao_digest_size(struct tcp_ao_key * key)96 static inline unsigned int tcp_ao_digest_size(struct tcp_ao_key *key)
97 {
98 	return key->digest_size;
99 }
100 
tcp_ao_sizeof_key(const struct tcp_ao_key * key)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 	struct rcu_head		rcu;
149 };
150 
151 #ifdef CONFIG_TCP_MD5SIG
152 #include <linux/jump_label.h>
153 extern struct static_key_false_deferred tcp_md5_needed;
154 #define static_branch_tcp_md5()	static_branch_unlikely(&tcp_md5_needed.key)
155 #else
156 #define static_branch_tcp_md5()	false
157 #endif
158 #ifdef CONFIG_TCP_AO
159 /* TCP-AO structures and functions */
160 #include <linux/jump_label.h>
161 extern struct static_key_false_deferred tcp_ao_needed;
162 #define static_branch_tcp_ao()	static_branch_unlikely(&tcp_ao_needed.key)
163 #else
164 #define static_branch_tcp_ao()	false
165 #endif
166 
167 #ifdef CONFIG_TCP_AO
168 /* TCP-AO structures and functions */
169 struct tcp4_ao_context {
170 	__be32		saddr;
171 	__be32		daddr;
172 	__be16		sport;
173 	__be16		dport;
174 	__be32		sisn;
175 	__be32		disn;
176 };
177 
178 struct tcp6_ao_context {
179 	struct in6_addr	saddr;
180 	struct in6_addr	daddr;
181 	__be16		sport;
182 	__be16		dport;
183 	__be32		sisn;
184 	__be32		disn;
185 };
186 
187 /* Established states are fast-path and there always is current_key/rnext_key */
188 #define TCP_AO_ESTABLISHED (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_FIN_WAIT2 | \
189 			    TCPF_CLOSE_WAIT | TCPF_LAST_ACK | TCPF_CLOSING)
190 
191 void tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
192 			 struct tcp_ao_key *key, struct tcphdr *th,
193 			 __u8 *hash_location);
194 void tcp_ao_mac_update(struct tcp_ao_mac_ctx *mac_ctx, const void *data,
195 		       size_t data_len);
196 int tcp_ao_hash_skb(unsigned short int family,
197 		    char *ao_hash, struct tcp_ao_key *key,
198 		    const struct sock *sk, const struct sk_buff *skb,
199 		    const u8 *tkey, int hash_offset, u32 sne);
200 int tcp_parse_ao(struct sock *sk, int cmd, unsigned short int family,
201 		 sockptr_t optval, int optlen);
202 struct tcp_ao_key *tcp_ao_established_key(const struct sock *sk,
203 					  struct tcp_ao_info *ao,
204 					  int sndid, int rcvid);
205 int tcp_ao_copy_all_matching(const struct sock *sk, struct sock *newsk,
206 			     struct request_sock *req, struct sk_buff *skb,
207 			     int family);
208 void tcp_ao_calc_traffic_key(const struct tcp_ao_key *mkt, u8 *traffic_key,
209 			     const void *input, unsigned int input_len);
210 void tcp_ao_destroy_sock(struct sock *sk, bool twsk);
211 void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw, struct tcp_sock *tp);
212 bool tcp_ao_ignore_icmp(const struct sock *sk, int family, int type, int code);
213 int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen);
214 int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen);
215 int tcp_ao_get_repair(struct sock *sk, sockptr_t optval, sockptr_t optlen);
216 int tcp_ao_set_repair(struct sock *sk, sockptr_t optval, unsigned int optlen);
217 enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
218 			const struct sk_buff *skb, unsigned short int family,
219 			const struct request_sock *req, int l3index,
220 			const struct tcp_ao_hdr *aoh);
221 u32 tcp_ao_compute_sne(u32 next_sne, u32 next_seq, u32 seq);
222 struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk, int l3index,
223 				    const union tcp_ao_addr *addr,
224 				    int family, int sndid, int rcvid);
225 int tcp_ao_hash_hdr(unsigned short family, char *ao_hash,
226 		    struct tcp_ao_key *key, const u8 *tkey,
227 		    const union tcp_ao_addr *daddr,
228 		    const union tcp_ao_addr *saddr,
229 		    const struct tcphdr *th, u32 sne);
230 int tcp_ao_prepare_reset(const struct sock *sk, struct sk_buff *skb,
231 			 const struct tcp_ao_hdr *aoh, int l3index, u32 seq,
232 			 struct tcp_ao_key **key, char **traffic_key,
233 			 bool *allocated_traffic_key, u8 *keyid, u32 *sne);
234 
235 /* ipv4 specific functions */
236 int tcp_v4_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
237 struct tcp_ao_key *tcp_v4_ao_lookup(const struct sock *sk, struct sock *addr_sk,
238 				    int sndid, int rcvid);
239 int tcp_v4_ao_synack_hash(char *ao_hash, struct tcp_ao_key *mkt,
240 			  struct request_sock *req, const struct sk_buff *skb,
241 			  int hash_offset, u32 sne);
242 void tcp_v4_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
243 			   const struct sock *sk,
244 			   __be32 sisn, __be32 disn, bool send);
245 void tcp_v4_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
246 			    struct request_sock *req);
247 struct tcp_ao_key *tcp_v4_ao_lookup_rsk(const struct sock *sk,
248 					struct request_sock *req,
249 					int sndid, int rcvid);
250 int tcp_v4_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
251 		       const struct sock *sk, const struct sk_buff *skb,
252 		       const u8 *tkey, int hash_offset, u32 sne);
253 /* ipv6 specific functions */
254 void tcp_v6_ao_hash_pseudoheader(struct tcp_ao_mac_ctx *mac_ctx,
255 				 const struct in6_addr *daddr,
256 				 const struct in6_addr *saddr, int nbytes);
257 void tcp_v6_ao_calc_key_skb(struct tcp_ao_key *mkt, u8 *key,
258 			    const struct sk_buff *skb, __be32 sisn,
259 			    __be32 disn);
260 void tcp_v6_ao_calc_key_sk(struct tcp_ao_key *mkt, u8 *key,
261 			   const struct sock *sk, __be32 sisn,
262 			   __be32 disn, bool send);
263 void tcp_v6_ao_calc_key_rsk(struct tcp_ao_key *mkt, u8 *key,
264 			    struct request_sock *req);
265 struct tcp_ao_key *tcp_v6_ao_lookup(const struct sock *sk,
266 				    struct sock *addr_sk, int sndid, int rcvid);
267 struct tcp_ao_key *tcp_v6_ao_lookup_rsk(const struct sock *sk,
268 					struct request_sock *req,
269 					int sndid, int rcvid);
270 int tcp_v6_ao_hash_skb(char *ao_hash, struct tcp_ao_key *key,
271 		       const struct sock *sk, const struct sk_buff *skb,
272 		       const u8 *tkey, int hash_offset, u32 sne);
273 int tcp_v6_parse_ao(struct sock *sk, int cmd, sockptr_t optval, int optlen);
274 int tcp_v6_ao_synack_hash(char *ao_hash, struct tcp_ao_key *ao_key,
275 			  struct request_sock *req, const struct sk_buff *skb,
276 			  int hash_offset, u32 sne);
277 void tcp_ao_established(struct sock *sk);
278 void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb);
279 void tcp_ao_connect_init(struct sock *sk);
280 void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
281 		      struct request_sock *req, unsigned short int family);
282 #else /* CONFIG_TCP_AO */
283 
tcp_ao_transmit_skb(struct sock * sk,struct sk_buff * skb,struct tcp_ao_key * key,struct tcphdr * th,__u8 * hash_location)284 static inline void tcp_ao_transmit_skb(struct sock *sk, struct sk_buff *skb,
285 				       struct tcp_ao_key *key,
286 				       struct tcphdr *th, __u8 *hash_location)
287 {
288 }
289 
tcp_ao_syncookie(struct sock * sk,const struct sk_buff * skb,struct request_sock * req,unsigned short int family)290 static inline void tcp_ao_syncookie(struct sock *sk, const struct sk_buff *skb,
291 				    struct request_sock *req, unsigned short int family)
292 {
293 }
294 
tcp_ao_ignore_icmp(const struct sock * sk,int family,int type,int code)295 static inline bool tcp_ao_ignore_icmp(const struct sock *sk, int family,
296 				      int type, int code)
297 {
298 	return false;
299 }
300 
tcp_inbound_ao_hash(struct sock * sk,const struct sk_buff * skb,unsigned short int family,const struct request_sock * req,int l3index,const struct tcp_ao_hdr * aoh)301 static inline enum skb_drop_reason tcp_inbound_ao_hash(struct sock *sk,
302 		const struct sk_buff *skb, unsigned short int family,
303 		const struct request_sock *req, int l3index,
304 		const struct tcp_ao_hdr *aoh)
305 {
306 	return SKB_NOT_DROPPED_YET;
307 }
308 
tcp_ao_do_lookup(const struct sock * sk,int l3index,const union tcp_ao_addr * addr,int family,int sndid,int rcvid)309 static inline struct tcp_ao_key *tcp_ao_do_lookup(const struct sock *sk,
310 		int l3index, const union tcp_ao_addr *addr,
311 		int family, int sndid, int rcvid)
312 {
313 	return NULL;
314 }
315 
tcp_ao_destroy_sock(struct sock * sk,bool twsk)316 static inline void tcp_ao_destroy_sock(struct sock *sk, bool twsk)
317 {
318 }
319 
tcp_ao_established(struct sock * sk)320 static inline void tcp_ao_established(struct sock *sk)
321 {
322 }
323 
tcp_ao_finish_connect(struct sock * sk,struct sk_buff * skb)324 static inline void tcp_ao_finish_connect(struct sock *sk, struct sk_buff *skb)
325 {
326 }
327 
tcp_ao_time_wait(struct tcp_timewait_sock * tcptw,struct tcp_sock * tp)328 static inline void tcp_ao_time_wait(struct tcp_timewait_sock *tcptw,
329 				    struct tcp_sock *tp)
330 {
331 }
332 
tcp_ao_connect_init(struct sock * sk)333 static inline void tcp_ao_connect_init(struct sock *sk)
334 {
335 }
336 
tcp_ao_get_mkts(struct sock * sk,sockptr_t optval,sockptr_t optlen)337 static inline int tcp_ao_get_mkts(struct sock *sk, sockptr_t optval, sockptr_t optlen)
338 {
339 	return -ENOPROTOOPT;
340 }
341 
tcp_ao_get_sock_info(struct sock * sk,sockptr_t optval,sockptr_t optlen)342 static inline int tcp_ao_get_sock_info(struct sock *sk, sockptr_t optval, sockptr_t optlen)
343 {
344 	return -ENOPROTOOPT;
345 }
346 
tcp_ao_get_repair(struct sock * sk,sockptr_t optval,sockptr_t optlen)347 static inline int tcp_ao_get_repair(struct sock *sk,
348 				    sockptr_t optval, sockptr_t optlen)
349 {
350 	return -ENOPROTOOPT;
351 }
352 
tcp_ao_set_repair(struct sock * sk,sockptr_t optval,unsigned int optlen)353 static inline int tcp_ao_set_repair(struct sock *sk,
354 				    sockptr_t optval, unsigned int optlen)
355 {
356 	return -ENOPROTOOPT;
357 }
358 #endif
359 
360 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
361 int tcp_do_parse_auth_options(const struct tcphdr *th,
362 			      const u8 **md5_hash, const u8 **ao_hash);
363 #else
tcp_do_parse_auth_options(const struct tcphdr * th,const u8 ** md5_hash,const u8 ** ao_hash)364 static inline int tcp_do_parse_auth_options(const struct tcphdr *th,
365 		const u8 **md5_hash, const u8 **ao_hash)
366 {
367 	*md5_hash = NULL;
368 	*ao_hash = NULL;
369 	return 0;
370 }
371 #endif
372 
373 #endif /* _TCP_AO_H */
374