diff --git a/ipset_linux.go b/ipset_linux.go index f4c05229f..95921a4de 100644 --- a/ipset_linux.go +++ b/ipset_linux.go @@ -314,13 +314,11 @@ func buildEntryData(entry *IPSetEntry) (*nl.RtAttr, error) { data.AddChild(nl.NewRtAttr(nl.IPSET_ATTR_CIDR2, nl.Uint8Attr(entry.CIDR2))) } - if entry.Port != nil { - if entry.Protocol == nil { - // use tcp protocol as default - val := uint8(unix.IPPROTO_TCP) - entry.Protocol = &val - } + if entry.Protocol != nil { data.AddChild(nl.NewRtAttr(nl.IPSET_ATTR_PROTO, nl.Uint8Attr(*entry.Protocol))) + } + + if entry.Port != nil { buf := make([]byte, 2) binary.BigEndian.PutUint16(buf, *entry.Port) data.AddChild(nl.NewRtAttr(int(nl.IPSET_ATTR_PORT|nl.NLA_F_NET_BYTEORDER), buf)) diff --git a/ipset_linux_test.go b/ipset_linux_test.go index 298c3a3a9..dc3c5c990 100644 --- a/ipset_linux_test.go +++ b/ipset_linux_test.go @@ -440,6 +440,21 @@ func TestIpsetCreateListAddDelDestroyWithTestCases(t *testing.T) { Replace: false, }, }, + { + desc: "Type-bitmap:port", + setname: "my-test-ipset-bitmap-port", + typename: "bitmap:port", + options: IpsetCreateOptions{ + Replace: true, + Timeout: &timeout, + PortFrom: 0, + PortTo: 1024, + }, + entry: &IPSetEntry{ + Port: &port, + Replace: false, + }, + }, } for _, tC := range testCases { @@ -519,14 +534,17 @@ func TestIpsetCreateListAddDelDestroyWithTestCases(t *testing.T) { } if tC.entry.Port != nil { - if *result.Entries[0].Protocol != *tC.entry.Protocol { - t.Fatalf("expected protocol to be '%d', got '%d'", *tC.entry.Protocol, *result.Entries[0].Protocol) - } if *result.Entries[0].Port != *tC.entry.Port { t.Fatalf("expected port to be '%d', got '%d'", *tC.entry.Port, *result.Entries[0].Port) } } + if tC.entry.Protocol != nil { + if result.Entries[0].Protocol == nil || *result.Entries[0].Protocol != *tC.entry.Protocol { + t.Fatalf("expected protocol to be '%d', got '%v'", *tC.entry.Protocol, result.Entries[0].Protocol) + } + } + if tC.entry.MAC != nil { if result.Entries[0].MAC.String() != tC.entry.MAC.String() { t.Fatalf("expected mac to be '%v', got '%v'", tC.entry.MAC, result.Entries[0].MAC)