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