Debian Codegraph @

link/netfilter.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "fmt"
7
8 "github.com/cilium/ebpf"
9 "github.com/cilium/ebpf/internal/sys"
10 )
11
12 const NetfilterIPDefrag NetfilterAttachFlags = 0 // Enable IP packet defragmentation
13
14 type NetfilterAttachFlags uint32
15
16 type NetfilterInetHook = sys.NetfilterInetHook
17
18 const (
19 NetfilterInetPreRouting = sys.NF_INET_PRE_ROUTING
20 NetfilterInetLocalIn = sys.NF_INET_LOCAL_IN
21 NetfilterInetForward = sys.NF_INET_FORWARD
22 NetfilterInetLocalOut = sys.NF_INET_LOCAL_OUT
23 NetfilterInetPostRouting = sys.NF_INET_POST_ROUTING
24 )
25
26 type NetfilterProtocolFamily = sys.NetfilterProtocolFamily
27
28 const (
29 NetfilterProtoUnspec = sys.NFPROTO_UNSPEC
30 NetfilterProtoInet = sys.NFPROTO_INET // Inet applies to both IPv4 and IPv6
31 NetfilterProtoIPv4 = sys.NFPROTO_IPV4
32 NetfilterProtoARP = sys.NFPROTO_ARP
33 NetfilterProtoNetdev = sys.NFPROTO_NETDEV
34 NetfilterProtoBridge = sys.NFPROTO_BRIDGE
35 NetfilterProtoIPv6 = sys.NFPROTO_IPV6
36 )
37
38 type NetfilterOptions struct {
39 // Program must be a netfilter BPF program.
40 Program *ebpf.Program
41 // The protocol family.
42 ProtocolFamily NetfilterProtocolFamily
43 // The netfilter hook to attach to.
44 Hook NetfilterInetHook
45 // Priority within hook
46 Priority int32
47 // Extra link flags
48 Flags uint32
49 // Netfilter flags
50 NetfilterFlags NetfilterAttachFlags
51 }
52
53 type netfilterLink struct {
54 RawLink
55 }
56
57 // AttachNetfilter links a netfilter BPF program to a netfilter hook.
58 func AttachNetfilter(opts NetfilterOptions) (Link, error) {
59 if opts.Program == nil {
60 return nil, fmt.Errorf("netfilter program is nil")
61 }
62
63 if t := opts.Program.Type(); t != ebpf.Netfilter {
64 return nil, fmt.Errorf("invalid program type %s, expected netfilter", t)
65 }
66
67 progFd := opts.Program.FD()
68 if progFd < 0 {
69 return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
70 }
71
72 attr := sys.LinkCreateNetfilterAttr{
73 ProgFd: uint32(opts.Program.FD()),
74 AttachType: sys.BPF_NETFILTER,
75 Flags: opts.Flags,
76 Pf: opts.ProtocolFamily,
77 Hooknum: opts.Hook,
78 Priority: opts.Priority,
79 NetfilterFlags: uint32(opts.NetfilterFlags),
80 }
81
82 fd, err := sys.LinkCreateNetfilter(&attr)
83 if err != nil {
84 return nil, fmt.Errorf("attach netfilter link: %w", err)
85 }
86
87 return &netfilterLink{RawLink{fd, ""}}, nil
88 }
89
90 func (*netfilterLink) Update(_ *ebpf.Program) error {
91 return fmt.Errorf("netfilter update: %w", ErrNotSupported)
92 }
93
94 func (nf *netfilterLink) Info() (*Info, error) {
95 var info sys.NetfilterLinkInfo
96 if err := sys.ObjInfo(nf.fd, &info); err != nil {
97 return nil, fmt.Errorf("netfilter link info: %s", err)
98 }
99 extra := &NetfilterInfo{
100 ProtocolFamily: info.Pf,
101 Hook: info.Hooknum,
102 Priority: info.Priority,
103 Flags: info.Flags,
104 }
105
106 return &Info{
107 info.Type,
108 info.Id,
109 ebpf.ProgramID(info.ProgId),
110 extra,
111 }, nil
112 }
113
114 var _ Link = (*netfilterLink)(nil)