Debian Codegraph @

nft/config/chain.go

raw symbols SCIP
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 config
21
22 import (
23 "github.com/networkplumbing/go-nft/nft/schema"
24 )
25
26 // AddChain appends the given chain to the nftable config.
27 // The chain is added without an explicit action (`add`).
28 // Adding multiple times the same chain has no affect when the config is applied.
29 func (c *Config) AddChain(chain *schema.Chain) {
30 nftable := schema.Nftable{Chain: chain}
31 c.Nftables = append(c.Nftables, nftable)
32 }
33
34 // DeleteChain appends a given chain to the nftable config
35 // with the `delete` action.
36 // Attempting to delete a non-existing chain, results with a failure when the config is applied.
37 // The chain must not contain any rules or be used as a jump target.
38 func (c *Config) DeleteChain(chain *schema.Chain) {
39 nftable := schema.Nftable{Delete: &schema.Objects{Chain: chain}}
40 c.Nftables = append(c.Nftables, nftable)
41 }
42
43 // FlushChain appends a given chain to the nftable config
44 // with the `flush` action.
45 // All rules under the chain are removed (when applied).
46 // Attempting to flush a non-existing chain, results with a failure when the config is applied.
47 func (c *Config) FlushChain(chain *schema.Chain) {
48 nftable := schema.Nftable{Flush: &schema.Objects{Chain: chain}}
49 c.Nftables = append(c.Nftables, nftable)
50 }
51
52 // LookupChain searches the configuration for a matching chain and returns it.
53 // The chain is matched first by the table and chain name.
54 // Other matching fields are optional (for matching base chains).
55 // Mutating the returned chain will result in mutating the configuration.
56 func (c *Config) LookupChain(toFind *schema.Chain) *schema.Chain {
57 for _, nftable := range c.Nftables {
58 if chain := nftable.Chain; chain != nil {
59 match := chain.Table == toFind.Table && chain.Family == toFind.Family && chain.Name == toFind.Name
60 if match {
61 if t := toFind.Type; t != "" {
62 match = match && chain.Type == t
63 }
64 if h := toFind.Hook; h != "" {
65 match = match && chain.Hook == h
66 }
67 if p := toFind.Prio; p != nil {
68 match = match && chain.Prio != nil && *chain.Prio == *p
69 }
70 if p := toFind.Policy; p != "" {
71 match = match && chain.Policy == p
72 }
73 if match {
74 return chain
75 }
76 }
77 }
78 }
79 return nil
80 }