Debian Codegraph @

drivers/net/ovpn-dco/pktid.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0
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 #include "pktid.h"
11
12 #include <linux/atomic.h>
13 #include <linux/jiffies.h>
14
15 void ovpn_pktid_xmit_init(struct ovpn_pktid_xmit *pid)
16 {
17 atomic64_set(&pid->seq_num, 1);
18 pid->tcp_linear = NULL;
19 }
20
21 void ovpn_pktid_recv_init(struct ovpn_pktid_recv *pr)
22 {
23 memset(pr, 0, sizeof(*pr));
24 spin_lock_init(&pr->lock);
25 }
26
27 /* Packet replay detection.
28 * Allows ID backtrack of up to REPLAY_WINDOW_SIZE - 1.
29 */
30 int ovpn_pktid_recv(struct ovpn_pktid_recv *pr, u32 pkt_id, u32 pkt_time)
31 {
32 const unsigned long now = jiffies;
33 int ret;
34
35 spin_lock(&pr->lock);
36
37 /* expire backtracks at or below pr->id after PKTID_RECV_EXPIRE time */
38 if (unlikely(time_after_eq(now, pr->expire)))
39 pr->id_floor = pr->id;
40
41 /* ID must not be zero */
42 if (unlikely(pkt_id == 0)) {
43 ret = -EINVAL;
44 goto out;
45 }
46
47 /* time changed? */
48 if (unlikely(pkt_time != pr->time)) {
49 if (pkt_time > pr->time) {
50 /* time moved forward, accept */
51 pr->base = 0;
52 pr->extent = 0;
53 pr->id = 0;
54 pr->time = pkt_time;
55 pr->id_floor = 0;
56 } else {
57 /* time moved backward, reject */
58 ret = -ETIME;
59 goto out;
60 }
61 }
62
63 if (likely(pkt_id == pr->id + 1)) {
64 /* well-formed ID sequence (incremented by 1) */
65 pr->base = REPLAY_INDEX(pr->base, -1);
66 pr->history[pr->base / 8] |= (1 << (pr->base % 8));
67 if (pr->extent < REPLAY_WINDOW_SIZE)
68 ++pr->extent;
69 pr->id = pkt_id;
70 } else if (pkt_id > pr->id) {
71 /* ID jumped forward by more than one */
72 const unsigned int delta = pkt_id - pr->id;
73
74 if (delta < REPLAY_WINDOW_SIZE) {
75 unsigned int i;
76
77 pr->base = REPLAY_INDEX(pr->base, -delta);
78 pr->history[pr->base / 8] |= (1 << (pr->base % 8));
79 pr->extent += delta;
80 if (pr->extent > REPLAY_WINDOW_SIZE)
81 pr->extent = REPLAY_WINDOW_SIZE;
82 for (i = 1; i < delta; ++i) {
83 unsigned int newb = REPLAY_INDEX(pr->base, i);
84
85 pr->history[newb / 8] &= ~BIT(newb % 8);
86 }
87 } else {
88 pr->base = 0;
89 pr->extent = REPLAY_WINDOW_SIZE;
90 memset(pr->history, 0, sizeof(pr->history));
91 pr->history[0] = 1;
92 }
93 pr->id = pkt_id;
94 } else {
95 /* ID backtrack */
96 const unsigned int delta = pr->id - pkt_id;
97
98 if (delta > pr->max_backtrack)
99 pr->max_backtrack = delta;
100 if (delta < pr->extent) {
101 if (pkt_id > pr->id_floor) {
102 const unsigned int ri = REPLAY_INDEX(pr->base,
103 delta);
104 u8 *p = &pr->history[ri / 8];
105 const u8 mask = (1 << (ri % 8));
106
107 if (*p & mask) {
108 ret = -EINVAL;
109 goto out;
110 }
111 *p |= mask;
112 } else {
113 ret = -EINVAL;
114 goto out;
115 }
116 } else {
117 ret = -EINVAL;
118 goto out;
119 }
120 }
121
122 pr->expire = now + PKTID_RECV_EXPIRE;
123 ret = 0;
124 out:
125 spin_unlock(&pr->lock);
126 return ret;
127 }