2018-06-04 15:48:32 -05:00
|
|
|
// Copyright 2018 Google LLC. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
2019-02-18 06:01:32 -06:00
|
|
|
"encoding/binary"
|
2018-06-23 14:12:14 -05:00
|
|
|
"fmt"
|
|
|
|
|
2018-06-04 15:48:32 -05:00
|
|
|
"github.com/google/nftables/binaryutil"
|
|
|
|
"github.com/mdlayher/netlink"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Immediate struct {
|
|
|
|
Register uint32
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2022-05-14 11:45:18 -05:00
|
|
|
func (e *Immediate) marshal(fam byte) ([]byte, error) {
|
2024-07-24 01:26:13 -05:00
|
|
|
data, err := e.marshalData(fam)
|
2018-06-04 15:48:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-24 01:26:13 -05:00
|
|
|
return netlink.MarshalAttributes([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_EXPR_NAME, Data: []byte("immediate\x00")},
|
|
|
|
{Type: unix.NLA_F_NESTED | unix.NFTA_EXPR_DATA, Data: data},
|
|
|
|
})
|
|
|
|
}
|
2018-06-04 15:48:32 -05:00
|
|
|
|
2024-07-24 01:26:13 -05:00
|
|
|
func (e *Immediate) marshalData(fam byte) ([]byte, error) {
|
|
|
|
immData, err := netlink.MarshalAttributes([]netlink.Attribute{
|
|
|
|
{Type: unix.NFTA_DATA_VALUE, Data: e.Data},
|
2018-06-04 15:48:32 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-24 01:26:13 -05:00
|
|
|
|
2018-06-04 15:48:32 -05:00
|
|
|
return netlink.MarshalAttributes([]netlink.Attribute{
|
2024-07-24 01:26:13 -05:00
|
|
|
{Type: unix.NFTA_IMMEDIATE_DREG, Data: binaryutil.BigEndian.PutUint32(e.Register)},
|
|
|
|
{Type: unix.NLA_F_NESTED | unix.NFTA_IMMEDIATE_DATA, Data: immData},
|
2018-06-04 15:48:32 -05:00
|
|
|
})
|
|
|
|
}
|
2018-06-23 14:12:14 -05:00
|
|
|
|
2022-05-14 11:45:18 -05:00
|
|
|
func (e *Immediate) unmarshal(fam byte, data []byte) error {
|
2019-02-18 06:01:32 -06:00
|
|
|
ad, err := netlink.NewAttributeDecoder(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ad.ByteOrder = binary.BigEndian
|
|
|
|
for ad.Next() {
|
|
|
|
switch ad.Type() {
|
|
|
|
case unix.NFTA_IMMEDIATE_DREG:
|
|
|
|
e.Register = ad.Uint32()
|
|
|
|
case unix.NFTA_IMMEDIATE_DATA:
|
|
|
|
nestedAD, err := netlink.NewAttributeDecoder(ad.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("nested NewAttributeDecoder() failed: %v", err)
|
|
|
|
}
|
|
|
|
for nestedAD.Next() {
|
|
|
|
switch nestedAD.Type() {
|
|
|
|
case unix.NFTA_DATA_VALUE:
|
|
|
|
e.Data = nestedAD.Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nestedAD.Err() != nil {
|
|
|
|
return fmt.Errorf("decoding immediate: %v", nestedAD.Err())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ad.Err()
|
2018-06-23 14:12:14 -05:00
|
|
|
}
|