Skip to content

Commit 654d892

Browse files
authored
Match on src ip for gtp4 headend (#27)
1 parent 51cca71 commit 654d892

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

internal/config/match.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
package config
66

77
type Match struct {
8-
Teid *uint32 `yaml:"teid,omitempty"`
8+
Teid *uint32 `yaml:"teid,omitempty"`
9+
InnerHeaderIPv4SrcPrefix *string `yaml:"inner-header-ipv4-src-prefix,omitempty"` // e.g. 192.168.0.1/32, Teid must be present
910
}

internal/netfunc/headend-gtp4.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,45 @@ func (h HeadendGTP4) Handle(packet []byte) ([]byte, error) {
7979
ipv4DA := pqt.NetworkLayer().NetworkFlow().Dst().Raw()
8080
argsMobSession := mup.NewArgsMobSession(qfi, reflectiveQosIndication, false, teid)
8181

82+
var innerHeaderIPv4 netip.Addr
83+
isInnerHeaderIPv4 := false
84+
8285
var bsid *config.Bsid
8386
for _, p := range h.policy {
8487
if p.Match == nil {
8588
bsid = &p.Bsid
8689
break
8790
}
8891
if p.Match.Teid != nil {
89-
if *p.Match.Teid == teid {
90-
bsid = &p.Bsid
91-
break
92+
if *p.Match.Teid != teid {
93+
continue
94+
}
95+
if p.Match.InnerHeaderIPv4SrcPrefix != nil {
96+
if !isInnerHeaderIPv4 {
97+
// init innerHeaderIPv4
98+
inner, ok := payload.(*layers.IPv4)
99+
if !ok {
100+
return nil, fmt.Errorf("Payload is not IPv4")
101+
}
102+
if inner.Version != 4 {
103+
return nil, fmt.Errorf("Payload is IPv%d instead of IPv4", inner.Version)
104+
}
105+
innerHeaderIPv4 = netip.AddrFrom4([4]byte{inner.SrcIP[0], inner.SrcIP[1], inner.SrcIP[2], inner.SrcIP[3]})
106+
isInnerHeaderIPv4 = true
107+
}
108+
prefix, err := netip.ParsePrefix(*p.Match.InnerHeaderIPv4SrcPrefix)
109+
if err != nil {
110+
return nil, fmt.Errorf("Malformed matching criteria (inner Header IPv4 Prefix): %s", err)
111+
}
112+
if prefix.Contains(innerHeaderIPv4) {
113+
bsid = &p.Bsid
114+
break
115+
}
92116
}
93117
}
94118
}
95119
if bsid == nil {
96-
return nil, fmt.Errorf("No policy found for this teid")
120+
return nil, fmt.Errorf("Could not found policy matching criterias")
97121
}
98122

99123
if bsid.BsidPrefix == nil {

0 commit comments

Comments
 (0)