summaryrefslogtreecommitdiffhomepage
path: root/device
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-05-18 14:32:31 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-05-18 15:43:22 -0600
commit99eb7896be17cc688f001886469fb109b0575cad (patch)
tree77f9ada331ad2152ada7a19188d277883a1d89a0 /device
parentf60b3919bec891d37652edc25c48d83345d9885c (diff)
device: rework padding calculation and don't shadow paddedSize
Reported-by: Jayakumar S <jayakumar82.s@gmail.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'device')
-rw-r--r--device/send.go32
1 files changed, 17 insertions, 15 deletions
diff --git a/device/send.go b/device/send.go
index 9be1233..c0bdba3 100644
--- a/device/send.go
+++ b/device/send.go
@@ -448,6 +448,21 @@ func (peer *Peer) RoutineNonce() {
}
}
+func calculatePaddingSize(packetSize, mtu int) int {
+ lastUnit := packetSize
+ if mtu == 0 {
+ return ((lastUnit + PaddingMultiple - 1) & ^(PaddingMultiple - 1)) - lastUnit
+ }
+ if lastUnit > mtu {
+ lastUnit %= mtu
+ }
+ paddedSize := ((lastUnit + PaddingMultiple - 1) & ^(PaddingMultiple - 1))
+ if paddedSize > mtu {
+ paddedSize = mtu
+ }
+ return paddedSize - lastUnit
+}
+
/* Encrypts the elements in the queue
* and marks them for sequential consumption (by releasing the mutex)
*
@@ -514,21 +529,8 @@ func (device *Device) RoutineEncryption() {
// pad content to multiple of 16
- mtu := int(atomic.LoadInt32(&device.tun.mtu))
- var paddedSize int
- if mtu == 0 {
- paddedSize = (len(elem.packet) + PaddingMultiple - 1) & ^(PaddingMultiple - 1)
- } else {
- lastUnit := len(elem.packet)
- if lastUnit > mtu {
- lastUnit %= mtu
- }
- paddedSize := (lastUnit + PaddingMultiple - 1) & ^(PaddingMultiple - 1)
- if paddedSize > mtu {
- paddedSize = mtu
- }
- }
- for i := len(elem.packet); i < paddedSize; i++ {
+ paddingSize := calculatePaddingSize(len(elem.packet), int(atomic.LoadInt32(&device.tun.mtu)))
+ for i := 0; i < paddingSize; i++ {
elem.packet = append(elem.packet, 0)
}