Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix computation of IPsec max. sequence number #27656

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 22 additions & 12 deletions cilium/cmd/encrypt_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"

"github.com/prometheus/procfs"
Expand All @@ -30,7 +31,7 @@ type void struct{}
var (
voidType void
countErrors int
regex = regexp.MustCompile("oseq[[:blank:]](0[xX][[:xdigit:]]+)?")
regex = regexp.MustCompile("oseq[[:blank:]]0[xX]([[:xdigit:]]+)")
)

var encryptStatusCmd = &cobra.Command{
Expand Down Expand Up @@ -89,27 +90,36 @@ func countUniqueIPsecKeys() int {
return len(keys)
}

func maxSequenceNumber() string {
maxSeqNum := "0"
out, err := exec.Command("ip", "xfrm", "state", "list", "reqid", ciliumReqId).Output()
if err != nil {
Fatalf("Cannot get xfrm states: %s", err)
}
commandOutput := string(out)
lines := strings.Split(commandOutput, "\n")
func extractMaxSequenceNumber(ipOutput string) int64 {
maxSeqNum := int64(0)
pchaigno marked this conversation as resolved.
Show resolved Hide resolved
lines := strings.Split(ipOutput, "\n")
for _, line := range lines {
matched := regex.FindStringSubmatchIndex(line)
if matched != nil {
pchaigno marked this conversation as resolved.
Show resolved Hide resolved
oseq := line[matched[2]:matched[3]]
oseq, err := strconv.ParseInt(line[matched[2]:matched[3]], 16, 64)
if err != nil {
Fatalf("Failed to parse sequence number '%s': %s",
line[matched[2]:matched[3]], err)
}
if oseq > maxSeqNum {
pchaigno marked this conversation as resolved.
Show resolved Hide resolved
maxSeqNum = oseq
}
}
}
if maxSeqNum == "0" {
return maxSeqNum
}

func maxSequenceNumber() string {
out, err := exec.Command("ip", "xfrm", "state", "list", "reqid", ciliumReqId).Output()
if err != nil {
Fatalf("Cannot get xfrm states: %s", err)
}
commandOutput := string(out)
maxSeqNum := extractMaxSequenceNumber(commandOutput)
if maxSeqNum == 0 {
return "N/A"
}
return fmt.Sprintf("%s/0xffffffff", maxSeqNum)
return fmt.Sprintf("0x%x/0xffffffff", maxSeqNum)
}

func getEncryptionMode() {
Expand Down
40 changes: 40 additions & 0 deletions cilium/cmd/encrypt_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,43 @@ func (s *EncryptStatusSuite) TestGetXfrmStats(c *C) {
}
c.Assert(currentCount, Equals, errCount)
}

func (s *EncryptStatusSuite) TestExtractMaxSequenceNumber(c *C) {
ipOutput := `src 10.84.1.32 dst 10.84.0.30
proto esp spi 0x00000003 reqid 1 mode tunnel
replay-window 0
mark 0x3cb23e00/0xffffff00 output-mark 0xe00/0xf00
aead rfc4106(gcm(aes)) 0x64ad37a9d8a8f20fb2e74ef6000f9d580898719f 128
anti-replay context: seq 0x0, oseq 0xc3, bitmap 0x00000000
sel src 0.0.0.0/0 dst 0.0.0.0/0
src 0.0.0.0 dst 10.84.1.32
proto esp spi 0x00000003 reqid 1 mode tunnel
replay-window 0
mark 0xd00/0xf00 output-mark 0xd00/0xf00
aead rfc4106(gcm(aes)) 0x64ad37a9d8a8f20fb2e74ef6000f9d580898719f 128
anti-replay context: seq 0x0, oseq 0x1410, bitmap 0x00000000
sel src 0.0.0.0/0 dst 0.0.0.0/0
src 10.84.1.32 dst 10.84.2.145
proto esp spi 0x00000003 reqid 1 mode tunnel
replay-window 0
mark 0x7e63e00/0xffffff00 output-mark 0xe00/0xf00
aead rfc4106(gcm(aes)) 0x64ad37a9d8a8f20fb2e74ef6000f9d580898719f 128
anti-replay context: seq 0x0, oseq 0x13e0, bitmap 0x00000000
sel src 0.0.0.0/0 dst 0.0.0.0/0`

maxSeqNumber := extractMaxSequenceNumber(ipOutput)
pchaigno marked this conversation as resolved.
Show resolved Hide resolved
c.Assert(maxSeqNumber, Equals, int64(0x1410))
}

// Attempt to simulate a case where the output would be interrupted mid-sentence.
func (s *EncryptStatusSuite) TestExtractMaxSequenceNumberError(c *C) {
ipOutput := `src 10.84.1.32 dst 10.84.0.30
proto esp spi 0x00000003 reqid 1 mode tunnel
replay-window 0
mark 0x3cb23e00/0xffffff00 output-mark 0xe00/0xf00
aead rfc4106(gcm(aes)) 0x64ad37a9d8a8f20fb2e74ef6000f9d580898719f 128
anti-replay context: seq 0x0, oseq 0x`

maxSeqNumber := extractMaxSequenceNumber(ipOutput)
c.Assert(maxSeqNumber, Equals, int64(0))
}