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

feat: add connmark support #41

Merged
merged 2 commits into from
Dec 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions nfqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ func (nfqueue *Nfqueue) SetVerdictWithMark(id uint32, verdict, mark int) error {
return nfqueue.setVerdict(id, verdict, false, attributes)
}

// SetVerdictWithConnMark signals the kernel the next action and the connmark for a specified package id
func (nfqueue *Nfqueue) SetVerdictWithConnMark(id uint32, verdict, mark int) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(mark))
ctAttrs, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: ctaMark,
Data: buf,
}})
if err != nil {
return err
}
attributes, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: netlink.Nested | nfQaCt,
Data: ctAttrs,
}})
if err != nil {
return err
}
return nfqueue.setVerdict(id, verdict, false, attributes)
}

// SetVerdictModPacket signals the kernel the next action for an altered packet
func (nfqueue *Nfqueue) SetVerdictModPacket(id uint32, verdict int, packet []byte) error {
data, err := netlink.MarshalAttributes([]netlink.Attribute{{
Expand Down Expand Up @@ -73,6 +94,33 @@ func (nfqueue *Nfqueue) SetVerdictModPacketWithMark(id uint32, verdict, mark int
return nfqueue.setVerdict(id, verdict, false, data)
}

// SetVerdictModPacketWithConnMark signals the kernel the next action and connmark for an altered packet
func (nfqueue *Nfqueue) SetVerdictModPacketWithConnMark(id uint32, verdict, mark int, packet []byte) error {
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(mark))
ctAttrs, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: ctaMark,
Data: buf,
}})
if err != nil {
return err
}
data, err := netlink.MarshalAttributes([]netlink.Attribute{
{
Type: nfQaPayload,
Data: packet,
},
{
Type: netlink.Nested | nfQaCt,
Data: ctAttrs,
},
})
if err != nil {
return err
}
return nfqueue.setVerdict(id, verdict, false, data)
}

// SetVerdict signals the kernel the next action for a specified package id
func (nfqueue *Nfqueue) SetVerdict(id uint32, verdict int) error {
return nfqueue.setVerdict(id, verdict, false, []byte{})
Expand Down
5 changes: 5 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ const (
NfQeueue
NfRepeat
)

// conntrack attributes
const (
ctaMark = 8
)
Loading