@@ -14,11 +14,12 @@ import (
14
14
"strings"
15
15
16
16
"github.com/cilium/tetragon/api/v1/tetragon"
17
+ "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
18
+
17
19
"github.com/cilium/tetragon/pkg/api/processapi"
18
20
"github.com/cilium/tetragon/pkg/config"
19
21
gt "github.com/cilium/tetragon/pkg/generictypes"
20
22
"github.com/cilium/tetragon/pkg/idtable"
21
- "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/v1alpha1"
22
23
"github.com/cilium/tetragon/pkg/kernels"
23
24
"github.com/cilium/tetragon/pkg/mbset"
24
25
"github.com/cilium/tetragon/pkg/reader/namespace"
@@ -536,17 +537,16 @@ func writeMatchAddrsInMap(k *KernelSelectorState, values []string) error {
536
537
for _ , v := range values {
537
538
addr , maskLen , err := parseAddr (v )
538
539
if err != nil {
539
- return fmt .Errorf ("MatchArgs value %s invalid: %w" , v , err )
540
+ return fmt .Errorf ("MatchArgs value %s invalid: parse IP: %w" , v , err )
540
541
}
541
- if len (addr ) == 4 {
542
+
543
+ if len (addr ) == net .IPv4len {
542
544
val := KernelLPMTrie4 {prefixLen : maskLen , addr : binary .LittleEndian .Uint32 (addr )}
543
545
m4 [val ] = struct {}{}
544
- } else if len ( addr ) == 16 {
546
+ } else {
545
547
val := KernelLPMTrie6 {prefixLen : maskLen }
546
548
copy (val .addr [:], addr )
547
549
m6 [val ] = struct {}{}
548
- } else {
549
- return fmt .Errorf ("MatchArgs value %s invalid: should be either 4 or 16 bytes long" , v )
550
550
}
551
551
}
552
552
// write the map ids into the selector
@@ -576,45 +576,55 @@ func getBase(v string) int {
576
576
}
577
577
578
578
func parseAddr (v string ) ([]byte , uint32 , error ) {
579
- ipaddr := net .ParseIP (v )
580
- if ipaddr != nil {
581
- ipaddr4 := ipaddr .To4 ()
582
- if ipaddr4 != nil {
583
- return ipaddr4 , 32 , nil
584
- }
585
- ipaddr6 := ipaddr .To16 ()
586
- if ipaddr6 != nil {
587
- return ipaddr6 , 128 , nil
588
- }
589
- return nil , 0 , errors .New ("IP address is not valid: does not parse as IPv4 or IPv6" )
590
- }
579
+ var maskLen uint32
580
+
591
581
vParts := strings .Split (v , "/" )
592
- if len (vParts ) != 2 {
593
- return nil , 0 , errors .New ("IP address is not valid: should be in format ADDR or ADDR/MASKLEN" )
594
- }
595
- ipaddr = net .ParseIP (vParts [0 ])
596
- if ipaddr == nil {
597
- return nil , 0 , errors .New ("IP CIDR is not valid: address part does not parse as IPv4 or IPv6" )
582
+ switch len (vParts ) {
583
+ case 1 :
584
+ case 2 :
585
+ x , err := strconv .ParseUint (vParts [1 ], 10 , 32 )
586
+ if err != nil {
587
+ return nil , 0 , errors .New ("CIDR mask is invalid" )
588
+ }
589
+ maskLen = uint32 (x )
590
+ default :
591
+ return nil , 0 , errors .New ("IP address is invalid: invalid format" )
598
592
}
599
- maskLen , err := strconv .ParseUint (vParts [1 ], 10 , 32 )
600
- if err != nil {
601
- return nil , 0 , errors .New ("IP CIDR is not valid: mask part does not parse" )
593
+
594
+ ipAddr := net .ParseIP (vParts [0 ])
595
+ if ipAddr == nil {
596
+ return nil , 0 , errors .New ("IP address is invalid: failed to parse" )
602
597
}
603
- ipaddr4 := ipaddr .To4 ()
604
- if ipaddr4 != nil {
605
- if maskLen <= 32 {
606
- return ipaddr4 , uint32 (maskLen ), nil
598
+
599
+ // IPv4-mapped IPv6 form of address (::ffff:x.x.x.x) will
600
+ // be successfully parsed as IPv4 address, but we want to consider
601
+ // such form only as IPv6 address to add it to corresponding map,
602
+ // so parse IPv4 address only in case of absence of colon.
603
+ if ! strings .Contains (v , ":" ) {
604
+ ip4 := ipAddr .To4 ()
605
+ if ip4 == nil {
606
+ return nil , 0 , errors .New ("IPv4 address is invalid" )
607
607
}
608
- return nil , 0 , errors .New ("IP CIDR is not valid: IPv4 mask len must be <= 32" )
609
- }
610
- ipaddr6 := ipaddr .To16 ()
611
- if ipaddr6 != nil {
612
- if maskLen <= 128 {
613
- return ipaddr6 , uint32 (maskLen ), nil
608
+ if maskLen == 0 {
609
+ maskLen = 32
610
+ } else if maskLen > 32 {
611
+ return nil , 0 , errors .New ("IPv4 mask len must be <= 32" )
614
612
}
615
- return nil , 0 , errors .New ("IP CIDR is not valid: IPv6 mask len must be <= 128" )
613
+
614
+ return ip4 , maskLen , nil
615
+ }
616
+
617
+ ip6 := ipAddr .To16 ()
618
+ if ip6 == nil {
619
+ return nil , 0 , errors .New ("IPv6 address is invalid" )
620
+ }
621
+ if maskLen == 0 {
622
+ maskLen = 128
623
+ } else if maskLen > 128 {
624
+ return nil , 0 , errors .New ("IPv6 mask len must be <= 128" )
616
625
}
617
- return nil , 0 , errors .New ("IP CIDR is not valid: address part does not parse" )
626
+
627
+ return ip6 , maskLen , nil
618
628
}
619
629
620
630
func writeMatchValues (k * KernelSelectorState , values []string , ty , op uint32 ) error {
0 commit comments