Debian Codegraph @

src/proto/gre.c

raw symbols SCIP
1
2 #include "gre.h"
3 #include "protocol_gre.h"
4 #include "common/attributes.h"
5 #include "config.h"
6 #include "crypto/psk.h"
7 #include "log-private.h"
8 #include "logging.h"
9 #include "rist-private.h"
10 #include "endian-shim.h"
11 #include "udp-private.h"
12 #include "transport-private.h"
13 #include "eap.h"
14 #include "peer.h"
15
16 #include <errno.h>
17 #include <stddef.h>
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 ssize_t _librist_proto_gre_send_data(struct rist_peer *p, uint8_t payload_type, uint16_t proto, uint8_t *payload, size_t payload_len, uint16_t src_port, uint16_t dst_port, uint8_t gre_version) {
25 bool encrypt = (p->key_tx.key_size > 0) && proto != RIST_GRE_PROTOCOL_TYPE_EAPOL;
26
27 /* Our encryption and compression operations directly modify the payload buffer we receive as a pointer
28 so we create a local pointer that points to the payload pointer, if we would either encrypt or compress we instead
29 malloc and mempcy, to ensure our source stays clean. We only do this with RAW data as these buffers are the only
30 assumed to be reused by retransmits */
31 bool modifying_payload = encrypt && (payload_type == RIST_PAYLOAD_TYPE_DATA_RAW || payload_type == RIST_PAYLOAD_TYPE_DATA_RAW_RTP_EXT);//Need to make a copy of our data if we'd resend it in the future, otherwise we can safely overwrite the buffer
32
33 uint8_t *payload_wr = payload;
34 uint8_t hdr_buf[MAX_GRE_SIZE];
35 struct rist_gre_hdr* hdr = (struct rist_gre_hdr *)hdr_buf;
36 memset(hdr, 0, sizeof(*hdr));
37 size_t hdr_len = sizeof(*hdr);
38
39
40 struct rist_peer *key_peer = p;
41 if (key_peer->parent != NULL && key_peer->parent->multicast_sender)
42 key_peer = key_peer->parent;
43
44 uint32_t seq;
45 assert(payload != NULL);
46
47 const size_t nonce_offset = sizeof(*hdr);
48 hdr->flags2 = (gre_version &0x7) << 3;
49
50 if (encrypt) {
51 hdr_len += 4;
52 if (gre_version && p->key_tx.key_size == 256)
53 {
54 hdr->flags2 |= (1 & 1UL) << 6;
55 }
56 }
57 //VSF TR-06-02:_2022-08-11 wants us to send a sequence if we're capable of sending non-RIST traffic
58 //For now default to always considering us capable to do that.
59 if (true || encrypt) {
60 SET_BIT(hdr->flags1, 4); // set seq bit
61 seq = key_peer->seq++;
62 //Write sequence number
63 hdr_buf[hdr_len] = seq >> 24;
64 hdr_buf[hdr_len+1] = seq >> 16;
65 hdr_buf[hdr_len+2] = seq >> 8;
66 hdr_buf[hdr_len+3] = seq;
67 hdr_len += 4;
68 }
69
70 size_t hdr_payload_offset = hdr_len;
71
72 //If we can use the new VSF ethertype (rist GRE version >= 2), we should, as the other way is considered deprecated
73 if ((gre_version >= 2 && (proto == RIST_GRE_PROTOCOL_TYPE_REDUCED || proto == RIST_GRE_PROTOCOL_TYPE_KEEPALIVE || proto == RIST_VSF_PROTOCOL_SUBTYPE_BUFFER_NEGOTIATION))) {
74 struct rist_vsf_proto *vsf = (struct rist_vsf_proto *)&hdr_buf[hdr_len];
75 hdr_len += sizeof(*vsf);
76 vsf->type = RIST_VSF_PROTOCOL_TYPE_RIST;//Network byte order! these are 0 values, so safe to do without htobe16
77 if (proto == RIST_GRE_PROTOCOL_TYPE_REDUCED)
78 vsf->subtype = RIST_VSF_PROTOCOL_SUBTYPE_REDUCED;
79 else if (proto == RIST_VSF_PROTOCOL_SUBTYPE_BUFFER_NEGOTIATION)
80 vsf->subtype = htobe16(RIST_VSF_PROTOCOL_SUBTYPE_BUFFER_NEGOTIATION);
81 else
82 vsf->subtype = htobe16(RIST_VSF_PROTOCOL_SUBTYPE_KEEPALIVE);
83
84 hdr->prot_type = htobe16(RIST_GRE_PROTOCOL_TYPE_VSF);
85 } else {
86 hdr->prot_type = htobe16(proto);
87 }
88
89 if (proto == RIST_GRE_PROTOCOL_TYPE_REDUCED) {
90 struct rist_reduced *red = (struct rist_reduced *)(&hdr_buf[hdr_len]);
91 hdr_len += sizeof(*red);
92 red->dst_port = htobe16(dst_port);
93 red->src_port = htobe16(src_port);
94 }
95
96 if (encrypt) {
97 if (modifying_payload || (hdr_payload_offset != hdr_len && !HAVE_MBEDTLS)) {
98 payload_wr = malloc(payload_len + 8);//Let's get rid of this malloc in the hotpath here
99 modifying_payload = true;
100 assert(payload_wr);
101 }
102 pthread_mutex_lock(&key_peer->peer_lock);
103 struct rist_key *key = &key_peer->key_tx;
104
105 #if HAVE_SRP_SUPPORT
106 if (librist_peer_should_rollover_passphrase(key_peer)) {
107 key_peer->key_tx_odd_active = !key_peer->key_tx_odd_active;
108 rist_log_priv(get_cctx(p), RIST_LOG_INFO, "Rolling over to %s passphrase\n", key_peer->key_tx_odd_active? "odd" : "even");
109 }
110 #endif
111
112 if (key_peer->key_tx_odd_active)
113 key = &p->key_tx_odd;
114
115 //Data that should be encrypted is part of the header
116 if (hdr_payload_offset != hdr_len) {
117 #if HAVE_MBEDTLS
118 //MbedTLS allows us to continue encryption, so we can encrypt hdr & payload in 2 ops
119 _librist_crypto_psk_encrypt(key, htobe32(seq), gre_version, &hdr_buf[hdr_payload_offset], &hdr_buf[hdr_payload_offset], hdr_len - hdr_payload_offset);
120 _librist_crypto_psk_encrypt_continue(key, payload, payload_wr, payload_len);
121 #else
122 memcpy(payload_wr, &hdr_buf[hdr_payload_offset], hdr_len - hdr_payload_offset);
123 memcpy(&payload_wr[hdr_len - hdr_payload_offset], payload, payload_len);
124 payload_len += (hdr_len - hdr_payload_offset);
125 hdr_len -= (hdr_len - hdr_payload_offset);
126 _librist_crypto_psk_encrypt(key, htobe32(seq), gre_version, payload_wr, payload_wr, payload_len);
127 #endif
128 } else {
129 //Single encryption pass suffices
130 _librist_crypto_psk_encrypt(key, htobe32(seq), gre_version, payload, payload_wr, payload_len);
131 }
132 pthread_mutex_unlock(&key_peer->peer_lock);
133 SET_BIT(hdr->flags1, 5); // set key bit
134 //Write key, our nonce is stored in network byte order so just memcpy it
135 memcpy(&hdr_buf[nonce_offset], key->gre_nonce, sizeof(p->key_tx.gre_nonce));
136 }
137
138 ssize_t ret;
139 int errorcode = 0;
140
141 /* Type 8 wrapping: when both sides have negotiated Advanced Profile,
142 * wrap outbound GRE packets in an AP Type 8 envelope so the remote
143 * can accept them on a unified AP port. */
144 if (get_cctx(p)->profile == RIST_PROFILE_ADVANCED &&
145 p->is_advanced && p->remote_supports_advanced) {
146 uint8_t gre_flat[RIST_MAX_PACKET_SIZE];
147 size_t gre_total = hdr_len + payload_len;
148 if (gre_total <= sizeof(gre_flat)) {
149 memcpy(gre_flat, hdr_buf, hdr_len);
150 memcpy(gre_flat + hdr_len, payload_wr, payload_len);
151 ret = (rist_adv_send_type8(p, gre_flat, gre_total) == 0)
152 ? (ssize_t)gre_total : -1;
153 if (modifying_payload)
154 free(payload_wr);
155 return ret;
156 }
157 }
158
159 #ifndef _WIN32
160 struct msghdr msghdr;
161 struct iovec iov[2];
162 iov[0].iov_base = hdr_buf;
163 iov[0].iov_len = hdr_len;
164 iov[1].iov_base = payload_wr;
165 iov[1].iov_len = payload_len;
166 msghdr.msg_iov = iov;
167 msghdr.msg_iovlen = 2;
168 msghdr.msg_name = &p->u.address;
169 msghdr.msg_namelen = p->address_len;
170 msghdr.msg_control = NULL;
171 msghdr.msg_controllen = 0;
172 msghdr.msg_flags = 0;
173 int retries = 0;
174 do {
175 ret = rist_transport_sendmsg(p, &msghdr, MSG_DONTWAIT);
176 if (RIST_UNLIKELY(ret < 0)) {
177 errorcode = errno;
178 retries++;
179 }
180 else {
181 errorcode = 0;
182 break;
183 }
184 } while (errorcode == EAGAIN && retries < RIST_MAX_SEND_RETRIES);
185 #else
186 WSAMSG msghdr = { 0 };
187 WSABUF iov[2];
188 DWORD dwBytes = 0;
189 iov[0].len = hdr_len;
190 iov[0].buf = (char *)hdr_buf;
191 iov[1].len = payload_len;
192 iov[1].buf = (char *)payload_wr;
193 msghdr.name = (struct sockaddr *)&p->u.address;
194 msghdr.namelen = p->address_len;
195 msghdr.lpBuffers = iov;
196 msghdr.dwBufferCount = 2;
197 ret = payload_len + hdr_len;
198 int retries = 0;
199 do {
200 if (RIST_UNLIKELY(WSASendMsg(p->sd, &msghdr, MSG_DONTWAIT, &dwBytes, NULL, NULL) != 0)) {
201 ret = -1;
202 errorcode = WSAGetLastError();
203 retries++;
204 }
205 else {
206 errorcode = 0;
207 break;
208 }
209 } while (errorcode == WSAEWOULDBLOCK && retries < RIST_MAX_SEND_RETRIES);
210 #endif
211
212 if (modifying_payload) {
213 free(payload_wr);
214 }
215
216 if (RIST_UNLIKELY(errorcode)) {
217 _librist_log_send_error(p, errorcode, (size_t)(hdr_len + payload_len),
218 "main-profile sendmsg");
219 } else if (RIST_UNLIKELY(retries > RIST_MAX_SEND_RETRIES / 5)) {
220 struct rist_common_ctx *ctx = get_cctx(p);
221 rist_log_priv(ctx, RIST_LOG_WARN, "Send Succeded after retries=%d, ret=%d, socket=%d\n", retries, ret, p->sd);
222 }
223
224 return ret;
225 }
226
227 void _librist_proto_gre_send_keepalive(struct rist_peer *p, uint8_t gre_version) {
228 uint8_t ka_buf[12] = {0};
229 struct rist_gre_keepalive *ka = (struct rist_gre_keepalive *)ka_buf;
230 memcpy(ka->mac_array, p->mac_addr, sizeof(ka->mac_array));
231 SET_BIT(ka->capabilities1, 0); // Null packet deletion
232 if (p->sender_ctx && p->sender_ctx->split_mode != LIBRIST_SPLIT_MODE_OFF)
233 SET_BIT(ka->capabilities1, 1); // Pair-split active
234 SET_BIT(ka->capabilities1, 2); // SMPTE-2022-7
235 SET_BIT(ka->capabilities1, 5); // Bonding
236 SET_BIT(ka->capabilities2, 5); // Reduced overhead
237
238 struct rist_common_ctx *cctx = get_cctx(p);
239 size_t ka_len = sizeof(struct rist_gre_keepalive);
240 if (cctx && cctx->profile == RIST_PROFILE_ADVANCED) {
241 /* TR-06-3 Section 5.3.6: I bit (bit 31 of 32-bit capabilities) */
242 ka_buf[8] = 0x80; /* I bit = bit 7 of capabilities3 (= bit 31 overall) */
243 ka_buf[9] = 0;
244 ka_buf[10] = 0;
245 ka_buf[11] = 0;
246 ka_len = 12;
247 }
248 _librist_proto_gre_send_data(p, 0, RIST_GRE_PROTOCOL_TYPE_KEEPALIVE, ka_buf, ka_len, 0, 0, gre_version);
249 }
250
251 int _librist_proto_gre_parse_keepalive(const uint8_t buf[], size_t buflen, struct rist_keepalive_info *info) {
252 memset(info, 0, sizeof(*info));
253 if (buflen < sizeof(struct rist_gre_keepalive)) {
254 return -1;
255 }
256 struct rist_gre_keepalive *ka = (struct rist_gre_keepalive *)buf;
257 memcpy(&info->ka, ka, SIZEOF_GRE_KEEPALIVE);
258 memcpy(info->mac, ka->mac_array, sizeof(ka->mac_array));
259 info->x = CHECK_BIT(ka->capabilities1, 7);
260 info->r = CHECK_BIT(ka->capabilities1, 6);
261 info->b = CHECK_BIT(ka->capabilities1, 5);
262 info->a = CHECK_BIT(ka->capabilities1, 4);
263 info->p = CHECK_BIT(ka->capabilities1, 3);
264 info->e = CHECK_BIT(ka->capabilities1, 2);
265 info->l = CHECK_BIT(ka->capabilities1, 1);
266 info->n = CHECK_BIT(ka->capabilities1, 0);
267 info->d = CHECK_BIT(ka->capabilities2, 7);
268 info->t = CHECK_BIT(ka->capabilities2, 6);
269 info->v = CHECK_BIT(ka->capabilities2, 5);
270 info->j = CHECK_BIT(ka->capabilities2, 4);
271 info->f = CHECK_BIT(ka->capabilities2, 3);
272 /* TR-06-3 extended capabilities (4 bytes at offset 8, if present) */
273 if (buflen >= sizeof(*ka) + 4) {
274 info->adv_i = !!(buf[sizeof(*ka)] & 0x80); /* bit 31 → I */
275 info->adv_g = !!(buf[sizeof(*ka)] & 0x40); /* bit 30 → G */
276 info->adv_c = !!(buf[sizeof(*ka)] & 0x20); /* bit 29 → C */
277 info->json_len = buflen - sizeof(*ka) - 4;
278 if (info->json_len > 0)
279 info->json = (const char *)&buf[sizeof(*ka) + 4];
280 } else {
281 info->json_len = buflen - sizeof(*ka);
282 if (info->json_len > 0)
283 info->json = (const char *)&buf[sizeof(*ka)];
284 }
285 return 0;
286 }
287
288 void _librist_proto_gre_send_buffer_negotiation(struct rist_peer *p, uint16_t sender_max_buffer, uint16_t receiver_current_buffer) {
289 struct rist_buffer_negotiation bn;
290 bn.sender_max_buffer_size_ms = htobe16(sender_max_buffer);
291 bn.receiver_current_buffer_size_ms = htobe16(receiver_current_buffer);
292 bn.protocol_type = 0;
293 _librist_proto_gre_send_data(p, 0, RIST_VSF_PROTOCOL_SUBTYPE_BUFFER_NEGOTIATION, (uint8_t *)&bn, sizeof(bn), 0, 0, 2);
294 }
295
296 int _librist_proto_gre_parse_buffer_negotiation(struct rist_peer *p, uint8_t buf[], size_t buflen, uint16_t *sender_max_buffer, uint16_t *receiver_current_buffer) {
297 if (buflen < sizeof(struct rist_buffer_negotiation)) {
298 return -1;
299 }
300 struct rist_buffer_negotiation *bn = (struct rist_buffer_negotiation *)buf;
301 if (bn->protocol_type != 0) {
302 bn->sender_max_buffer_size_ms = 0;
303 bn->receiver_current_buffer_size_ms = 0;
304 _librist_proto_gre_send_data(p, 0, RIST_VSF_PROTOCOL_SUBTYPE_BUFFER_NEGOTIATION, buf, buflen, 0, 0, 2);
305 return -2;
306 }
307 *sender_max_buffer = be16toh(bn->sender_max_buffer_size_ms);
308 *receiver_current_buffer = be16toh(bn->receiver_current_buffer_size_ms);
309 return 0;
310 }