| 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 configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/ |
| 21 | |
| 22 | import ( |
| 23 | "bytesscip-go gomod github.com/golang/go/src go1.16 bytes/" |
| 24 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/" |
| 25 | |
| 26 | "github.com/networkplumbing/go-nft/nft/schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/" |
| 27 | ) |
| 28 | |
| 29 | // AddRule appends the given rule to the nftable config. |
| 30 | // The rule is added without an explicit action (`add`). |
| 31 | // Adding multiple times the same rule will result in multiple identical rules when applied. |
| 32 | func (c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#) AddRulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#AddRule().(rule *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#) { |
| 33 | nftable := schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Nftablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#{Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#Rule.: rule} |
| 34 | c.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables. = append(c.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables., nftable) |
| 35 | } |
| 36 | |
| 37 | // DeleteRule appends a given rule to the nftable config |
| 38 | // with the `delete` action. |
| 39 | // A rule is identified by its handle ID and it must be present in the given rule. |
| 40 | // Attempting to delete a non-existing rule, results with a failure when the config is applied. |
| 41 | // A common usage is to use LookupRule() and then to pass the result to DeleteRule. |
| 42 | func (c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#) DeleteRulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#DeleteRule().(rule *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#) { |
| 43 | nftable := schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Nftablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#{Deletescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#Delete.: &schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Objectsscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Objects#{Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Objects#Rule.: rule}} |
| 44 | c.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables. = append(c.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables., nftable) |
| 45 | } |
| 46 | |
| 47 | // LookupRule searches the configuration for a matching rule and returns it. |
| 48 | // The rule is matched first by the table and chain. |
| 49 | // Other matching fields are optional (nil or an empty string arguments imply no-matching). |
| 50 | // Mutating the returned chain will result in mutating the configuration. |
| 51 | func (c *Configscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#) LookupRulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/Config#LookupRule().(toFind *schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#) []*schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule# { |
| 52 | var rules []*schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule# |
| 53 | |
| 54 | for _, nftable := range c.Nftablesscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Root#Nftables. { |
| 55 | if r := nftable.Rulescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Nftable#Rule.; r != nil { |
| 56 | match := r.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Table. == toFind.Tablescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Table. && r.Familyscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Family. == toFind.Familyscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Family. && r.Chainscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Chain. == toFind.Chainscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Chain. |
| 57 | if match { |
| 58 | if h := toFind.Handlescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Handle.; h != nil { |
| 59 | match = match && r.Handlescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Handle. != nil && *r.Handlescip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Handle. == *h |
| 60 | } |
| 61 | if i := toFind.Indexscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Index.; i != nil { |
| 62 | match = match && r.Indexscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Index. != nil && *r.Indexscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Index. == *i |
| 63 | } |
| 64 | if co := toFind.Commentscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Comment.; co != "" { |
| 65 | match = match && r.Commentscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Comment. == co |
| 66 | } |
| 67 | if toFindStatements := toFind.Exprscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Expr.; toFindStatements != nil { |
| 68 | if match = match && len(toFindStatements) == len(r.Exprscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Expr.); match { |
| 69 | for i, toFindStatement := range toFindStatements { |
| 70 | equal, err := areStatementsEqualscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/areStatementsEqual().(toFindStatement, r.Exprscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Rule#Expr.[i]) |
| 71 | match = match && err == nil && equal |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | if match { |
| 76 | rules = append(rules, r) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return rules |
| 82 | } |
| 83 | |
| 84 | func areStatementsEqualscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/config`/areStatementsEqual().(statementA, statementB schemascip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/.Statementscip-go gomod github.com/networkplumbing/go-nft . `github.com/networkplumbing/go-nft/nft/schema`/Statement#) (bool, error) { |
| 85 | statementARow, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(statementA) |
| 86 | if err != nil { |
| 87 | return false, err |
| 88 | } |
| 89 | statementBRow, err := jsonscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.16 `encoding/json`/Marshal().(statementB) |
| 90 | if err != nil { |
| 91 | return false, err |
| 92 | } |
| 93 | return bytesscip-go gomod github.com/golang/go/src go1.16 bytes/.Equalscip-go gomod github.com/golang/go/src go1.16 bytes/Equal().(statementARow, statementBRow), nil |
| 94 | } |