diff --git a/.gitignore b/.gitignore index 71c6bf1..a0c21ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,5 @@ .vscode .idea -*.o *.tmp -bpf_bpfeb*.go -bpf_bpfel*.go dae outline.json \ No newline at end of file diff --git a/control/.gitignore b/control/.gitignore deleted file mode 100644 index ff17f28..0000000 --- a/control/.gitignore +++ /dev/null @@ -1 +0,0 @@ -bpf_objects_wan_lan.go \ No newline at end of file diff --git a/control/bpf_bpfeb.go b/control/bpf_bpfeb.go new file mode 100644 index 0000000..d7ab4b0 --- /dev/null +++ b/control/bpf_bpfeb.go @@ -0,0 +1,256 @@ +// Code generated by bpf2go; DO NOT EDIT. +//go:build arm64be || armbe || mips || mips64 || mips64p32 || ppc64 || s390 || s390x || sparc || sparc64 +// +build arm64be armbe mips mips64 mips64p32 ppc64 s390 s390x sparc sparc64 + +package control + +import ( + "bytes" + _ "embed" + "fmt" + "io" + + "github.com/cilium/ebpf" +) + +type bpfDomainRouting struct{ Bitmap [3]uint32 } + +type bpfDstRoutingResult struct { + Ip [4]uint32 + Port uint16 + _ [2]byte + RoutingResult bpfRoutingResult +} + +type bpfIfParams struct { + RxCksmOffload bool + TxL4CksmIp4Offload bool + TxL4CksmIp6Offload bool + UseNonstandardOffloadAlgorithm bool +} + +type bpfIpPort struct { + Ip struct{ U6Addr8 [16]uint8 } + Port uint16 + _ [6]byte +} + +type bpfLpmKey struct { + TrieKey struct { + Prefixlen uint32 + Data [0]uint8 + } + Data [4]uint32 +} + +type bpfMatchSet struct { + Value [16]uint8 + Not bool + Type uint8 + Outbound uint8 + Unused uint8 + Mark uint32 +} + +type bpfOutboundConnectivityQuery struct { + Outbound uint8 + L4proto uint8 + Ipversion uint8 +} + +type bpfPidPname struct { + Pid uint32 + Pname [16]int8 +} + +type bpfRoutingResult struct { + Mark uint32 + Mac [6]uint8 + Outbound uint8 + Pname [16]uint8 + _ [1]byte + Pid uint32 +} + +type bpfTuples struct { + Sip struct{ U6Addr8 [16]uint8 } + Dip struct{ U6Addr8 [16]uint8 } + Sport uint16 + Dport uint16 + L4proto uint8 + _ [3]byte +} + +// loadBpf returns the embedded CollectionSpec for bpf. +func loadBpf() (*ebpf.CollectionSpec, error) { + reader := bytes.NewReader(_BpfBytes) + spec, err := ebpf.LoadCollectionSpecFromReader(reader) + if err != nil { + return nil, fmt.Errorf("can't load bpf: %w", err) + } + + return spec, err +} + +// loadBpfObjects loads bpf and converts it into a struct. +// +// The following types are suitable as obj argument: +// +// *bpfObjects +// *bpfPrograms +// *bpfMaps +// +// See ebpf.CollectionSpec.LoadAndAssign documentation for details. +func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error { + spec, err := loadBpf() + if err != nil { + return err + } + + return spec.LoadAndAssign(obj, opts) +} + +// bpfSpecs contains maps and programs before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfSpecs struct { + bpfProgramSpecs + bpfMapSpecs +} + +// bpfSpecs contains programs before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfProgramSpecs struct { + TproxyLanEgress *ebpf.ProgramSpec `ebpf:"tproxy_lan_egress"` + TproxyLanIngress *ebpf.ProgramSpec `ebpf:"tproxy_lan_ingress"` + TproxyWanCgConnect4 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_connect4"` + TproxyWanCgConnect6 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_connect6"` + TproxyWanCgSendmsg4 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sendmsg4"` + TproxyWanCgSendmsg6 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sendmsg6"` + TproxyWanCgSockCreate *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sock_create"` + TproxyWanCgSockRelease *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sock_release"` + TproxyWanEgress *ebpf.ProgramSpec `ebpf:"tproxy_wan_egress"` + TproxyWanIngress *ebpf.ProgramSpec `ebpf:"tproxy_wan_ingress"` +} + +// bpfMapSpecs contains maps before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfMapSpecs struct { + CookiePidMap *ebpf.MapSpec `ebpf:"cookie_pid_map"` + DomainRoutingMap *ebpf.MapSpec `ebpf:"domain_routing_map"` + H_portMap *ebpf.MapSpec `ebpf:"h_port_map"` + IfindexParamsMap *ebpf.MapSpec `ebpf:"ifindex_params_map"` + L4protoIpversionMap *ebpf.MapSpec `ebpf:"l4proto_ipversion_map"` + ListenSocketMap *ebpf.MapSpec `ebpf:"listen_socket_map"` + LpmArrayMap *ebpf.MapSpec `ebpf:"lpm_array_map"` + LpmKeyMap *ebpf.MapSpec `ebpf:"lpm_key_map"` + OutboundConnectivityMap *ebpf.MapSpec `ebpf:"outbound_connectivity_map"` + ParamMap *ebpf.MapSpec `ebpf:"param_map"` + RoutingMap *ebpf.MapSpec `ebpf:"routing_map"` + RoutingTuplesMap *ebpf.MapSpec `ebpf:"routing_tuples_map"` + TcpDstMap *ebpf.MapSpec `ebpf:"tcp_dst_map"` + TgidPnameMap *ebpf.MapSpec `ebpf:"tgid_pname_map"` + UnusedLpmType *ebpf.MapSpec `ebpf:"unused_lpm_type"` +} + +// bpfObjects contains all objects after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfObjects struct { + bpfPrograms + bpfMaps +} + +func (o *bpfObjects) Close() error { + return _BpfClose( + &o.bpfPrograms, + &o.bpfMaps, + ) +} + +// bpfMaps contains all maps after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfMaps struct { + CookiePidMap *ebpf.Map `ebpf:"cookie_pid_map"` + DomainRoutingMap *ebpf.Map `ebpf:"domain_routing_map"` + H_portMap *ebpf.Map `ebpf:"h_port_map"` + IfindexParamsMap *ebpf.Map `ebpf:"ifindex_params_map"` + L4protoIpversionMap *ebpf.Map `ebpf:"l4proto_ipversion_map"` + ListenSocketMap *ebpf.Map `ebpf:"listen_socket_map"` + LpmArrayMap *ebpf.Map `ebpf:"lpm_array_map"` + LpmKeyMap *ebpf.Map `ebpf:"lpm_key_map"` + OutboundConnectivityMap *ebpf.Map `ebpf:"outbound_connectivity_map"` + ParamMap *ebpf.Map `ebpf:"param_map"` + RoutingMap *ebpf.Map `ebpf:"routing_map"` + RoutingTuplesMap *ebpf.Map `ebpf:"routing_tuples_map"` + TcpDstMap *ebpf.Map `ebpf:"tcp_dst_map"` + TgidPnameMap *ebpf.Map `ebpf:"tgid_pname_map"` + UnusedLpmType *ebpf.Map `ebpf:"unused_lpm_type"` +} + +func (m *bpfMaps) Close() error { + return _BpfClose( + m.CookiePidMap, + m.DomainRoutingMap, + m.H_portMap, + m.IfindexParamsMap, + m.L4protoIpversionMap, + m.ListenSocketMap, + m.LpmArrayMap, + m.LpmKeyMap, + m.OutboundConnectivityMap, + m.ParamMap, + m.RoutingMap, + m.RoutingTuplesMap, + m.TcpDstMap, + m.TgidPnameMap, + m.UnusedLpmType, + ) +} + +// bpfPrograms contains all programs after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfPrograms struct { + TproxyLanEgress *ebpf.Program `ebpf:"tproxy_lan_egress"` + TproxyLanIngress *ebpf.Program `ebpf:"tproxy_lan_ingress"` + TproxyWanCgConnect4 *ebpf.Program `ebpf:"tproxy_wan_cg_connect4"` + TproxyWanCgConnect6 *ebpf.Program `ebpf:"tproxy_wan_cg_connect6"` + TproxyWanCgSendmsg4 *ebpf.Program `ebpf:"tproxy_wan_cg_sendmsg4"` + TproxyWanCgSendmsg6 *ebpf.Program `ebpf:"tproxy_wan_cg_sendmsg6"` + TproxyWanCgSockCreate *ebpf.Program `ebpf:"tproxy_wan_cg_sock_create"` + TproxyWanCgSockRelease *ebpf.Program `ebpf:"tproxy_wan_cg_sock_release"` + TproxyWanEgress *ebpf.Program `ebpf:"tproxy_wan_egress"` + TproxyWanIngress *ebpf.Program `ebpf:"tproxy_wan_ingress"` +} + +func (p *bpfPrograms) Close() error { + return _BpfClose( + p.TproxyLanEgress, + p.TproxyLanIngress, + p.TproxyWanCgConnect4, + p.TproxyWanCgConnect6, + p.TproxyWanCgSendmsg4, + p.TproxyWanCgSendmsg6, + p.TproxyWanCgSockCreate, + p.TproxyWanCgSockRelease, + p.TproxyWanEgress, + p.TproxyWanIngress, + ) +} + +func _BpfClose(closers ...io.Closer) error { + for _, closer := range closers { + if err := closer.Close(); err != nil { + return err + } + } + return nil +} + +// Do not access this directly. +//go:embed bpf_bpfeb.o +var _BpfBytes []byte diff --git a/control/bpf_bpfeb.o b/control/bpf_bpfeb.o new file mode 100644 index 0000000..e9c1849 Binary files /dev/null and b/control/bpf_bpfeb.o differ diff --git a/control/bpf_bpfel.go b/control/bpf_bpfel.go new file mode 100644 index 0000000..420bb83 --- /dev/null +++ b/control/bpf_bpfel.go @@ -0,0 +1,256 @@ +// Code generated by bpf2go; DO NOT EDIT. +//go:build 386 || amd64 || amd64p32 || arm || arm64 || mips64le || mips64p32le || mipsle || ppc64le || riscv64 +// +build 386 amd64 amd64p32 arm arm64 mips64le mips64p32le mipsle ppc64le riscv64 + +package control + +import ( + "bytes" + _ "embed" + "fmt" + "io" + + "github.com/cilium/ebpf" +) + +type bpfDomainRouting struct{ Bitmap [3]uint32 } + +type bpfDstRoutingResult struct { + Ip [4]uint32 + Port uint16 + _ [2]byte + RoutingResult bpfRoutingResult +} + +type bpfIfParams struct { + RxCksmOffload bool + TxL4CksmIp4Offload bool + TxL4CksmIp6Offload bool + UseNonstandardOffloadAlgorithm bool +} + +type bpfIpPort struct { + Ip struct{ U6Addr8 [16]uint8 } + Port uint16 + _ [6]byte +} + +type bpfLpmKey struct { + TrieKey struct { + Prefixlen uint32 + Data [0]uint8 + } + Data [4]uint32 +} + +type bpfMatchSet struct { + Value [16]uint8 + Not bool + Type uint8 + Outbound uint8 + Unused uint8 + Mark uint32 +} + +type bpfOutboundConnectivityQuery struct { + Outbound uint8 + L4proto uint8 + Ipversion uint8 +} + +type bpfPidPname struct { + Pid uint32 + Pname [16]int8 +} + +type bpfRoutingResult struct { + Mark uint32 + Mac [6]uint8 + Outbound uint8 + Pname [16]uint8 + _ [1]byte + Pid uint32 +} + +type bpfTuples struct { + Sip struct{ U6Addr8 [16]uint8 } + Dip struct{ U6Addr8 [16]uint8 } + Sport uint16 + Dport uint16 + L4proto uint8 + _ [3]byte +} + +// loadBpf returns the embedded CollectionSpec for bpf. +func loadBpf() (*ebpf.CollectionSpec, error) { + reader := bytes.NewReader(_BpfBytes) + spec, err := ebpf.LoadCollectionSpecFromReader(reader) + if err != nil { + return nil, fmt.Errorf("can't load bpf: %w", err) + } + + return spec, err +} + +// loadBpfObjects loads bpf and converts it into a struct. +// +// The following types are suitable as obj argument: +// +// *bpfObjects +// *bpfPrograms +// *bpfMaps +// +// See ebpf.CollectionSpec.LoadAndAssign documentation for details. +func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error { + spec, err := loadBpf() + if err != nil { + return err + } + + return spec.LoadAndAssign(obj, opts) +} + +// bpfSpecs contains maps and programs before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfSpecs struct { + bpfProgramSpecs + bpfMapSpecs +} + +// bpfSpecs contains programs before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfProgramSpecs struct { + TproxyLanEgress *ebpf.ProgramSpec `ebpf:"tproxy_lan_egress"` + TproxyLanIngress *ebpf.ProgramSpec `ebpf:"tproxy_lan_ingress"` + TproxyWanCgConnect4 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_connect4"` + TproxyWanCgConnect6 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_connect6"` + TproxyWanCgSendmsg4 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sendmsg4"` + TproxyWanCgSendmsg6 *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sendmsg6"` + TproxyWanCgSockCreate *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sock_create"` + TproxyWanCgSockRelease *ebpf.ProgramSpec `ebpf:"tproxy_wan_cg_sock_release"` + TproxyWanEgress *ebpf.ProgramSpec `ebpf:"tproxy_wan_egress"` + TproxyWanIngress *ebpf.ProgramSpec `ebpf:"tproxy_wan_ingress"` +} + +// bpfMapSpecs contains maps before they are loaded into the kernel. +// +// It can be passed ebpf.CollectionSpec.Assign. +type bpfMapSpecs struct { + CookiePidMap *ebpf.MapSpec `ebpf:"cookie_pid_map"` + DomainRoutingMap *ebpf.MapSpec `ebpf:"domain_routing_map"` + H_portMap *ebpf.MapSpec `ebpf:"h_port_map"` + IfindexParamsMap *ebpf.MapSpec `ebpf:"ifindex_params_map"` + L4protoIpversionMap *ebpf.MapSpec `ebpf:"l4proto_ipversion_map"` + ListenSocketMap *ebpf.MapSpec `ebpf:"listen_socket_map"` + LpmArrayMap *ebpf.MapSpec `ebpf:"lpm_array_map"` + LpmKeyMap *ebpf.MapSpec `ebpf:"lpm_key_map"` + OutboundConnectivityMap *ebpf.MapSpec `ebpf:"outbound_connectivity_map"` + ParamMap *ebpf.MapSpec `ebpf:"param_map"` + RoutingMap *ebpf.MapSpec `ebpf:"routing_map"` + RoutingTuplesMap *ebpf.MapSpec `ebpf:"routing_tuples_map"` + TcpDstMap *ebpf.MapSpec `ebpf:"tcp_dst_map"` + TgidPnameMap *ebpf.MapSpec `ebpf:"tgid_pname_map"` + UnusedLpmType *ebpf.MapSpec `ebpf:"unused_lpm_type"` +} + +// bpfObjects contains all objects after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfObjects struct { + bpfPrograms + bpfMaps +} + +func (o *bpfObjects) Close() error { + return _BpfClose( + &o.bpfPrograms, + &o.bpfMaps, + ) +} + +// bpfMaps contains all maps after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfMaps struct { + CookiePidMap *ebpf.Map `ebpf:"cookie_pid_map"` + DomainRoutingMap *ebpf.Map `ebpf:"domain_routing_map"` + H_portMap *ebpf.Map `ebpf:"h_port_map"` + IfindexParamsMap *ebpf.Map `ebpf:"ifindex_params_map"` + L4protoIpversionMap *ebpf.Map `ebpf:"l4proto_ipversion_map"` + ListenSocketMap *ebpf.Map `ebpf:"listen_socket_map"` + LpmArrayMap *ebpf.Map `ebpf:"lpm_array_map"` + LpmKeyMap *ebpf.Map `ebpf:"lpm_key_map"` + OutboundConnectivityMap *ebpf.Map `ebpf:"outbound_connectivity_map"` + ParamMap *ebpf.Map `ebpf:"param_map"` + RoutingMap *ebpf.Map `ebpf:"routing_map"` + RoutingTuplesMap *ebpf.Map `ebpf:"routing_tuples_map"` + TcpDstMap *ebpf.Map `ebpf:"tcp_dst_map"` + TgidPnameMap *ebpf.Map `ebpf:"tgid_pname_map"` + UnusedLpmType *ebpf.Map `ebpf:"unused_lpm_type"` +} + +func (m *bpfMaps) Close() error { + return _BpfClose( + m.CookiePidMap, + m.DomainRoutingMap, + m.H_portMap, + m.IfindexParamsMap, + m.L4protoIpversionMap, + m.ListenSocketMap, + m.LpmArrayMap, + m.LpmKeyMap, + m.OutboundConnectivityMap, + m.ParamMap, + m.RoutingMap, + m.RoutingTuplesMap, + m.TcpDstMap, + m.TgidPnameMap, + m.UnusedLpmType, + ) +} + +// bpfPrograms contains all programs after they have been loaded into the kernel. +// +// It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. +type bpfPrograms struct { + TproxyLanEgress *ebpf.Program `ebpf:"tproxy_lan_egress"` + TproxyLanIngress *ebpf.Program `ebpf:"tproxy_lan_ingress"` + TproxyWanCgConnect4 *ebpf.Program `ebpf:"tproxy_wan_cg_connect4"` + TproxyWanCgConnect6 *ebpf.Program `ebpf:"tproxy_wan_cg_connect6"` + TproxyWanCgSendmsg4 *ebpf.Program `ebpf:"tproxy_wan_cg_sendmsg4"` + TproxyWanCgSendmsg6 *ebpf.Program `ebpf:"tproxy_wan_cg_sendmsg6"` + TproxyWanCgSockCreate *ebpf.Program `ebpf:"tproxy_wan_cg_sock_create"` + TproxyWanCgSockRelease *ebpf.Program `ebpf:"tproxy_wan_cg_sock_release"` + TproxyWanEgress *ebpf.Program `ebpf:"tproxy_wan_egress"` + TproxyWanIngress *ebpf.Program `ebpf:"tproxy_wan_ingress"` +} + +func (p *bpfPrograms) Close() error { + return _BpfClose( + p.TproxyLanEgress, + p.TproxyLanIngress, + p.TproxyWanCgConnect4, + p.TproxyWanCgConnect6, + p.TproxyWanCgSendmsg4, + p.TproxyWanCgSendmsg6, + p.TproxyWanCgSockCreate, + p.TproxyWanCgSockRelease, + p.TproxyWanEgress, + p.TproxyWanIngress, + ) +} + +func _BpfClose(closers ...io.Closer) error { + for _, closer := range closers { + if err := closer.Close(); err != nil { + return err + } + } + return nil +} + +// Do not access this directly. +//go:embed bpf_bpfel.o +var _BpfBytes []byte diff --git a/control/bpf_bpfel.o b/control/bpf_bpfel.o new file mode 100644 index 0000000..0509770 Binary files /dev/null and b/control/bpf_bpfel.o differ diff --git a/foo.o b/foo.o new file mode 100644 index 0000000..2f884ea Binary files /dev/null and b/foo.o differ