Debian Codegraph @

nft/lib/lib.go

raw symbols SCIP
1 //go:build cgo
2 // +build cgo
3
4 /*
5 * This file is part of the go-nft project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Copyright 2021 Red Hat, Inc.
20 *
21 */
22 package lib
23
24 // #cgo CFLAGS: -g -Wall
25 // #cgo LDFLAGS: -lnftables
26 // #include <nftables/libnftables.h>
27 // #include <stdlib.h>
28 // #include <string.h>
29 import "C"
30 import (
31 "fmt"
32 "strings"
33 "unsafe"
34
35 "github.com/networkplumbing/go-nft/nft"
36 )
37
38 const (
39 cmdList = "list"
40 cmdRuleset = "ruleset"
41 )
42
43 // ReadConfig loads the nftables configuration from the system and
44 // returns it as a nftables config structure.
45 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
46 func ReadConfig(filterCommands ...string) (*nft.Config, error) {
47 whatToList := cmdRuleset
48 if len(filterCommands) > 0 {
49 whatToList = strings.Join(filterCommands, " ")
50 }
51 stdout, err := libNftablesRunCmd(fmt.Sprintf("%s %s", cmdList, whatToList))
52 if err != nil {
53 return nil, err
54 }
55
56 config := nft.NewConfig()
57 if err := config.FromJSON(stdout); err != nil {
58 return nil, fmt.Errorf("failed to list ruleset: %v", err)
59 }
60
61 return config, nil
62 }
63
64 // ApplyConfig applies the given nftables config on the system.
65 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
66 func ApplyConfig(c *nft.Config) error {
67 data, err := c.ToJSON()
68 if err != nil {
69 return err
70 }
71
72 if _, err = libNftablesRunCmd(string(data)); err != nil {
73 return err
74 }
75
76 return nil
77 }
78
79 // ApplyConfigEcho applies the given nftables config on the system, echoing
80 // back the added elements with their assigned handles
81 // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel.
82 func ApplyConfigEcho(c *nft.Config) (*nft.Config, error) {
83 data, err := c.ToJSON()
84 if err != nil {
85 return nil, err
86 }
87
88 stdout, err := libNftablesRunCmdEcho(string(data))
89 if err != nil {
90 return nil, err
91 }
92
93 config := nft.NewConfig()
94 if err := config.FromJSON(stdout); err != nil {
95 return nil, fmt.Errorf("failed to parse echo: %v", err)
96 }
97
98 return config, nil
99 }
100
101 func libNftablesRunCmd(cmd string) ([]byte, error) {
102 nft := C.nft_ctx_new(C.NFT_CTX_DEFAULT)
103 defer C.nft_ctx_free(nft)
104
105 C.nft_ctx_output_set_flags(nft, C.NFT_CTX_OUTPUT_JSON)
106
107 return libNftablesRunCmdContext(nft, cmd)
108 }
109
110 func libNftablesRunCmdEcho(cmd string) ([]byte, error) {
111 nft := C.nft_ctx_new(C.NFT_CTX_DEFAULT)
112 defer C.nft_ctx_free(nft)
113
114 C.nft_ctx_output_set_flags(nft, C.NFT_CTX_OUTPUT_JSON|C.NFT_CTX_OUTPUT_ECHO|C.NFT_CTX_OUTPUT_HANDLE)
115
116 return libNftablesRunCmdContext(nft, cmd)
117 }
118
119 func libNftablesRunCmdContext(nft *C.struct_nft_ctx, cmd string) ([]byte, error) {
120
121 buf := C.CString(cmd)
122 defer C.free(unsafe.Pointer(buf))
123
124 rc := C.nft_ctx_buffer_output(nft)
125 if rc != C.EXIT_SUCCESS {
126 return nil, fmt.Errorf("failed enabling output buffering (rc=%d)", rc)
127 }
128
129 rc = C.nft_ctx_buffer_error(nft)
130 if rc != C.EXIT_SUCCESS {
131 return nil, fmt.Errorf("failed enabling error buffering (rc=%d)", rc)
132 }
133
134 rc = C.nft_run_cmd_from_buffer(nft, buf)
135 if rc != C.EXIT_SUCCESS {
136 errMsg := C.nft_ctx_get_error_buffer(nft)
137 return nil, fmt.Errorf("failed running cmd (rc=%d): %s", rc, C.GoString(errMsg))
138 }
139
140 config := C.nft_ctx_get_output_buffer(nft)
141 configLen := C.int(C.strlen(config))
142 return C.GoBytes(unsafe.Pointer(config), configLen), nil
143 }