| 1 | /* |
| 2 | * This file is part of the go-nft project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * Copyright 2021 Red Hat, Inc. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | package nft |
| 21 | |
| 22 | import ( |
| 23 | "context" |
| 24 | "time" |
| 25 | |
| 26 | nftconfig "github.com/networkplumbing/go-nft/nft/config" |
| 27 | nftexec "github.com/networkplumbing/go-nft/nft/exec" |
| 28 | ) |
| 29 | |
| 30 | type Config = nftconfig.Config |
| 31 | |
| 32 | const ( |
| 33 | defaultTimeout = 30 * time.Second |
| 34 | ) |
| 35 | |
| 36 | // NewConfig returns a new nftables config structure. |
| 37 | func NewConfig() *nftconfig.Config { |
| 38 | return nftconfig.New() |
| 39 | } |
| 40 | |
| 41 | // ReadConfig loads the nftables configuration from the system and |
| 42 | // returns it as a nftables config structure. |
| 43 | // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 44 | func ReadConfig(filterCommands ...string) (*Config, error) { |
| 45 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 46 | defer cancel() |
| 47 | return ReadConfigContext(ctx, filterCommands...) |
| 48 | } |
| 49 | |
| 50 | // ReadConfigContext loads the nftables configuration from the system and |
| 51 | // returns it as a nftables config structure. |
| 52 | // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 53 | func ReadConfigContext(ctx context.Context, filterCommands ...string) (*Config, error) { |
| 54 | return nftexec.ReadConfig(ctx, filterCommands...) |
| 55 | } |
| 56 | |
| 57 | // ApplyConfig applies the given nftables config on the system. |
| 58 | // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 59 | func ApplyConfig(c *Config) error { |
| 60 | ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) |
| 61 | defer cancel() |
| 62 | return ApplyConfigContext(ctx, c) |
| 63 | } |
| 64 | |
| 65 | // ApplyConfigContext applies the given nftables config on the system. |
| 66 | // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 67 | func ApplyConfigContext(ctx context.Context, c *Config) error { |
| 68 | return nftexec.ApplyConfig(ctx, c) |
| 69 | } |
| 70 | |
| 71 | // ApplyConfigEcho applies the given nftables config on the system, echoing |
| 72 | // back the added elements with their assigned handles |
| 73 | // The system is expected to have the `nft` executable deployed and nftables enabled in the kernel. |
| 74 | func ApplyConfigEcho(ctx context.Context, c *Config) (*Config, error) { |
| 75 | return nftexec.ApplyConfigEcho(ctx, c) |
| 76 | } |