[Feature] Support IPv6

This commit is contained in:
hengyoush
2024-09-16 18:45:08 +08:00
parent 8bf9dc3b92
commit 2cbefe0d03
353 changed files with 434 additions and 302 deletions

View File

@@ -81,11 +81,11 @@ func StartAgent(bpfAttachFunctions []bpf.AttachBpfProgFunction,
type ConnEventAssertions struct {
expectPid uint32
expectLocalIp string
expectLocalAddrFamily int
expectLocalAddrFamily uint16
expectLocalPort int
expectProtocol bpf.AgentTrafficProtocolT
expectRemoteIp string
expectRemoteFamily int
expectRemoteFamily uint16
expectRemotePort int
expectReadBytes uint64
expectWriteBytes uint64
@@ -100,17 +100,17 @@ func AssertConnEvent(t *testing.T, connectEvent bpf.AgentConnEvtT, assert ConnEv
t.Fatalf("Pid Incorrect: %d != %d", connectEvent.ConnInfo.ConnId.Upid.Pid, assert.expectPid)
}
expectLocalIp := assert.expectLocalIp
localIp := common.IntToIP(connectEvent.ConnInfo.Laddr.In4.SinAddr.S_addr)
localIp := string(common.BytesToNetIP(connectEvent.ConnInfo.Laddr.In6.Sin6Addr.In6U.U6Addr8[:], false))
if expectLocalIp != "" && localIp != expectLocalIp {
t.Fatalf("Local IP Incorrect: %s != %s", localIp, expectLocalIp)
}
localAddr := connectEvent.ConnInfo.Laddr
localAddrFamily := localAddr.In4.SinFamily
localAddrFamily := localAddr.In6.Sin6Family
expectLocalAddrFamily := assert.expectLocalAddrFamily
if expectLocalAddrFamily >= 0 && expectLocalAddrFamily != int(localAddrFamily) {
if expectLocalAddrFamily >= 0 && expectLocalAddrFamily != uint16(localAddrFamily) {
t.Fatalf("LocalAddr Family Incorrect: %d != %d", localAddrFamily, expectLocalAddrFamily)
}
localPort := localAddr.In4.SinPort
localPort := localAddr.In6.Sin6Port
expectLocalPort := assert.expectLocalPort
if expectLocalPort >= 0 && expectLocalPort != int(localPort) {
t.Fatalf("Local Port Incorrect: %d != %d", localPort, expectLocalPort)
@@ -121,17 +121,17 @@ func AssertConnEvent(t *testing.T, connectEvent bpf.AgentConnEvtT, assert ConnEv
t.Fatalf("Protocol Incorrect: %d != %d", protocol, expectProtocol)
}
remoteAddr := connectEvent.ConnInfo.Raddr
remoteIp := common.IntToIP(remoteAddr.In4.SinAddr.S_addr)
remoteIp := string(common.BytesToNetIP(remoteAddr.In6.Sin6Addr.In6U.U6Addr8[:], false))
expectRemoteIp := assert.expectRemoteIp
if expectRemoteIp != "" && expectRemoteIp != remoteIp {
t.Fatalf("Remote IP Incorrect: %s != %s", remoteIp, expectRemoteIp)
}
remoteAddrFamily := remoteAddr.In4.SinFamily
remoteAddrFamily := remoteAddr.In6.Sin6Family
expectRemoteFamily := assert.expectRemoteFamily
if expectRemoteFamily >= 0 && expectRemoteFamily != int(remoteAddrFamily) {
if expectRemoteFamily >= 0 && expectRemoteFamily != uint16(remoteAddrFamily) {
t.Fatalf("RemoteAddr Family Incorrect: %d != %d", remoteAddrFamily, expectRemoteFamily)
}
remotePort := remoteAddr.In4.SinPort
remotePort := remoteAddr.In6.Sin6Port
expectRemotePort := assert.expectRemotePort
if expectRemotePort >= 0 && expectRemotePort != int(remotePort) {
t.Fatalf("Remote Port Incorrect: %d != %d", remotePort, expectRemotePort)
@@ -290,10 +290,10 @@ func findInterestedConnEvent(t *testing.T, connEventList []bpf.AgentConnEvtT, op
t.Helper()
resultList := make([]bpf.AgentConnEvtT, 0)
for _, connEvent := range connEventList {
if options.findByRemotePort && options.remotePort != connEvent.ConnInfo.Raddr.In4.SinPort {
if options.findByRemotePort && options.remotePort != connEvent.ConnInfo.Raddr.In6.Sin6Port {
continue
}
if options.findByLocalPort && options.localPort != connEvent.ConnInfo.Laddr.In4.SinPort {
if options.findByLocalPort && options.localPort != connEvent.ConnInfo.Laddr.In6.Sin6Port {
continue
}
if options.findByConnType && options.connType != connEvent.ConnType {
@@ -325,10 +325,10 @@ func findInterestedSyscallEvents(t *testing.T, syscallEventList []bpf.SyscallEve
continue
}
connectEvent := connectEvents[0]
if options.findByRemotePort && connectEvent.ConnInfo.Raddr.In4.SinPort != options.remotePort {
if options.findByRemotePort && connectEvent.ConnInfo.Raddr.In6.Sin6Port != options.remotePort {
continue
}
if options.findByLocalPort && connectEvent.ConnInfo.Laddr.In4.SinPort != options.localPort {
if options.findByLocalPort && connectEvent.ConnInfo.Laddr.In6.Sin6Port != options.localPort {
continue
}
resultList = append(resultList, each)
@@ -354,10 +354,10 @@ func findInterestedKernEvents(t *testing.T, kernEventList []bpf.AgentKernEvt, op
continue
}
connectEvent := connectEvents[0]
if options.findByRemotePort && connectEvent.ConnInfo.Raddr.In4.SinPort != options.remotePort {
if options.findByRemotePort && connectEvent.ConnInfo.Raddr.In6.Sin6Port != options.remotePort {
continue
}
if options.findByLocalPort && connectEvent.ConnInfo.Laddr.In4.SinPort != options.localPort {
if options.findByLocalPort && connectEvent.ConnInfo.Laddr.In6.Sin6Port != options.localPort {
continue
}
if options.findDataLenGtZeroEvent && each.Len == 0 {

View File

@@ -8,6 +8,7 @@ import (
"kyanos/bpf"
"kyanos/common"
"kyanos/monitor"
"net"
"slices"
"sync"
"sync/atomic"
@@ -21,8 +22,8 @@ var RecordFunc func(protocol.Record, *Connection4) error
var OnCloseRecordFunc func(*Connection4) error
type Connection4 struct {
LocalIp common.Addr
RemoteIp common.Addr
LocalIp net.IP
RemoteIp net.IP
LocalPort common.Port
RemotePort common.Port
Protocol bpf.AgentTrafficProtocolT
@@ -193,18 +194,17 @@ func (c *Connection4) ProtocolInferred() bool {
func (c *Connection4) extractSockKeys() (bpf.AgentSockKey, bpf.AgentSockKey) {
var key bpf.AgentSockKey
key.Dip = common.BytesToInt[uint32](c.RemoteIp)
key.Sip = common.BytesToInt[uint32](c.LocalIp)
key.Dport = uint32(c.RemotePort)
key.Sport = uint32(c.LocalPort)
key.Family = uint32(common.AF_INET) // TODO @ipv6
key.Dip = [2]uint64(common.BytesToSockKey(c.RemoteIp))
key.Sip = [2]uint64(common.BytesToSockKey(c.LocalIp))
key.Dport = uint16(c.RemotePort)
key.Sport = uint16(c.LocalPort)
// key.Family = uint32(common.AF_INET) // TODO @ipv6
var revKey bpf.AgentSockKey
revKey.Sip = common.BytesToInt[uint32](c.RemoteIp)
revKey.Dip = common.BytesToInt[uint32](c.LocalIp)
revKey.Sport = uint32(c.RemotePort)
revKey.Dport = uint32(c.LocalPort)
revKey.Family = uint32(common.AF_INET)
revKey.Sip = [2]uint64(common.BytesToSockKey(c.RemoteIp))
revKey.Dip = [2]uint64(common.BytesToSockKey(c.LocalIp))
revKey.Sport = uint16(c.RemotePort)
revKey.Dport = uint16(c.LocalPort)
return key, revKey
}
@@ -412,6 +412,7 @@ func (c *Connection4) updateProgressTime(sb *buffer.StreamBuffer) {
} else {
c.lastRespMadeProgressTime = time.Now().UnixMilli()
}
// common.ConntrackLog.Debugf("%s update progress time to %v", c.ToString(), time.Now())
}
func (c *Connection4) getLastProgressTime(sb *buffer.StreamBuffer) int64 {
if c.reqStreamBuffer == sb {
@@ -425,7 +426,8 @@ func (c *Connection4) checkProgress(sb *buffer.StreamBuffer) bool {
c.updateProgressTime(sb)
return false
}
if time.Now().UnixMilli()-c.getLastProgressTime(sb) > 1000 {
headTime, ok := sb.FindTimestampBySeq(uint64(sb.Position0()))
if !ok || time.Now().UnixMilli()-int64(common.NanoToMills(headTime)) > 1000 {
sb.RemoveHead()
return true
} else {

View File

@@ -106,12 +106,18 @@ func (p *Processor) run() {
case event := <-p.connEvents:
TgidFd := uint64(event.ConnInfo.ConnId.Upid.Pid)<<32 | uint64(event.ConnInfo.ConnId.Fd)
var conn *Connection4
isIpv6 := event.ConnInfo.Laddr.In6.Sin6Family == common.AF_INET6
if isIpv6 {
common.DefaultLog.Warnf("ipv6: %x", event.ConnInfo.Laddr.In6.Sin6Addr.In6U.U6Addr8[:])
}
if event.ConnType == bpf.AgentConnTypeTKConnect {
conn = &Connection4{
LocalIp: common.IntToBytes(event.ConnInfo.Laddr.In4.SinAddr.S_addr),
RemoteIp: common.IntToBytes(event.ConnInfo.Raddr.In4.SinAddr.S_addr),
LocalPort: common.Port(event.ConnInfo.Laddr.In4.SinPort),
RemotePort: common.Port(event.ConnInfo.Raddr.In4.SinPort),
LocalIp: common.BytesToNetIP(event.ConnInfo.Laddr.In6.Sin6Addr.In6U.U6Addr8[:], isIpv6),
// LocalIp: common.IntToBytes(event.ConnInfo.Laddr.In4.SinAddr.S_addr),
RemoteIp: common.BytesToNetIP(event.ConnInfo.Raddr.In6.Sin6Addr.In6U.U6Addr8[:], isIpv6),
// RemoteIp: common.IntToBytes(event.ConnInfo.Raddr.In4.SinAddr.S_addr),
LocalPort: common.Port(event.ConnInfo.Laddr.In6.Sin6Port),
RemotePort: common.Port(event.ConnInfo.Raddr.In6.Sin6Port),
Protocol: event.ConnInfo.Protocol,
Role: event.ConnInfo.Role,
TgidFd: TgidFd,

View File

@@ -39,22 +39,22 @@ type AgentConnInfoT struct {
ReadBytes uint64
WriteBytes uint64
Laddr struct {
In4 struct {
SinFamily uint16
SinPort uint16
SinAddr struct{ S_addr uint32 }
Pad [8]uint8
In6 struct {
Sin6Family uint16
Sin6Port uint16
Sin6Flowinfo uint32
Sin6Addr struct{ In6U struct{ U6Addr8 [16]uint8 } }
Sin6ScopeId uint32
}
_ [12]byte
}
Raddr struct {
In4 struct {
SinFamily uint16
SinPort uint16
SinAddr struct{ S_addr uint32 }
Pad [8]uint8
In6 struct {
Sin6Family uint16
Sin6Port uint16
Sin6Flowinfo uint32
Sin6Addr struct{ In6U struct{ U6Addr8 [16]uint8 } }
Sin6ScopeId uint32
}
_ [12]byte
}
Protocol AgentTrafficProtocolT
Role AgentEndpointRoleT
@@ -110,11 +110,11 @@ type AgentKernEvtData struct {
}
type AgentSockKey struct {
Sip uint32
Dip uint32
Sport uint32
Dport uint32
Family uint32
Sip [2]uint64
Dip [2]uint64
Sport uint16
Dport uint16
_ [4]byte
}
type AgentStepT uint32

View File

@@ -39,22 +39,22 @@ type AgentOldConnInfoT struct {
ReadBytes uint64
WriteBytes uint64
Laddr struct {
In4 struct {
SinFamily uint16
SinPort uint16
SinAddr struct{ S_addr uint32 }
Pad [8]uint8
In6 struct {
Sin6Family uint16
Sin6Port uint16
Sin6Flowinfo uint32
Sin6Addr struct{ In6U struct{ U6Addr8 [16]uint8 } }
Sin6ScopeId uint32
}
_ [12]byte
}
Raddr struct {
In4 struct {
SinFamily uint16
SinPort uint16
SinAddr struct{ S_addr uint32 }
Pad [8]uint8
In6 struct {
Sin6Family uint16
Sin6Port uint16
Sin6Flowinfo uint32
Sin6Addr struct{ In6U struct{ U6Addr8 [16]uint8 } }
Sin6ScopeId uint32
}
_ [12]byte
}
Protocol AgentOldTrafficProtocolT
Role AgentOldEndpointRoleT
@@ -110,11 +110,11 @@ type AgentOldKernEvtData struct {
}
type AgentOldSockKey struct {
Sip uint32
Dip uint32
Sport uint32
Dport uint32
Family uint32
Sip [2]uint64
Dip [2]uint64
Sport uint16
Dport uint16
_ [4]byte
}
type AgentOldStepT uint32

Some files were not shown because too many files have changed in this diff Show More