Skip to content

Commit a833fb5

Browse files
florianljsimonetti
andauthored
add netlink/rule (#139)
* add netlink/rule Signed-off-by: Florian Lehner <[email protected]> * Add some fuzzing corpus Signed-off-by: Jeroen Simonetti <[email protected]> Co-authored-by: Jeroen Simonetti <[email protected]>
1 parent d380b50 commit a833fb5

File tree

132 files changed

+698
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+698
-1
lines changed

conn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Conn struct {
1717
Address *AddressService
1818
Route *RouteService
1919
Neigh *NeighService
20+
Rule *RuleService
2021
}
2122

2223
var _ conn = &netlink.Conn{}
@@ -54,6 +55,7 @@ func newConn(c conn) *Conn {
5455
rtc.Address = &AddressService{c: rtc}
5556
rtc.Route = &RouteService{c: rtc}
5657
rtc.Neigh = &NeighService{c: rtc}
58+
rtc.Rule = &RuleService{c: rtc}
5759

5860
return rtc
5961
}
@@ -179,6 +181,8 @@ func unpackMessages(msgs []netlink.Message) ([]Message, error) {
179181
m = &RouteMessage{}
180182
case unix.RTM_GETNEIGH, unix.RTM_NEWNEIGH, unix.RTM_DELNEIGH:
181183
m = &NeighMessage{}
184+
case unix.RTM_GETRULE, unix.RTM_NEWRULE, unix.RTM_DELRULE:
185+
m = &RuleMessage{}
182186
default:
183187
continue
184188
}

example_rule_list_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package rtnetlink_test
2+
3+
import (
4+
"log"
5+
6+
"github.com/jsimonetti/rtnetlink"
7+
)
8+
9+
// List all rules
10+
func Example_listRule() {
11+
// Dial a connection to the rtnetlink socket
12+
conn, err := rtnetlink.Dial(nil)
13+
if err != nil {
14+
log.Fatal(err)
15+
}
16+
defer conn.Close()
17+
18+
// Request a list of rules
19+
rules, err := conn.Rule.List()
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
24+
for _, rule := range rules {
25+
log.Printf("%+v", rule)
26+
}
27+
}

fuzz.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,17 @@ func FuzzNeighMessage(data []byte) int {
5858

5959
return 1
6060
}
61+
62+
// FuzzRuleMessage will fuzz a RuleMessage
63+
func FuzzRuleMessage(data []byte) int {
64+
m := &RuleMessage{}
65+
if err := (m).UnmarshalBinary(data); err != nil {
66+
return 0
67+
}
68+
69+
if _, err := m.MarshalBinary(); err != nil {
70+
panic(err)
71+
}
72+
73+
return 1
74+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require (
66
github.com/cilium/ebpf v0.8.1
77
github.com/google/go-cmp v0.5.7
88
github.com/mdlayher/netlink v1.6.0
9-
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27
9+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f
1010
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
3434
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3535
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27 h1:XDXtA5hveEEV8JB2l7nhMTp3t3cHp9ZpwcdjqyEWLlo=
3636
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f h1:8w7RhxzTVgUzw/AH/9mUV5q0vMgy40SQRursCcfmkCw=
38+
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3739
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
3840
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
3941
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

internal/unix/types_linux.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,32 @@ const (
108108
RT_SCOPE_UNIVERSE = linux.RT_SCOPE_UNIVERSE
109109
RT_SCOPE_HOST = linux.RT_SCOPE_HOST
110110
RT_SCOPE_LINK = linux.RT_SCOPE_LINK
111+
RTM_NEWRULE = linux.RTM_NEWRULE
112+
RTM_GETRULE = linux.RTM_GETRULE
113+
RTM_DELRULE = linux.RTM_DELRULE
114+
FRA_UNSPEC = linux.FRA_UNSPEC
115+
FRA_DST = linux.FRA_DST
116+
FRA_SRC = linux.FRA_SRC
117+
FRA_IIFNAME = linux.FRA_IIFNAME
118+
FRA_GOTO = linux.FRA_GOTO
119+
FRA_UNUSED2 = linux.FRA_UNUSED2
120+
FRA_PRIORITY = linux.FRA_PRIORITY
121+
FRA_UNUSED3 = linux.FRA_UNUSED3
122+
FRA_UNUSED4 = linux.FRA_UNUSED4
123+
FRA_UNUSED5 = linux.FRA_UNUSED5
124+
FRA_FWMARK = linux.FRA_FWMARK
125+
FRA_FLOW = linux.FRA_FLOW
126+
FRA_TUN_ID = linux.FRA_TUN_ID
127+
FRA_SUPPRESS_IFGROUP = linux.FRA_SUPPRESS_IFGROUP
128+
FRA_SUPPRESS_PREFIXLEN = linux.FRA_SUPPRESS_PREFIXLEN
129+
FRA_TABLE = linux.FRA_TABLE
130+
FRA_FWMASK = linux.FRA_FWMASK
131+
FRA_OIFNAME = linux.FRA_OIFNAME
132+
FRA_PAD = linux.FRA_PAD
133+
FRA_L3MDEV = linux.FRA_L3MDEV
134+
FRA_UID_RANGE = linux.FRA_UID_RANGE
135+
FRA_PROTOCOL = linux.FRA_PROTOCOL
136+
FRA_IP_PROTO = linux.FRA_IP_PROTO
137+
FRA_SPORT_RANGE = linux.FRA_SPORT_RANGE
138+
FRA_DPORT_RANGE = linux.FRA_DPORT_RANGE
111139
)

internal/unix/types_other.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,32 @@ const (
104104
RT_SCOPE_UNIVERSE = 0x0
105105
RT_SCOPE_HOST = 0xfe
106106
RT_SCOPE_LINK = 0xfd
107+
RTM_NEWRULE = 0x20
108+
RTM_GETRULE = 0x22
109+
RTM_DELRULE = 0x21
110+
FRA_UNSPEC = 0x0
111+
FRA_DST = 0x1
112+
FRA_SRC = 0x2
113+
FRA_IIFNAME = 0x3
114+
FRA_GOTO = 0x4
115+
FRA_UNUSED2 = 0x5
116+
FRA_PRIORITY = 0x6
117+
FRA_UNUSED3 = 0x7
118+
FRA_UNUSED4 = 0x8
119+
FRA_UNUSED5 = 0x9
120+
FRA_FWMARK = 0xa
121+
FRA_FLOW = 0xb
122+
FRA_TUN_ID = 0xc
123+
FRA_SUPPRESS_IFGROUP = 0xd
124+
FRA_SUPPRESS_PREFIXLEN = 0xe
125+
FRA_TABLE = 0xf
126+
FRA_FWMASK = 0x10
127+
FRA_OIFNAME = 0x11
128+
FRA_PAD = 0x12
129+
FRA_L3MDEV = 0x13
130+
FRA_UID_RANGE = 0x14
131+
FRA_PROTOCOL = 0x15
132+
FRA_IP_PROTO = 0x16
133+
FRA_SPORT_RANGE = 0x17
134+
FRA_DPORT_RANGE = 0x18
107135
)

0 commit comments

Comments
 (0)