| 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 nftscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ |
| 21 | |
| 22 | import ( |
| 23 | "contextscip-go gomod github.com/golang/go/src go1.16 context/" |
| 24 | "timescip-go gomod github.com/golang/go/src go1.16 time/" |
| 25 | |
| 26 | nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/ "github.com/networkplumbing/go-nft/nft/configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/" |
| 27 | nftexecscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/ "github.com/networkplumbing/go-nft/nft/execscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/" |
| 28 | ) |
| 29 | |
| 30 | type Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config# = nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config# |
| 31 | |
| 32 | const ( |
| 33 | defaultTimeoutscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/defaultTimeout. = 30 * timescip-go gomod github.com/golang/go/src go1.16 time/.Secondscip-go gomod github.com/golang/go/src go1.16 time/Second. |
| 34 | ) |
| 35 | |
| 36 | // NewConfig returns a new nftables config structure. |
| 37 | func NewConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/NewConfig().() *nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config# { |
| 38 | return nftconfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/.Newscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/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 ReadConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ReadConfig().(filterCommands ...string) (*Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, error) { |
| 45 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.16 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.16 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.16 context/.Backgroundscip-go gomod github.com/golang/go/src go1.16 context/Background().(), defaultTimeoutscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/defaultTimeout.) |
| 46 | defer cancel() |
| 47 | return ReadConfigContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/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 ReadConfigContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ReadConfigContext().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, filterCommands ...string) (*Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, error) { |
| 54 | return nftexecscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/.ReadConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/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 ApplyConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ApplyConfig().(c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#) error { |
| 60 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.16 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.16 context/WithTimeout().(contextscip-go gomod github.com/golang/go/src go1.16 context/.Backgroundscip-go gomod github.com/golang/go/src go1.16 context/Background().(), defaultTimeoutscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/defaultTimeout.) |
| 61 | defer cancel() |
| 62 | return ApplyConfigContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/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 ApplyConfigContextscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ApplyConfigContext().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#) error { |
| 68 | return nftexecscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/.ApplyConfigscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/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 ApplyConfigEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/ApplyConfigEcho().(ctx contextscip-go gomod github.com/golang/go/src go1.16 context/.Contextscip-go gomod github.com/golang/go/src go1.16 context/Context#, c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#) (*Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft`/Config#, error) { |
| 75 | return nftexecscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/.ApplyConfigEchoscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/exec`/ApplyConfigEcho().(ctx, c) |
| 76 | } |