Debian Codegraph @

drivers/net/ovpn-dco/pktid.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* OpenVPN data channel accelerator
3 *
4 * Copyright (C) 2020- OpenVPN, Inc.
5 *
6 * Author: Antonio Quartulli <[email protected]>
7 * James Yonan <[email protected]>
8 */
9
10 #ifndef _NET_OVPN_DCO_OVPNPKTID_H_
11 #define _NET_OVPN_DCO_OVPNPKTID_H_
12
13 #include "main.h"
14
15 /* When the OpenVPN protocol is run in AEAD mode, use
16 * the OpenVPN packet ID as the AEAD nonce:
17 *
18 * 00000005 521c3b01 4308c041
19 * [seq # ] [ nonce_tail ]
20 * [ 12-byte full IV ] -> NONCE_SIZE
21 * [4-bytes -> NONCE_WIRE_SIZE
22 * on wire]
23 */
24
25 /* OpenVPN nonce size */
26 #define NONCE_SIZE 12
27 /* amount of bytes of the nonce received from user space */
28 #define NONCE_TAIL_SIZE 8
29
30 /* OpenVPN nonce size reduced by 8-byte nonce tail -- this is the
31 * size of the AEAD Associated Data (AD) sent over the wire
32 * and is normally the head of the IV
33 */
34 #define NONCE_WIRE_SIZE (NONCE_SIZE - sizeof(struct ovpn_nonce_tail))
35
36 /* If no packets received for this length of time, set a backtrack floor
37 * at highest received packet ID thus far.
38 */
39 #define PKTID_RECV_EXPIRE (30 * HZ)
40
41 /* Last 8 bytes of AEAD nonce
42 * Provided by userspace and usually derived from
43 * key material generated during TLS handshake
44 */
45 struct ovpn_nonce_tail {
46 u8 u8[NONCE_TAIL_SIZE];
47 };
48
49 /* Packet-ID state for transmitter */
50 struct ovpn_pktid_xmit {
51 atomic64_t seq_num;
52 struct ovpn_tcp_linear *tcp_linear;
53 };
54
55 /* replay window sizing in bytes = 2^REPLAY_WINDOW_ORDER */
56 #define REPLAY_WINDOW_ORDER 8
57
58 #define REPLAY_WINDOW_BYTES BIT(REPLAY_WINDOW_ORDER)
59 #define REPLAY_WINDOW_SIZE (REPLAY_WINDOW_BYTES * 8)
60 #define REPLAY_INDEX(base, i) (((base) + (i)) & (REPLAY_WINDOW_SIZE - 1))
61
62 /* Packet-ID state for receiver.
63 * Other than lock member, can be zeroed to initialize.
64 */
65 struct ovpn_pktid_recv {
66 /* "sliding window" bitmask of recent packet IDs received */
67 u8 history[REPLAY_WINDOW_BYTES];
68 /* bit position of deque base in history */
69 unsigned int base;
70 /* extent (in bits) of deque in history */
71 unsigned int extent;
72 /* expiration of history in jiffies */
73 unsigned long expire;
74 /* highest sequence number received */
75 u32 id;
76 /* highest time stamp received */
77 u32 time;
78 /* we will only accept backtrack IDs > id_floor */
79 u32 id_floor;
80 unsigned int max_backtrack;
81 /* protects entire pktd ID state */
82 spinlock_t lock;
83 };
84
85 /* Get the next packet ID for xmit */
86 static inline int ovpn_pktid_xmit_next(struct ovpn_pktid_xmit *pid, u32 *pktid)
87 {
88 const s64 seq_num = atomic64_fetch_add_unless(&pid->seq_num, 1,
89 0x100000000LL);
90 /* when the 32bit space is over, we return an error because the packet ID is used to create
91 * the cipher IV and we do not want to re-use the same value more than once
92 */
93 if (unlikely(seq_num == 0x100000000LL))
94 return -ERANGE;
95
96 *pktid = (u32)seq_num;
97
98 return 0;
99 }
100
101 /* Write 12-byte AEAD IV to dest */
102 static inline void ovpn_pktid_aead_write(const u32 pktid,
103 const struct ovpn_nonce_tail *nt,
104 unsigned char *dest)
105 {
106 *(__force __be32 *)(dest) = htonl(pktid);
107 BUILD_BUG_ON(4 + sizeof(struct ovpn_nonce_tail) != NONCE_SIZE);
108 memcpy(dest + 4, nt->u8, sizeof(struct ovpn_nonce_tail));
109 }
110
111 void ovpn_pktid_xmit_init(struct ovpn_pktid_xmit *pid);
112 void ovpn_pktid_recv_init(struct ovpn_pktid_recv *pr);
113
114 int ovpn_pktid_recv(struct ovpn_pktid_recv *pr, u32 pkt_id, u32 pkt_time);
115
116 #endif /* _NET_OVPN_DCO_OVPNPKTID_H_ */