Debian Codegraph @

drivers/net/ovpn-dco/proto.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_OVPNPROTO_H_
11 #define _NET_OVPN_DCO_OVPNPROTO_H_
12
13 #include "main.h"
14
15 #include <linux/skbuff.h>
16
17 /* Methods for operating on the initial command
18 * byte of the OpenVPN protocol.
19 */
20
21 /* packet opcode (high 5 bits) and key-id (low 3 bits) are combined in
22 * one byte
23 */
24 #define OVPN_KEY_ID_MASK 0x07
25 #define OVPN_OPCODE_SHIFT 3
26 #define OVPN_OPCODE_MASK 0x1F
27 /* upper bounds on opcode and key ID */
28 #define OVPN_KEY_ID_MAX (OVPN_KEY_ID_MASK + 1)
29 #define OVPN_OPCODE_MAX (OVPN_OPCODE_MASK + 1)
30 /* packet opcodes of interest to us */
31 #define OVPN_DATA_V1 6 /* data channel V1 packet */
32 #define OVPN_DATA_V2 9 /* data channel V2 packet */
33 /* size of initial packet opcode */
34 #define OVPN_OP_SIZE_V1 1
35 #define OVPN_OP_SIZE_V2 4
36 #define OVPN_PEER_ID_MASK 0x00FFFFFF
37 #define OVPN_PEER_ID_UNDEF 0x00FFFFFF
38 /* first byte of keepalive message */
39 #define OVPN_KEEPALIVE_FIRST_BYTE 0x2a
40 /* first byte of exit message */
41 #define OVPN_EXPLICIT_EXIT_NOTIFY_FIRST_BYTE 0x28
42
43 /**
44 * Extract the OP code from the specified byte
45 *
46 * Return the OP code
47 */
48 static inline u8 ovpn_opcode_from_byte(u8 byte)
49 {
50 return byte >> OVPN_OPCODE_SHIFT;
51 }
52
53 /**
54 * Extract the OP code from the skb head.
55 *
56 * Note: this function assumes that the skb head was pulled enough
57 * to access the first byte.
58 *
59 * Return the OP code
60 */
61 static inline u8 ovpn_opcode_from_skb(const struct sk_buff *skb, u16 offset)
62 {
63 return ovpn_opcode_from_byte(*(skb->data + offset));
64 }
65
66 /**
67 * Extract the key ID from the skb head.
68 *
69 * Note: this function assumes that the skb head was pulled enough
70 * to access the first byte.
71 *
72 * Return the key ID
73 */
74
75 static inline u8 ovpn_key_id_from_skb(const struct sk_buff *skb)
76 {
77 return *skb->data & OVPN_KEY_ID_MASK;
78 }
79
80 /**
81 * Extract the peer ID from the skb head.
82 *
83 * Note: this function assumes that the skb head was pulled enough
84 * to access the first 4 bytes.
85 *
86 * Return the peer ID.
87 */
88
89 static inline u32 ovpn_peer_id_from_skb(const struct sk_buff *skb, u16 offset)
90 {
91 return ntohl(*(__be32 *)(skb->data + offset)) & OVPN_PEER_ID_MASK;
92 }
93
94 static inline u32 ovpn_opcode_compose(u8 opcode, u8 key_id, u32 peer_id)
95 {
96 const u8 op = (opcode << OVPN_OPCODE_SHIFT) | (key_id & OVPN_KEY_ID_MASK);
97
98 return (op << 24) | (peer_id & OVPN_PEER_ID_MASK);
99 }
100
101 #endif /* _NET_OVPN_DCO_OVPNPROTO_H_ */