| 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 libscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/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 | "fmtscip-go gomod github.com/golang/go/src go1.16 fmt/" |
| 32 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 33 | "unsafescip-go gomod github.com/golang/go/src go1.16 unsafe/" |
| 34 | |
| 35 | "github.com/networkplumbing/go-nft/nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/" |
| 36 | ) |
| 37 | |
| 38 | const ( |
| 39 | cmdListscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/cmdList. = "list" |
| 40 | cmdRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/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 ReadConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/ReadConfig().(filterCommands ...string) (*nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, error) { |
| 47 | whatToList := cmdRulesetscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/cmdRuleset. |
| 48 | if len(filterCommands) > 0 { |
| 49 | whatToList = stringsscip-go gomod github.com/golang/go/src go1.16 strings/.Joinscip-go gomod github.com/golang/go/src go1.16 strings/Join().(filterCommands, " ") |
| 50 | } |
| 51 | stdout, err := libNftablesRunCmdscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/libNftablesRunCmd().(fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.16 fmt/Sprintf().("%s %s", cmdListscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/cmdList., whatToList)) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | config := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewConfig().() |
| 57 | if err := config.FromJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FromJSON().(stdout); err != nil { |
| 58 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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 ApplyConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/ApplyConfig().(c *nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#) error { |
| 67 | data, err := c.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().() |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | if _, err = libNftablesRunCmdscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/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 ApplyConfigEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/ApplyConfigEcho().(c *nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#) (*nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, error) { |
| 83 | data, err := c.ToJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#ToJSON().() |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | |
| 88 | stdout, err := libNftablesRunCmdEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/libNftablesRunCmdEcho().(string(data)) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | |
| 93 | config := nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/.NewConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewConfig().() |
| 94 | if err := config.FromJSONscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#FromJSON().(stdout); err != nil { |
| 95 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 fmt/Errorf().("failed to parse echo: %v", err) |
| 96 | } |
| 97 | |
| 98 | return config, nil |
| 99 | } |
| 100 | |
| 101 | func libNftablesRunCmdscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/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 libNftablesRunCmdContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/libNftablesRunCmdContext().(nft, cmd) |
| 108 | } |
| 109 | |
| 110 | func libNftablesRunCmdEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/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 libNftablesRunCmdContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/libNftablesRunCmdContext().(nft, cmd) |
| 117 | } |
| 118 | |
| 119 | func libNftablesRunCmdContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/lib`/libNftablesRunCmdContext().(nft *C.struct_nft_ctx, cmd string) ([]byte, error) { |
| 120 | |
| 121 | buf := C.CString(cmd) |
| 122 | defer C.free(unsafescip-go gomod github.com/golang/go/src go1.16 unsafe/.Pointer(buf)) |
| 123 | |
| 124 | rc := C.nft_ctx_buffer_output(nft) |
| 125 | if rc != C.EXIT_SUCCESS { |
| 126 | return nil, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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, fmtscip-go gomod github.com/golang/go/src go1.16 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.16 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(unsafescip-go gomod github.com/golang/go/src go1.16 unsafe/.Pointer(config), configLen), nil |
| 143 | } |